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.
13 #include "object-store.h"
15 #include "run-command.h"
16 #include "parse-options.h"
19 #include "gpg-interface.h"
20 #include "sha1-array.h"
22 #include "ref-filter.h"
24 static const char * const git_tag_usage[] = {
25 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"
26 "\t\t<tagname> [<head>]"),
27 N_("git tag -d <tagname>..."),
28 N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]\n"
29 "\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
30 N_("git tag -v [--format=<format>] <tagname>..."),
34 static unsigned int colopts;
35 static int force_sign_annotate;
37 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
38 struct ref_format *format)
40 struct ref_array array;
44 memset(&array, 0, sizeof(array));
46 if (filter->lines == -1)
49 if (!format->format) {
51 to_free = xstrfmt("%s %%(contents:lines=%d)",
52 "%(align:15)%(refname:lstrip=2)%(end)",
54 format->format = to_free;
56 format->format = "%(refname:lstrip=2)";
59 if (verify_ref_format(format))
60 die(_("unable to parse format string"));
61 filter->with_commit_tag_algo = 1;
62 filter_refs(&array, filter, FILTER_REFS_TAGS);
63 ref_array_sort(sorting, &array);
65 for (i = 0; i < array.nr; i++)
66 show_ref_array_item(array.items[i], format);
67 ref_array_clear(&array);
73 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
74 const struct object_id *oid, const void *cb_data);
76 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
80 struct strbuf ref = STRBUF_INIT;
84 for (p = argv; *p; p++) {
86 strbuf_addf(&ref, "refs/tags/%s", *p);
87 if (read_ref(ref.buf, &oid)) {
88 error(_("tag '%s' not found."), *p);
92 if (fn(*p, ref.buf, &oid, cb_data))
99 static int delete_tag(const char *name, const char *ref,
100 const struct object_id *oid, const void *cb_data)
102 if (delete_ref(NULL, ref, oid, 0))
104 printf(_("Deleted tag '%s' (was %s)\n"), name,
105 find_unique_abbrev(oid, DEFAULT_ABBREV));
109 static int verify_tag(const char *name, const char *ref,
110 const struct object_id *oid, const void *cb_data)
113 const struct ref_format *format = cb_data;
114 flags = GPG_VERIFY_VERBOSE;
117 flags = GPG_VERIFY_OMIT_STATUS;
119 if (gpg_verify_tag(oid, name, flags))
123 pretty_print_ref(name, oid, format);
128 static int do_sign(struct strbuf *buffer)
130 return sign_buffer(buffer, buffer, get_signing_key());
133 static const char tag_template[] =
134 N_("\nWrite a message for tag:\n %s\n"
135 "Lines starting with '%c' will be ignored.\n");
137 static const char tag_template_nocleanup[] =
138 N_("\nWrite a message for tag:\n %s\n"
139 "Lines starting with '%c' will be kept; you may remove them"
140 " yourself if you want to.\n");
142 static int git_tag_config(const char *var, const char *value, void *cb)
145 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
147 if (!strcmp(var, "tag.sort")) {
149 return config_error_nonbool(var);
150 parse_ref_sorting(sorting_tail, value);
154 status = git_gpg_config(var, value, cb);
157 if (!strcmp(var, "tag.forcesignannotated")) {
158 force_sign_annotate = git_config_bool(var, value);
162 if (starts_with(var, "column."))
163 return git_column_config(var, value, "tag", &colopts);
164 return git_color_default_config(var, value, cb);
167 static void write_tag_body(int fd, const struct object_id *oid)
170 enum object_type type;
173 buf = read_object_file(oid, &type, &size);
177 sp = strstr(buf, "\n\n");
179 if (!sp || !size || type != OBJ_TAG) {
183 sp += 2; /* skip the 2 LFs */
184 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
189 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
191 if (sign && do_sign(buf) < 0)
192 return error(_("unable to sign the tag"));
193 if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
194 return error(_("unable to write tag file"));
198 struct create_tag_options {
199 unsigned int message_given:1;
200 unsigned int use_editor:1;
209 static const char message_advice_nested_tag[] =
210 N_("You have created a nested tag. The object referred to by your new tag is\n"
211 "already a tag. If you meant to tag the object that it points to, use:\n"
213 "\tgit tag -f %s %s^{}");
215 static void create_tag(const struct object_id *object, const char *object_ref,
217 struct strbuf *buf, struct create_tag_options *opt,
218 struct object_id *prev, struct object_id *result)
220 enum object_type type;
221 struct strbuf header = STRBUF_INIT;
224 type = oid_object_info(the_repository, object, NULL);
225 if (type <= OBJ_NONE)
226 die(_("bad object type."));
228 if (type == OBJ_TAG && advice_nested_tag)
229 advise(_(message_advice_nested_tag), tag, object_ref);
239 git_committer_info(IDENT_STRICT));
241 if (!opt->message_given || opt->use_editor) {
244 /* write the template message before editing: */
245 path = git_pathdup("TAG_EDITMSG");
246 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
248 die_errno(_("could not create file '%s'"), path);
250 if (opt->message_given) {
251 write_or_die(fd, buf->buf, buf->len);
253 } else if (!is_null_oid(prev)) {
254 write_tag_body(fd, prev);
256 struct strbuf buf = STRBUF_INIT;
257 strbuf_addch(&buf, '\n');
258 if (opt->cleanup_mode == CLEANUP_ALL)
259 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
261 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
262 write_or_die(fd, buf.buf, buf.len);
263 strbuf_release(&buf);
267 if (launch_editor(path, buf, NULL)) {
269 _("Please supply the message using either -m or -F option.\n"));
274 if (opt->cleanup_mode != CLEANUP_NONE)
275 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
277 if (!opt->message_given && !buf->len)
278 die(_("no tag message?"));
280 strbuf_insert(buf, 0, header.buf, header.len);
281 strbuf_release(&header);
283 if (build_tag_object(buf, opt->sign, result) < 0) {
285 fprintf(stderr, _("The tag message has been left in %s\n"),
290 unlink_or_warn(path);
295 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
297 enum object_type type;
302 const char *subject_start;
304 char *rla = getenv("GIT_REFLOG_ACTION");
306 strbuf_addstr(sb, rla);
308 strbuf_addstr(sb, "tag: tagging ");
309 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
312 strbuf_addstr(sb, " (");
313 type = oid_object_info(the_repository, oid, NULL);
316 strbuf_addstr(sb, "object of unknown type");
319 if ((buf = read_object_file(oid, &type, &size)) != NULL) {
320 subject_len = find_commit_subject(buf, &subject_start);
321 strbuf_insert(sb, sb->len, subject_start, subject_len);
323 strbuf_addstr(sb, "commit object");
327 if ((c = lookup_commit_reference(the_repository, oid)) != NULL)
328 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
331 strbuf_addstr(sb, "tree object");
334 strbuf_addstr(sb, "blob object");
337 strbuf_addstr(sb, "other tag object");
340 strbuf_addch(sb, ')');
348 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
350 struct msg_arg *msg = opt->value;
352 BUG_ON_OPT_NEG(unset);
357 strbuf_addstr(&(msg->buf), "\n\n");
358 strbuf_addstr(&(msg->buf), arg);
363 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
369 strbuf_addf(sb, "refs/tags/%s", name);
371 return check_refname_format(sb->buf, 0);
374 int cmd_tag(int argc, const char **argv, const char *prefix)
376 struct strbuf buf = STRBUF_INIT;
377 struct strbuf ref = STRBUF_INIT;
378 struct strbuf reflog_msg = STRBUF_INIT;
379 struct object_id object, prev;
380 const char *object_ref, *tag;
381 struct create_tag_options opt;
382 char *cleanup_arg = NULL;
383 int create_reflog = 0;
384 int annotate = 0, force = 0;
385 int cmdmode = 0, create_tag_object = 0;
386 const char *msgfile = NULL, *keyid = NULL;
387 struct msg_arg msg = { 0, STRBUF_INIT };
388 struct ref_transaction *transaction;
389 struct strbuf err = STRBUF_INIT;
390 struct ref_filter filter;
391 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
392 struct ref_format format = REF_FORMAT_INIT;
395 struct option options[] = {
396 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
397 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
398 N_("print <n> lines of each tag message"),
399 PARSE_OPT_OPTARG, NULL, 1 },
400 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
401 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
403 OPT_GROUP(N_("Tag creation options")),
404 OPT_BOOL('a', "annotate", &annotate,
405 N_("annotated tag, needs a message")),
406 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
407 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg },
408 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
409 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
410 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
411 OPT_CLEANUP(&cleanup_arg),
412 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
413 N_("use another key to sign the tag")),
414 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
415 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
417 OPT_GROUP(N_("Tag listing options")),
418 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
419 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
420 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
421 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
422 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
423 OPT_MERGED(&filter, N_("print only tags that are merged")),
424 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
425 OPT_REF_SORT(sorting_tail),
427 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
428 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
429 parse_opt_object_name, (intptr_t) "HEAD"
431 OPT_STRING( 0 , "format", &format.format, N_("format"),
432 N_("format to use for the output")),
433 OPT__COLOR(&format.use_color, N_("respect format colors")),
434 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
438 setup_ref_filter_porcelain_msg();
440 git_config(git_tag_config, sorting_tail);
442 memset(&opt, 0, sizeof(opt));
443 memset(&filter, 0, sizeof(filter));
446 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
450 set_signing_key(keyid);
452 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
457 else if (filter.with_commit || filter.no_commit ||
458 filter.points_at.nr || filter.merge_commit ||
464 setup_auto_pager("tag", 1);
466 if ((create_tag_object || force) && (cmdmode != 0))
467 usage_with_options(git_tag_usage, options);
469 finalize_colopts(&colopts, -1);
470 if (cmdmode == 'l' && filter.lines != -1) {
471 if (explicitly_enable_column(colopts))
472 die(_("--column and -n are incompatible"));
476 sorting = ref_default_sorting();
477 sorting->ignore_case = icase;
478 filter.ignore_case = icase;
479 if (cmdmode == 'l') {
481 if (column_active(colopts)) {
482 struct column_options copts;
483 memset(&copts, 0, sizeof(copts));
485 run_column_filter(colopts, &copts);
487 filter.name_patterns = argv;
488 ret = list_tags(&filter, sorting, &format);
489 if (column_active(colopts))
490 stop_column_filter();
493 if (filter.lines != -1)
494 die(_("-n option is only allowed in list mode"));
495 if (filter.with_commit)
496 die(_("--contains option is only allowed in list mode"));
497 if (filter.no_commit)
498 die(_("--no-contains option is only allowed in list mode"));
499 if (filter.points_at.nr)
500 die(_("--points-at option is only allowed in list mode"));
501 if (filter.merge_commit)
502 die(_("--merged and --no-merged options are only allowed in list mode"));
504 return for_each_tag_name(argv, delete_tag, NULL);
505 if (cmdmode == 'v') {
506 if (format.format && verify_ref_format(&format))
507 usage_with_options(git_tag_usage, options);
508 return for_each_tag_name(argv, verify_tag, &format);
511 if (msg.given || msgfile) {
512 if (msg.given && msgfile)
513 die(_("only one -F or -m option is allowed."));
515 strbuf_addbuf(&buf, &(msg.buf));
517 if (!strcmp(msgfile, "-")) {
518 if (strbuf_read(&buf, 0, 1024) < 0)
519 die_errno(_("cannot read '%s'"), msgfile);
521 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
522 die_errno(_("could not open or read '%s'"),
530 object_ref = argc == 2 ? argv[1] : "HEAD";
532 die(_("too many params"));
534 if (get_oid(object_ref, &object))
535 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
537 if (strbuf_check_tag_ref(&ref, tag))
538 die(_("'%s' is not a valid tag name."), tag);
540 if (read_ref(ref.buf, &prev))
543 die(_("tag '%s' already exists"), tag);
545 opt.message_given = msg.given || msgfile;
546 opt.use_editor = edit_flag;
548 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
549 opt.cleanup_mode = CLEANUP_ALL;
550 else if (!strcmp(cleanup_arg, "verbatim"))
551 opt.cleanup_mode = CLEANUP_NONE;
552 else if (!strcmp(cleanup_arg, "whitespace"))
553 opt.cleanup_mode = CLEANUP_SPACE;
555 die(_("Invalid cleanup mode %s"), cleanup_arg);
557 create_reflog_msg(&object, &reflog_msg);
559 if (create_tag_object) {
560 if (force_sign_annotate && !annotate)
562 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
565 transaction = ref_transaction_begin(&err);
567 ref_transaction_update(transaction, ref.buf, &object, &prev,
568 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
569 reflog_msg.buf, &err) ||
570 ref_transaction_commit(transaction, &err))
572 ref_transaction_free(transaction);
573 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
574 printf(_("Updated tag '%s' (was %s)\n"), tag,
575 find_unique_abbrev(&prev, DEFAULT_ABBREV));