git-bisect-lk2009: update nist report link
[git/debian.git] / builtin / tag.c
blobd428c45dc8d812255336ed5da20804981d3be4ab
1 /*
2 * Builtin "git tag"
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 */
9 #include "cache.h"
10 #include "config.h"
11 #include "builtin.h"
12 #include "refs.h"
13 #include "object-store.h"
14 #include "tag.h"
15 #include "run-command.h"
16 #include "parse-options.h"
17 #include "diff.h"
18 #include "revision.h"
19 #include "gpg-interface.h"
20 #include "oid-array.h"
21 #include "column.h"
22 #include "ref-filter.h"
23 #include "date.h"
25 static const char * const git_tag_usage[] = {
26 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n"
27 " <tagname> [<commit> | <object>]"),
28 N_("git tag -d <tagname>..."),
29 N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n"
30 " [--points-at <object>] [--column[=<options>] | --no-column]\n"
31 " [--create-reflog] [--sort=<key>] [--format=<format>]\n"
32 " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
33 N_("git tag -v [--format=<format>] <tagname>..."),
34 NULL
37 static unsigned int colopts;
38 static int force_sign_annotate;
39 static int config_sign_tag = -1; /* unspecified */
41 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
42 struct ref_format *format)
44 struct ref_array array;
45 struct strbuf output = STRBUF_INIT;
46 struct strbuf err = STRBUF_INIT;
47 char *to_free = NULL;
48 int i;
50 memset(&array, 0, sizeof(array));
52 if (filter->lines == -1)
53 filter->lines = 0;
55 if (!format->format) {
56 if (filter->lines) {
57 to_free = xstrfmt("%s %%(contents:lines=%d)",
58 "%(align:15)%(refname:lstrip=2)%(end)",
59 filter->lines);
60 format->format = to_free;
61 } else
62 format->format = "%(refname:lstrip=2)";
65 if (verify_ref_format(format))
66 die(_("unable to parse format string"));
67 filter->with_commit_tag_algo = 1;
68 filter_refs(&array, filter, FILTER_REFS_TAGS);
69 ref_array_sort(sorting, &array);
71 for (i = 0; i < array.nr; i++) {
72 strbuf_reset(&output);
73 strbuf_reset(&err);
74 if (format_ref_array_item(array.items[i], format, &output, &err))
75 die("%s", err.buf);
76 fwrite(output.buf, 1, output.len, stdout);
77 putchar('\n');
80 strbuf_release(&err);
81 strbuf_release(&output);
82 ref_array_clear(&array);
83 free(to_free);
85 return 0;
88 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
89 const struct object_id *oid, void *cb_data);
91 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
92 void *cb_data)
94 const char **p;
95 struct strbuf ref = STRBUF_INIT;
96 int had_error = 0;
97 struct object_id oid;
99 for (p = argv; *p; p++) {
100 strbuf_reset(&ref);
101 strbuf_addf(&ref, "refs/tags/%s", *p);
102 if (read_ref(ref.buf, &oid)) {
103 error(_("tag '%s' not found."), *p);
104 had_error = 1;
105 continue;
107 if (fn(*p, ref.buf, &oid, cb_data))
108 had_error = 1;
110 strbuf_release(&ref);
111 return had_error;
114 static int collect_tags(const char *name, const char *ref,
115 const struct object_id *oid, void *cb_data)
117 struct string_list *ref_list = cb_data;
119 string_list_append(ref_list, ref);
120 ref_list->items[ref_list->nr - 1].util = oiddup(oid);
121 return 0;
124 static int delete_tags(const char **argv)
126 int result;
127 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
128 struct string_list_item *item;
130 result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete);
131 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
132 result = 1;
134 for_each_string_list_item(item, &refs_to_delete) {
135 const char *name = item->string;
136 struct object_id *oid = item->util;
137 if (!ref_exists(name))
138 printf(_("Deleted tag '%s' (was %s)\n"),
139 item->string + 10,
140 find_unique_abbrev(oid, DEFAULT_ABBREV));
142 free(oid);
144 string_list_clear(&refs_to_delete, 0);
145 return result;
148 static int verify_tag(const char *name, const char *ref,
149 const struct object_id *oid, void *cb_data)
151 int flags;
152 struct ref_format *format = cb_data;
153 flags = GPG_VERIFY_VERBOSE;
155 if (format->format)
156 flags = GPG_VERIFY_OMIT_STATUS;
158 if (gpg_verify_tag(oid, name, flags))
159 return -1;
161 if (format->format)
162 pretty_print_ref(name, oid, format);
164 return 0;
167 static int do_sign(struct strbuf *buffer)
169 return sign_buffer(buffer, buffer, get_signing_key());
172 static const char tag_template[] =
173 N_("\nWrite a message for tag:\n %s\n"
174 "Lines starting with '%c' will be ignored.\n");
176 static const char tag_template_nocleanup[] =
177 N_("\nWrite a message for tag:\n %s\n"
178 "Lines starting with '%c' will be kept; you may remove them"
179 " yourself if you want to.\n");
181 static int git_tag_config(const char *var, const char *value, void *cb)
183 int status;
185 if (!strcmp(var, "tag.gpgsign")) {
186 config_sign_tag = git_config_bool(var, value);
187 return 0;
190 if (!strcmp(var, "tag.sort")) {
191 if (!value)
192 return config_error_nonbool(var);
193 string_list_append(cb, value);
194 return 0;
197 status = git_gpg_config(var, value, cb);
198 if (status)
199 return status;
200 if (!strcmp(var, "tag.forcesignannotated")) {
201 force_sign_annotate = git_config_bool(var, value);
202 return 0;
205 if (starts_with(var, "column."))
206 return git_column_config(var, value, "tag", &colopts);
207 return git_color_default_config(var, value, cb);
210 static void write_tag_body(int fd, const struct object_id *oid)
212 unsigned long size;
213 enum object_type type;
214 char *buf, *sp, *orig;
215 struct strbuf payload = STRBUF_INIT;
216 struct strbuf signature = STRBUF_INIT;
218 orig = buf = read_object_file(oid, &type, &size);
219 if (!buf)
220 return;
221 if (parse_signature(buf, size, &payload, &signature)) {
222 buf = payload.buf;
223 size = payload.len;
225 /* skip header */
226 sp = strstr(buf, "\n\n");
228 if (!sp || !size || type != OBJ_TAG) {
229 free(buf);
230 return;
232 sp += 2; /* skip the 2 LFs */
233 write_or_die(fd, sp, buf + size - sp);
235 free(orig);
236 strbuf_release(&payload);
237 strbuf_release(&signature);
240 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
242 if (sign && do_sign(buf) < 0)
243 return error(_("unable to sign the tag"));
244 if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0)
245 return error(_("unable to write tag file"));
246 return 0;
249 struct create_tag_options {
250 unsigned int message_given:1;
251 unsigned int use_editor:1;
252 unsigned int sign;
253 enum {
254 CLEANUP_NONE,
255 CLEANUP_SPACE,
256 CLEANUP_ALL
257 } cleanup_mode;
260 static const char message_advice_nested_tag[] =
261 N_("You have created a nested tag. The object referred to by your new tag is\n"
262 "already a tag. If you meant to tag the object that it points to, use:\n"
263 "\n"
264 "\tgit tag -f %s %s^{}");
266 static void create_tag(const struct object_id *object, const char *object_ref,
267 const char *tag,
268 struct strbuf *buf, struct create_tag_options *opt,
269 struct object_id *prev, struct object_id *result)
271 enum object_type type;
272 struct strbuf header = STRBUF_INIT;
273 char *path = NULL;
275 type = oid_object_info(the_repository, object, NULL);
276 if (type <= OBJ_NONE)
277 die(_("bad object type."));
279 if (type == OBJ_TAG)
280 advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag),
281 tag, object_ref);
283 strbuf_addf(&header,
284 "object %s\n"
285 "type %s\n"
286 "tag %s\n"
287 "tagger %s\n\n",
288 oid_to_hex(object),
289 type_name(type),
290 tag,
291 git_committer_info(IDENT_STRICT));
293 if (!opt->message_given || opt->use_editor) {
294 int fd;
296 /* write the template message before editing: */
297 path = git_pathdup("TAG_EDITMSG");
298 fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
300 if (opt->message_given) {
301 write_or_die(fd, buf->buf, buf->len);
302 strbuf_reset(buf);
303 } else if (!is_null_oid(prev)) {
304 write_tag_body(fd, prev);
305 } else {
306 struct strbuf buf = STRBUF_INIT;
307 strbuf_addch(&buf, '\n');
308 if (opt->cleanup_mode == CLEANUP_ALL)
309 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
310 else
311 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
312 write_or_die(fd, buf.buf, buf.len);
313 strbuf_release(&buf);
315 close(fd);
317 if (launch_editor(path, buf, NULL)) {
318 fprintf(stderr,
319 _("Please supply the message using either -m or -F option.\n"));
320 exit(1);
324 if (opt->cleanup_mode != CLEANUP_NONE)
325 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
327 if (!opt->message_given && !buf->len)
328 die(_("no tag message?"));
330 strbuf_insert(buf, 0, header.buf, header.len);
331 strbuf_release(&header);
333 if (build_tag_object(buf, opt->sign, result) < 0) {
334 if (path)
335 fprintf(stderr, _("The tag message has been left in %s\n"),
336 path);
337 exit(128);
339 if (path) {
340 unlink_or_warn(path);
341 free(path);
345 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
347 enum object_type type;
348 struct commit *c;
349 char *buf;
350 unsigned long size;
351 int subject_len = 0;
352 const char *subject_start;
354 char *rla = getenv("GIT_REFLOG_ACTION");
355 if (rla) {
356 strbuf_addstr(sb, rla);
357 } else {
358 strbuf_addstr(sb, "tag: tagging ");
359 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
362 strbuf_addstr(sb, " (");
363 type = oid_object_info(the_repository, oid, NULL);
364 switch (type) {
365 default:
366 strbuf_addstr(sb, "object of unknown type");
367 break;
368 case OBJ_COMMIT:
369 if ((buf = read_object_file(oid, &type, &size))) {
370 subject_len = find_commit_subject(buf, &subject_start);
371 strbuf_insert(sb, sb->len, subject_start, subject_len);
372 } else {
373 strbuf_addstr(sb, "commit object");
375 free(buf);
377 if ((c = lookup_commit_reference(the_repository, oid)))
378 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
379 break;
380 case OBJ_TREE:
381 strbuf_addstr(sb, "tree object");
382 break;
383 case OBJ_BLOB:
384 strbuf_addstr(sb, "blob object");
385 break;
386 case OBJ_TAG:
387 strbuf_addstr(sb, "other tag object");
388 break;
390 strbuf_addch(sb, ')');
393 struct msg_arg {
394 int given;
395 struct strbuf buf;
398 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
400 struct msg_arg *msg = opt->value;
402 BUG_ON_OPT_NEG(unset);
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;
413 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
415 if (name[0] == '-')
416 return -1;
418 strbuf_reset(sb);
419 strbuf_addf(sb, "refs/tags/%s", name);
421 return check_refname_format(sb->buf, 0);
424 int cmd_tag(int argc, const char **argv, const char *prefix)
426 struct strbuf buf = STRBUF_INIT;
427 struct strbuf ref = STRBUF_INIT;
428 struct strbuf reflog_msg = STRBUF_INIT;
429 struct object_id object, prev;
430 const char *object_ref, *tag;
431 struct create_tag_options opt;
432 char *cleanup_arg = NULL;
433 int create_reflog = 0;
434 int annotate = 0, force = 0;
435 int cmdmode = 0, create_tag_object = 0;
436 const char *msgfile = NULL, *keyid = NULL;
437 struct msg_arg msg = { .buf = STRBUF_INIT };
438 struct ref_transaction *transaction;
439 struct strbuf err = STRBUF_INIT;
440 struct ref_filter filter;
441 struct ref_sorting *sorting;
442 struct string_list sorting_options = STRING_LIST_INIT_DUP;
443 struct ref_format format = REF_FORMAT_INIT;
444 int icase = 0;
445 int edit_flag = 0;
446 struct option options[] = {
447 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
448 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
449 N_("print <n> lines of each tag message"),
450 PARSE_OPT_OPTARG, NULL, 1 },
451 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
452 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
454 OPT_GROUP(N_("Tag creation options")),
455 OPT_BOOL('a', "annotate", &annotate,
456 N_("annotated tag, needs a message")),
457 OPT_CALLBACK_F('m', "message", &msg, N_("message"),
458 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg),
459 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
460 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
461 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
462 OPT_CLEANUP(&cleanup_arg),
463 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
464 N_("use another key to sign the tag")),
465 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
466 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
468 OPT_GROUP(N_("Tag listing options")),
469 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
470 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
471 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
472 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
473 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
474 OPT_MERGED(&filter, N_("print only tags that are merged")),
475 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
476 OPT_REF_SORT(&sorting_options),
478 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
479 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
480 parse_opt_object_name, (intptr_t) "HEAD"
482 OPT_STRING( 0 , "format", &format.format, N_("format"),
483 N_("format to use for the output")),
484 OPT__COLOR(&format.use_color, N_("respect format colors")),
485 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
486 OPT_END()
488 int ret = 0;
489 const char *only_in_list = NULL;
491 setup_ref_filter_porcelain_msg();
493 git_config(git_tag_config, &sorting_options);
495 memset(&opt, 0, sizeof(opt));
496 memset(&filter, 0, sizeof(filter));
497 filter.lines = -1;
498 opt.sign = -1;
500 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
502 if (!cmdmode) {
503 if (argc == 0)
504 cmdmode = 'l';
505 else if (filter.with_commit || filter.no_commit ||
506 filter.reachable_from || filter.unreachable_from ||
507 filter.points_at.nr || filter.lines != -1)
508 cmdmode = 'l';
511 if (cmdmode == 'l')
512 setup_auto_pager("tag", 1);
514 if (opt.sign == -1)
515 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
517 if (keyid) {
518 opt.sign = 1;
519 set_signing_key(keyid);
521 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
523 if ((create_tag_object || force) && (cmdmode != 0))
524 usage_with_options(git_tag_usage, options);
526 finalize_colopts(&colopts, -1);
527 if (cmdmode == 'l' && filter.lines != -1) {
528 if (explicitly_enable_column(colopts))
529 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
530 colopts = 0;
532 sorting = ref_sorting_options(&sorting_options);
533 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
534 filter.ignore_case = icase;
535 if (cmdmode == 'l') {
536 if (column_active(colopts)) {
537 struct column_options copts;
538 memset(&copts, 0, sizeof(copts));
539 copts.padding = 2;
540 run_column_filter(colopts, &copts);
542 filter.name_patterns = argv;
543 ret = list_tags(&filter, sorting, &format);
544 if (column_active(colopts))
545 stop_column_filter();
546 goto cleanup;
548 if (filter.lines != -1)
549 only_in_list = "-n";
550 else if (filter.with_commit)
551 only_in_list = "--contains";
552 else if (filter.no_commit)
553 only_in_list = "--no-contains";
554 else if (filter.points_at.nr)
555 only_in_list = "--points-at";
556 else if (filter.reachable_from)
557 only_in_list = "--merged";
558 else if (filter.unreachable_from)
559 only_in_list = "--no-merged";
560 if (only_in_list)
561 die(_("the '%s' option is only allowed in list mode"), only_in_list);
562 if (cmdmode == 'd') {
563 ret = delete_tags(argv);
564 goto cleanup;
566 if (cmdmode == 'v') {
567 if (format.format && verify_ref_format(&format))
568 usage_with_options(git_tag_usage, options);
569 ret = for_each_tag_name(argv, verify_tag, &format);
570 goto cleanup;
573 if (msg.given || msgfile) {
574 if (msg.given && msgfile)
575 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
576 if (msg.given)
577 strbuf_addbuf(&buf, &(msg.buf));
578 else {
579 if (!strcmp(msgfile, "-")) {
580 if (strbuf_read(&buf, 0, 1024) < 0)
581 die_errno(_("cannot read '%s'"), msgfile);
582 } else {
583 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
584 die_errno(_("could not open or read '%s'"),
585 msgfile);
590 tag = argv[0];
592 object_ref = argc == 2 ? argv[1] : "HEAD";
593 if (argc > 2)
594 die(_("too many arguments"));
596 if (get_oid(object_ref, &object))
597 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
599 if (strbuf_check_tag_ref(&ref, tag))
600 die(_("'%s' is not a valid tag name."), tag);
602 if (read_ref(ref.buf, &prev))
603 oidclr(&prev);
604 else if (!force)
605 die(_("tag '%s' already exists"), tag);
607 opt.message_given = msg.given || msgfile;
608 opt.use_editor = edit_flag;
610 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
611 opt.cleanup_mode = CLEANUP_ALL;
612 else if (!strcmp(cleanup_arg, "verbatim"))
613 opt.cleanup_mode = CLEANUP_NONE;
614 else if (!strcmp(cleanup_arg, "whitespace"))
615 opt.cleanup_mode = CLEANUP_SPACE;
616 else
617 die(_("Invalid cleanup mode %s"), cleanup_arg);
619 create_reflog_msg(&object, &reflog_msg);
621 if (create_tag_object) {
622 if (force_sign_annotate && !annotate)
623 opt.sign = 1;
624 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
627 transaction = ref_transaction_begin(&err);
628 if (!transaction ||
629 ref_transaction_update(transaction, ref.buf, &object, &prev,
630 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
631 reflog_msg.buf, &err) ||
632 ref_transaction_commit(transaction, &err))
633 die("%s", err.buf);
634 ref_transaction_free(transaction);
635 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
636 printf(_("Updated tag '%s' (was %s)\n"), tag,
637 find_unique_abbrev(&prev, DEFAULT_ABBREV));
639 cleanup:
640 ref_sorting_release(sorting);
641 strbuf_release(&buf);
642 strbuf_release(&ref);
643 strbuf_release(&reflog_msg);
644 strbuf_release(&msg.buf);
645 strbuf_release(&err);
646 return ret;