]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/tag.c
Merge branch 'master' of git://bogomips.org/git-svn
[thirdparty/git.git] / builtin / tag.c
CommitLineData
62e09ce9
CR
1/*
2 * Builtin "git tag"
3 *
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>,
5 * Carlos Rica <jasampler@gmail.com>
6 * Based on git-tag.sh and mktag.c by Linus Torvalds.
7 */
8
9#include "cache.h"
10#include "builtin.h"
11#include "refs.h"
12#include "tag.h"
13#include "run-command.h"
39686585 14#include "parse-options.h"
ffc4b801
JK
15#include "diff.h"
16#include "revision.h"
39686585
CR
17
18static const char * const git_tag_usage[] = {
1b1dd23f
SB
19 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
20 "git tag -d <tagname>...",
588d0e83 21 "git tag -l [-n[<num>]] [<pattern>...]",
1b1dd23f 22 "git tag -v <tagname>...",
39686585
CR
23 NULL
24};
62e09ce9
CR
25
26static char signingkey[1000];
27
62e09ce9 28struct tag_filter {
588d0e83 29 const char **patterns;
62e09ce9 30 int lines;
32c35cfb 31 struct commit_list *with_commit;
62e09ce9
CR
32};
33
588d0e83
JK
34static int match_pattern(const char **patterns, const char *ref)
35{
36 /* no pattern means match everything */
37 if (!*patterns)
38 return 1;
39 for (; *patterns; patterns++)
40 if (!fnmatch(*patterns, ref, 0))
41 return 1;
42 return 0;
43}
44
ffc4b801
JK
45static int in_commit_list(const struct commit_list *want, struct commit *c)
46{
47 for (; want; want = want->next)
48 if (!hashcmp(want->item->object.sha1, c->object.sha1))
49 return 1;
50 return 0;
51}
52
53static int contains_recurse(struct commit *candidate,
c6d72c49 54 const struct commit_list *want)
ffc4b801
JK
55{
56 struct commit_list *p;
57
58 /* was it previously marked as containing a want commit? */
59 if (candidate->object.flags & TMP_MARK)
60 return 1;
61 /* or marked as not possibly containing a want commit? */
62 if (candidate->object.flags & UNINTERESTING)
63 return 0;
64 /* or are we it? */
65 if (in_commit_list(want, candidate))
66 return 1;
67
68 if (parse_commit(candidate) < 0)
69 return 0;
70
71 /* Otherwise recurse and mark ourselves for future traversals. */
72 for (p = candidate->parents; p; p = p->next) {
c6d72c49 73 if (contains_recurse(p->item, want)) {
ffc4b801
JK
74 candidate->object.flags |= TMP_MARK;
75 return 1;
76 }
77 }
78 candidate->object.flags |= UNINTERESTING;
79 return 0;
80}
81
82static int contains(struct commit *candidate, const struct commit_list *want)
83{
c6d72c49 84 return contains_recurse(candidate, want);
ffc4b801
JK
85}
86
62e09ce9
CR
87static int show_reference(const char *refname, const unsigned char *sha1,
88 int flag, void *cb_data)
89{
90 struct tag_filter *filter = cb_data;
91
588d0e83 92 if (match_pattern(filter->patterns, refname)) {
62e09ce9
CR
93 int i;
94 unsigned long size;
95 enum object_type type;
96 char *buf, *sp, *eol;
97 size_t len;
98
32c35cfb
JG
99 if (filter->with_commit) {
100 struct commit *commit;
101
102 commit = lookup_commit_reference_gently(sha1, 1);
103 if (!commit)
104 return 0;
ffc4b801 105 if (!contains(commit, filter->with_commit))
32c35cfb
JG
106 return 0;
107 }
108
62e09ce9
CR
109 if (!filter->lines) {
110 printf("%s\n", refname);
111 return 0;
112 }
113 printf("%-15s ", refname);
114
e1f14cce
MH
115 buf = read_sha1_file(sha1, &type, &size);
116 if (!buf || !size)
62e09ce9 117 return 0;
e1f14cce
MH
118
119 /* skip header */
120 sp = strstr(buf, "\n\n");
121 if (!sp) {
e317cfaf
CR
122 free(buf);
123 return 0;
124 }
62e09ce9 125 /* only take up to "lines" lines, and strip the signature */
81536b2d 126 size = parse_signature(buf, size);
e317cfaf 127 for (i = 0, sp += 2;
81536b2d 128 i < filter->lines && sp < buf + size;
62e09ce9
CR
129 i++) {
130 if (i)
131 printf("\n ");
132 eol = memchr(sp, '\n', size - (sp - buf));
133 len = eol ? eol - sp : size - (sp - buf);
134 fwrite(sp, len, 1, stdout);
135 if (!eol)
136 break;
137 sp = eol + 1;
138 }
139 putchar('\n');
140 free(buf);
141 }
142
143 return 0;
144}
145
588d0e83 146static int list_tags(const char **patterns, int lines,
32c35cfb 147 struct commit_list *with_commit)
62e09ce9
CR
148{
149 struct tag_filter filter;
62e09ce9 150
588d0e83 151 filter.patterns = patterns;
62e09ce9 152 filter.lines = lines;
32c35cfb 153 filter.with_commit = with_commit;
62e09ce9
CR
154
155 for_each_tag_ref(show_reference, (void *) &filter);
156
62e09ce9
CR
157 return 0;
158}
159
e317cfaf 160typedef int (*each_tag_name_fn)(const char *name, const char *ref,
62e09ce9
CR
161 const unsigned char *sha1);
162
e317cfaf 163static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
62e09ce9
CR
164{
165 const char **p;
166 char ref[PATH_MAX];
167 int had_error = 0;
168 unsigned char sha1[20];
169
170 for (p = argv; *p; p++) {
171 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
172 >= sizeof(ref)) {
d08ebf99 173 error(_("tag name too long: %.*s..."), 50, *p);
62e09ce9
CR
174 had_error = 1;
175 continue;
176 }
177 if (!resolve_ref(ref, sha1, 1, NULL)) {
d08ebf99 178 error(_("tag '%s' not found."), *p);
62e09ce9
CR
179 had_error = 1;
180 continue;
181 }
182 if (fn(*p, ref, sha1))
183 had_error = 1;
184 }
185 return had_error;
186}
187
188static int delete_tag(const char *name, const char *ref,
189 const unsigned char *sha1)
190{
eca35a25 191 if (delete_ref(ref, sha1, 0))
62e09ce9 192 return 1;
d08ebf99 193 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
62e09ce9
CR
194 return 0;
195}
196
197static int verify_tag(const char *name, const char *ref,
198 const unsigned char *sha1)
199{
a6ccbbdb 200 const char *argv_verify_tag[] = {"verify-tag",
62e09ce9
CR
201 "-v", "SHA1_HEX", NULL};
202 argv_verify_tag[2] = sha1_to_hex(sha1);
203
a6ccbbdb 204 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
d08ebf99 205 return error(_("could not verify the tag '%s'"), name);
62e09ce9
CR
206 return 0;
207}
208
fd17f5b5 209static int do_sign(struct strbuf *buffer)
62e09ce9
CR
210{
211 struct child_process gpg;
212 const char *args[4];
213 char *bracket;
214 int len;
c4adea82 215 int i, j;
62e09ce9
CR
216
217 if (!*signingkey) {
774751a8 218 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
e317cfaf 219 sizeof(signingkey)) > sizeof(signingkey) - 1)
d08ebf99 220 return error(_("committer info too long."));
62e09ce9
CR
221 bracket = strchr(signingkey, '>');
222 if (bracket)
223 bracket[1] = '\0';
224 }
225
aba91192
CR
226 /* When the username signingkey is bad, program could be terminated
227 * because gpg exits without reading and then write gets SIGPIPE. */
228 signal(SIGPIPE, SIG_IGN);
229
62e09ce9
CR
230 memset(&gpg, 0, sizeof(gpg));
231 gpg.argv = args;
232 gpg.in = -1;
233 gpg.out = -1;
234 args[0] = "gpg";
235 args[1] = "-bsau";
236 args[2] = signingkey;
237 args[3] = NULL;
238
239 if (start_command(&gpg))
d08ebf99 240 return error(_("could not run gpg."));
62e09ce9 241
fd17f5b5 242 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
aba91192 243 close(gpg.in);
e72ae288 244 close(gpg.out);
aba91192 245 finish_command(&gpg);
d08ebf99 246 return error(_("gpg did not accept the tag data"));
aba91192 247 }
62e09ce9 248 close(gpg.in);
fd17f5b5 249 len = strbuf_read(buffer, gpg.out, 1024);
e72ae288 250 close(gpg.out);
62e09ce9 251
aba91192 252 if (finish_command(&gpg) || !len || len < 0)
d08ebf99 253 return error(_("gpg failed to sign the tag"));
62e09ce9 254
c4adea82
JS
255 /* Strip CR from the line endings, in case we are on Windows. */
256 for (i = j = 0; i < buffer->len; i++)
257 if (buffer->buf[i] != '\r') {
258 if (i != j)
259 buffer->buf[j] = buffer->buf[i];
260 j++;
261 }
262 strbuf_setlen(buffer, j);
263
fd17f5b5 264 return 0;
62e09ce9
CR
265}
266
267static const char tag_template[] =
7fbff25a 268 N_("\n"
62e09ce9
CR
269 "#\n"
270 "# Write a tag message\n"
7fbff25a 271 "#\n");
62e09ce9 272
be15f505
LT
273static void set_signingkey(const char *value)
274{
275 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
d08ebf99 276 die(_("signing key value too long (%.10s...)"), value);
be15f505
LT
277}
278
ef90d6d4 279static int git_tag_config(const char *var, const char *value, void *cb)
62e09ce9
CR
280{
281 if (!strcmp(var, "user.signingkey")) {
282 if (!value)
c96a6d36 283 return config_error_nonbool(var);
be15f505 284 set_signingkey(value);
62e09ce9
CR
285 return 0;
286 }
287
ef90d6d4 288 return git_default_config(var, value, cb);
62e09ce9
CR
289}
290
bab8118a
MH
291static void write_tag_body(int fd, const unsigned char *sha1)
292{
293 unsigned long size;
294 enum object_type type;
e10dfb62 295 char *buf, *sp;
bab8118a
MH
296
297 buf = read_sha1_file(sha1, &type, &size);
298 if (!buf)
299 return;
300 /* skip header */
301 sp = strstr(buf, "\n\n");
302
303 if (!sp || !size || type != OBJ_TAG) {
304 free(buf);
305 return;
306 }
307 sp += 2; /* skip the 2 LFs */
e10dfb62 308 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
bab8118a
MH
309
310 free(buf);
311}
312
3927bbe9
JK
313static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
314{
315 if (sign && do_sign(buf) < 0)
d08ebf99 316 return error(_("unable to sign the tag"));
3927bbe9 317 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
d08ebf99 318 return error(_("unable to write tag file"));
3927bbe9
JK
319 return 0;
320}
321
62e09ce9 322static void create_tag(const unsigned char *object, const char *tag,
fd17f5b5 323 struct strbuf *buf, int message, int sign,
bd46c9a9 324 unsigned char *prev, unsigned char *result)
62e09ce9
CR
325{
326 enum object_type type;
fd17f5b5
PH
327 char header_buf[1024];
328 int header_len;
3927bbe9 329 char *path = NULL;
62e09ce9
CR
330
331 type = sha1_object_info(object, NULL);
e317cfaf 332 if (type <= OBJ_NONE)
d08ebf99 333 die(_("bad object type."));
62e09ce9
CR
334
335 header_len = snprintf(header_buf, sizeof(header_buf),
336 "object %s\n"
337 "type %s\n"
338 "tag %s\n"
339 "tagger %s\n\n",
340 sha1_to_hex(object),
341 typename(type),
342 tag,
774751a8 343 git_committer_info(IDENT_ERROR_ON_NO_NAME));
62e09ce9 344
e317cfaf 345 if (header_len > sizeof(header_buf) - 1)
d08ebf99 346 die(_("tag header too big."));
62e09ce9
CR
347
348 if (!message) {
62e09ce9
CR
349 int fd;
350
351 /* write the template message before editing: */
a4f34cbb 352 path = git_pathdup("TAG_EDITMSG");
62e09ce9
CR
353 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
354 if (fd < 0)
d08ebf99 355 die_errno(_("could not create file '%s'"), path);
bab8118a
MH
356
357 if (!is_null_sha1(prev))
358 write_tag_body(fd, prev);
359 else
7fbff25a 360 write_or_die(fd, _(tag_template), strlen(_(tag_template)));
62e09ce9
CR
361 close(fd);
362
7198203a
SB
363 if (launch_editor(path, buf, NULL)) {
364 fprintf(stderr,
d08ebf99 365 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
366 exit(1);
367 }
62e09ce9 368 }
62e09ce9 369
6d69b6f6 370 stripspace(buf, 1);
62e09ce9 371
fd17f5b5 372 if (!message && !buf->len)
d08ebf99 373 die(_("no tag message?"));
62e09ce9 374
fd17f5b5 375 strbuf_insert(buf, 0, header_buf, header_len);
62e09ce9 376
3927bbe9
JK
377 if (build_tag_object(buf, sign, result) < 0) {
378 if (path)
d08ebf99 379 fprintf(stderr, _("The tag message has been left in %s\n"),
3927bbe9
JK
380 path);
381 exit(128);
382 }
383 if (path) {
691f1a28 384 unlink_or_warn(path);
3927bbe9
JK
385 free(path);
386 }
62e09ce9
CR
387}
388
bd46c9a9
JH
389struct msg_arg {
390 int given;
391 struct strbuf buf;
392};
393
394static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
395{
396 struct msg_arg *msg = opt->value;
397
398 if (!arg)
399 return -1;
400 if (msg->buf.len)
401 strbuf_addstr(&(msg->buf), "\n\n");
402 strbuf_addstr(&(msg->buf), arg);
403 msg->given = 1;
404 return 0;
405}
406
4f0accd6
MS
407static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
408{
409 if (name[0] == '-')
8d9c5010 410 return -1;
4f0accd6
MS
411
412 strbuf_reset(sb);
413 strbuf_addf(sb, "refs/tags/%s", name);
414
8d9c5010 415 return check_refname_format(sb->buf, 0);
4f0accd6
MS
416}
417
62e09ce9
CR
418int cmd_tag(int argc, const char **argv, const char *prefix)
419{
f285a2d7 420 struct strbuf buf = STRBUF_INIT;
4f0accd6 421 struct strbuf ref = STRBUF_INIT;
62e09ce9 422 unsigned char object[20], prev[20];
62e09ce9 423 const char *object_ref, *tag;
62e09ce9
CR
424 struct ref_lock *lock;
425
6fa8342b 426 int annotate = 0, sign = 0, force = 0, lines = -1,
78d776a9 427 list = 0, delete = 0, verify = 0;
dbd0f5c7 428 const char *msgfile = NULL, *keyid = NULL;
bd46c9a9 429 struct msg_arg msg = { 0, STRBUF_INIT };
32c35cfb 430 struct commit_list *with_commit = NULL;
39686585 431 struct option options[] = {
c97eff5a 432 OPT_BOOLEAN('l', "list", &list, "list tag names"),
e3a0ca87
SB
433 { OPTION_INTEGER, 'n', NULL, &lines, "n",
434 "print <n> lines of each tag message",
39686585 435 PARSE_OPT_OPTARG, NULL, 1 },
c97eff5a
MG
436 OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
437 OPT_BOOLEAN('v', "verify", &verify, "verify tags"),
39686585
CR
438
439 OPT_GROUP("Tag creation options"),
c97eff5a 440 OPT_BOOLEAN('a', "annotate", &annotate,
39686585 441 "annotated tag, needs a message"),
c97eff5a 442 OPT_CALLBACK('m', "message", &msg, "message",
3f406175 443 "tag message", parse_msg_arg),
c97eff5a
MG
444 OPT_FILENAME('F', "file", &msgfile, "read message from file"),
445 OPT_BOOLEAN('s', "sign", &sign, "annotated and GPG-signed tag"),
446 OPT_STRING('u', "local-user", &keyid, "key-id",
39686585 447 "use another key to sign the tag"),
76946b76 448 OPT__FORCE(&force, "replace the tag if exists"),
32c35cfb
JG
449
450 OPT_GROUP("Tag listing options"),
451 {
452 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
453 "print only tags that contain the commit",
454 PARSE_OPT_LASTARG_DEFAULT,
455 parse_opt_with_commit, (intptr_t)"HEAD",
456 },
39686585
CR
457 OPT_END()
458 };
459
ef90d6d4 460 git_config(git_tag_config, NULL);
62e09ce9 461
37782920 462 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
62e09ce9 463
be15f505
LT
464 if (keyid) {
465 sign = 1;
466 set_signingkey(keyid);
467 }
c1a41b9d
JH
468 if (sign)
469 annotate = 1;
6fa8342b
ST
470 if (argc == 0 && !(delete || verify))
471 list = 1;
c1a41b9d 472
6fa8342b
ST
473 if ((annotate || msg.given || msgfile || force) &&
474 (list || delete || verify))
475 usage_with_options(git_tag_usage, options);
476
477 if (list + delete + verify > 1)
478 usage_with_options(git_tag_usage, options);
39686585 479 if (list)
588d0e83 480 return list_tags(argv, lines == -1 ? 0 : lines,
32c35cfb 481 with_commit);
6fa8342b 482 if (lines != -1)
d08ebf99 483 die(_("-n option is only allowed with -l."));
32c35cfb 484 if (with_commit)
d08ebf99 485 die(_("--contains option is only allowed with -l."));
39686585
CR
486 if (delete)
487 return for_each_tag_name(argv, delete_tag);
488 if (verify)
489 return for_each_tag_name(argv, verify_tag);
490
bd46c9a9
JH
491 if (msg.given || msgfile) {
492 if (msg.given && msgfile)
d08ebf99 493 die(_("only one -F or -m option is allowed."));
39686585 494 annotate = 1;
bd46c9a9
JH
495 if (msg.given)
496 strbuf_addbuf(&buf, &(msg.buf));
39686585
CR
497 else {
498 if (!strcmp(msgfile, "-")) {
387e7e19 499 if (strbuf_read(&buf, 0, 1024) < 0)
d08ebf99 500 die_errno(_("cannot read '%s'"), msgfile);
387e7e19 501 } else {
39686585 502 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
d08ebf99 503 die_errno(_("could not open or read '%s'"),
d824cbba 504 msgfile);
62e09ce9 505 }
62e09ce9 506 }
62e09ce9
CR
507 }
508
39686585 509 tag = argv[0];
62e09ce9 510
39686585
CR
511 object_ref = argc == 2 ? argv[1] : "HEAD";
512 if (argc > 2)
d08ebf99 513 die(_("too many params"));
62e09ce9
CR
514
515 if (get_sha1(object_ref, object))
d08ebf99 516 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
62e09ce9 517
4f0accd6 518 if (strbuf_check_tag_ref(&ref, tag))
d08ebf99 519 die(_("'%s' is not a valid tag name."), tag);
62e09ce9 520
4f0accd6 521 if (!resolve_ref(ref.buf, prev, 1, NULL))
62e09ce9
CR
522 hashclr(prev);
523 else if (!force)
d08ebf99 524 die(_("tag '%s' already exists"), tag);
62e09ce9
CR
525
526 if (annotate)
bd46c9a9
JH
527 create_tag(object, tag, &buf, msg.given || msgfile,
528 sign, prev, object);
62e09ce9 529
4f0accd6 530 lock = lock_any_ref_for_update(ref.buf, prev, 0);
62e09ce9 531 if (!lock)
4f0accd6 532 die(_("%s: cannot lock the ref"), ref.buf);
62e09ce9 533 if (write_ref_sha1(lock, object, NULL) < 0)
4f0accd6 534 die(_("%s: cannot update the ref"), ref.buf);
0a043b1f 535 if (force && hashcmp(prev, object))
d08ebf99 536 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
62e09ce9 537
fd17f5b5 538 strbuf_release(&buf);
4f0accd6 539 strbuf_release(&ref);
62e09ce9
CR
540 return 0;
541}