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