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 "run-command.h"
14 #include "parse-options.h"
17 #include "gpg-interface.h"
18 #include "sha1-array.h"
21 static const char * const git_tag_usage[] = {
22 N_("git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]"),
23 N_("git tag -d <tagname>..."),
24 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] "
25 "\n\t\t[<pattern>...]"),
26 N_("git tag -v <tagname>..."),
30 #define STRCMP_SORT 0 /* must be zero */
32 #define SORT_MASK 0x7fff
33 #define REVERSE_SORT 0x8000
36 const char **patterns;
39 struct string_list tags;
40 struct commit_list *with_commit;
43 static struct sha1_array points_at;
44 static unsigned int colopts;
46 static int match_pattern(const char **patterns, const char *ref)
48 /* no pattern means match everything */
51 for (; *patterns; patterns++)
52 if (!wildmatch(*patterns, ref, 0, NULL))
57 static const unsigned char *match_points_at(const char *refname,
58 const unsigned char *sha1)
60 const unsigned char *tagged_sha1 = NULL;
63 if (sha1_array_lookup(&points_at, sha1) >= 0)
65 obj = parse_object(sha1);
67 die(_("malformed object at '%s'"), refname);
68 if (obj->type == OBJ_TAG)
69 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
70 if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
75 static int in_commit_list(const struct commit_list *want, struct commit *c)
77 for (; want; want = want->next)
78 if (!hashcmp(want->item->object.sha1, c->object.sha1))
83 static int contains_recurse(struct commit *candidate,
84 const struct commit_list *want)
86 struct commit_list *p;
88 /* was it previously marked as containing a want commit? */
89 if (candidate->object.flags & TMP_MARK)
91 /* or marked as not possibly containing a want commit? */
92 if (candidate->object.flags & UNINTERESTING)
95 if (in_commit_list(want, candidate))
98 if (parse_commit(candidate) < 0)
101 /* Otherwise recurse and mark ourselves for future traversals. */
102 for (p = candidate->parents; p; p = p->next) {
103 if (contains_recurse(p->item, want)) {
104 candidate->object.flags |= TMP_MARK;
108 candidate->object.flags |= UNINTERESTING;
112 static int contains(struct commit *candidate, const struct commit_list *want)
114 return contains_recurse(candidate, want);
117 static void show_tag_lines(const unsigned char *sha1, int lines)
121 enum object_type type;
122 char *buf, *sp, *eol;
125 buf = read_sha1_file(sha1, &type, &size);
127 die_errno("unable to read object %s", sha1_to_hex(sha1));
128 if (type != OBJ_COMMIT && type != OBJ_TAG)
131 die("an empty %s object %s?",
132 typename(type), sha1_to_hex(sha1));
135 sp = strstr(buf, "\n\n");
139 /* only take up to "lines" lines, and strip the signature from a tag */
141 size = parse_signature(buf, size);
142 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
145 eol = memchr(sp, '\n', size - (sp - buf));
146 len = eol ? eol - sp : size - (sp - buf);
147 fwrite(sp, len, 1, stdout);
156 static int show_reference(const char *refname, const unsigned char *sha1,
157 int flag, void *cb_data)
159 struct tag_filter *filter = cb_data;
161 if (match_pattern(filter->patterns, refname)) {
162 if (filter->with_commit) {
163 struct commit *commit;
165 commit = lookup_commit_reference_gently(sha1, 1);
168 if (!contains(commit, filter->with_commit))
172 if (points_at.nr && !match_points_at(refname, sha1))
175 if (!filter->lines) {
177 string_list_append(&filter->tags, refname);
179 printf("%s\n", refname);
182 printf("%-15s ", refname);
183 show_tag_lines(sha1, filter->lines);
190 static int sort_by_version(const void *a_, const void *b_)
192 const struct string_list_item *a = a_;
193 const struct string_list_item *b = b_;
194 return versioncmp(a->string, b->string);
197 static int list_tags(const char **patterns, int lines,
198 struct commit_list *with_commit, int sort)
200 struct tag_filter filter;
202 filter.patterns = patterns;
203 filter.lines = lines;
205 filter.with_commit = with_commit;
206 memset(&filter.tags, 0, sizeof(filter.tags));
207 filter.tags.strdup_strings = 1;
209 for_each_tag_ref(show_reference, (void *) &filter);
212 if ((sort & SORT_MASK) == VERCMP_SORT)
213 qsort(filter.tags.items, filter.tags.nr,
214 sizeof(struct string_list_item), sort_by_version);
215 if (sort & REVERSE_SORT)
216 for (i = filter.tags.nr - 1; i >= 0; i--)
217 printf("%s\n", filter.tags.items[i].string);
219 for (i = 0; i < filter.tags.nr; i++)
220 printf("%s\n", filter.tags.items[i].string);
221 string_list_clear(&filter.tags, 0);
226 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
227 const unsigned char *sha1);
229 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
234 unsigned char sha1[20];
236 for (p = argv; *p; p++) {
237 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
239 error(_("tag name too long: %.*s..."), 50, *p);
243 if (read_ref(ref, sha1)) {
244 error(_("tag '%s' not found."), *p);
248 if (fn(*p, ref, sha1))
254 static int delete_tag(const char *name, const char *ref,
255 const unsigned char *sha1)
257 if (delete_ref(ref, sha1, 0))
259 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
263 static int verify_tag(const char *name, const char *ref,
264 const unsigned char *sha1)
266 const char *argv_verify_tag[] = {"verify-tag",
267 "-v", "SHA1_HEX", NULL};
268 argv_verify_tag[2] = sha1_to_hex(sha1);
270 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
271 return error(_("could not verify the tag '%s'"), name);
275 static int do_sign(struct strbuf *buffer)
277 return sign_buffer(buffer, buffer, get_signing_key());
280 static const char tag_template[] =
281 N_("\nWrite a tag message\n"
282 "Lines starting with '%c' will be ignored.\n");
284 static const char tag_template_nocleanup[] =
285 N_("\nWrite a tag message\n"
286 "Lines starting with '%c' will be kept; you may remove them"
287 " yourself if you want to.\n");
289 static int git_tag_config(const char *var, const char *value, void *cb)
291 int status = git_gpg_config(var, value, cb);
294 if (starts_with(var, "column."))
295 return git_column_config(var, value, "tag", &colopts);
296 return git_default_config(var, value, cb);
299 static void write_tag_body(int fd, const unsigned char *sha1)
302 enum object_type type;
305 buf = read_sha1_file(sha1, &type, &size);
309 sp = strstr(buf, "\n\n");
311 if (!sp || !size || type != OBJ_TAG) {
315 sp += 2; /* skip the 2 LFs */
316 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
321 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
323 if (sign && do_sign(buf) < 0)
324 return error(_("unable to sign the tag"));
325 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
326 return error(_("unable to write tag file"));
330 struct create_tag_options {
331 unsigned int message_given:1;
340 static void create_tag(const unsigned char *object, const char *tag,
341 struct strbuf *buf, struct create_tag_options *opt,
342 unsigned char *prev, unsigned char *result)
344 enum object_type type;
345 char header_buf[1024];
349 type = sha1_object_info(object, NULL);
350 if (type <= OBJ_NONE)
351 die(_("bad object type."));
353 header_len = snprintf(header_buf, sizeof(header_buf),
361 git_committer_info(IDENT_STRICT));
363 if (header_len > sizeof(header_buf) - 1)
364 die(_("tag header too big."));
366 if (!opt->message_given) {
369 /* write the template message before editing: */
370 path = git_pathdup("TAG_EDITMSG");
371 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
373 die_errno(_("could not create file '%s'"), path);
375 if (!is_null_sha1(prev)) {
376 write_tag_body(fd, prev);
378 struct strbuf buf = STRBUF_INIT;
379 strbuf_addch(&buf, '\n');
380 if (opt->cleanup_mode == CLEANUP_ALL)
381 strbuf_commented_addf(&buf, _(tag_template), comment_line_char);
383 strbuf_commented_addf(&buf, _(tag_template_nocleanup), comment_line_char);
384 write_or_die(fd, buf.buf, buf.len);
385 strbuf_release(&buf);
389 if (launch_editor(path, buf, NULL)) {
391 _("Please supply the message using either -m or -F option.\n"));
396 if (opt->cleanup_mode != CLEANUP_NONE)
397 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
399 if (!opt->message_given && !buf->len)
400 die(_("no tag message?"));
402 strbuf_insert(buf, 0, header_buf, header_len);
404 if (build_tag_object(buf, opt->sign, result) < 0) {
406 fprintf(stderr, _("The tag message has been left in %s\n"),
411 unlink_or_warn(path);
421 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
423 struct msg_arg *msg = opt->value;
428 strbuf_addstr(&(msg->buf), "\n\n");
429 strbuf_addstr(&(msg->buf), arg);
434 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
440 strbuf_addf(sb, "refs/tags/%s", name);
442 return check_refname_format(sb->buf, 0);
445 static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
446 const char *arg, int unset)
448 unsigned char sha1[20];
451 sha1_array_clear(&points_at);
455 return error(_("switch 'points-at' requires an object"));
456 if (get_sha1(arg, sha1))
457 return error(_("malformed object name '%s'"), arg);
458 sha1_array_append(&points_at, sha1);
462 static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
464 int *sort = opt->value;
468 flags |= REVERSE_SORT;
471 if (starts_with(arg, "version:")) {
474 } else if (starts_with(arg, "v:")) {
479 if (strcmp(arg, "refname"))
480 die(_("unsupported sort specification %s"), arg);
485 int cmd_tag(int argc, const char **argv, const char *prefix)
487 struct strbuf buf = STRBUF_INIT;
488 struct strbuf ref = STRBUF_INIT;
489 unsigned char object[20], prev[20];
490 const char *object_ref, *tag;
491 struct ref_lock *lock;
492 struct create_tag_options opt;
493 char *cleanup_arg = NULL;
494 int annotate = 0, force = 0, lines = -1;
495 int cmdmode = 0, sort = 0;
496 const char *msgfile = NULL, *keyid = NULL;
497 struct msg_arg msg = { 0, STRBUF_INIT };
498 struct commit_list *with_commit = NULL;
499 struct option options[] = {
500 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
501 { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
502 N_("print <n> lines of each tag message"),
503 PARSE_OPT_OPTARG, NULL, 1 },
504 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
505 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
507 OPT_GROUP(N_("Tag creation options")),
508 OPT_BOOL('a', "annotate", &annotate,
509 N_("annotated tag, needs a message")),
510 OPT_CALLBACK('m', "message", &msg, N_("message"),
511 N_("tag message"), parse_msg_arg),
512 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
513 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
514 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
515 N_("how to strip spaces and #comments from message")),
516 OPT_STRING('u', "local-user", &keyid, N_("key id"),
517 N_("use another key to sign the tag")),
518 OPT__FORCE(&force, N_("replace the tag if exists")),
519 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
521 OPTION_CALLBACK, 0, "sort", &sort, N_("type"), N_("sort tags"),
522 PARSE_OPT_NONEG, parse_opt_sort
525 OPT_GROUP(N_("Tag listing options")),
527 OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"),
528 N_("print only tags that contain the commit"),
529 PARSE_OPT_LASTARG_DEFAULT,
530 parse_opt_with_commit, (intptr_t)"HEAD",
533 OPTION_CALLBACK, 0, "with", &with_commit, N_("commit"),
534 N_("print only tags that contain the commit"),
535 PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
536 parse_opt_with_commit, (intptr_t)"HEAD",
539 OPTION_CALLBACK, 0, "points-at", NULL, N_("object"),
540 N_("print only tags of the object"), 0, parse_opt_points_at
545 git_config(git_tag_config, NULL);
547 memset(&opt, 0, sizeof(opt));
549 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
553 set_signing_key(keyid);
557 if (argc == 0 && !cmdmode)
560 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
561 usage_with_options(git_tag_usage, options);
563 finalize_colopts(&colopts, -1);
564 if (cmdmode == 'l' && lines != -1) {
565 if (explicitly_enable_column(colopts))
566 die(_("--column and -n are incompatible"));
569 if (cmdmode == 'l') {
571 if (column_active(colopts)) {
572 struct column_options copts;
573 memset(&copts, 0, sizeof(copts));
575 run_column_filter(colopts, &copts);
577 if (lines != -1 && sort)
578 die(_("--sort and -n are incompatible"));
579 ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, sort);
580 if (column_active(colopts))
581 stop_column_filter();
585 die(_("-n option is only allowed with -l."));
587 die(_("--contains option is only allowed with -l."));
589 die(_("--points-at option is only allowed with -l."));
591 return for_each_tag_name(argv, delete_tag);
593 return for_each_tag_name(argv, verify_tag);
595 if (msg.given || msgfile) {
596 if (msg.given && msgfile)
597 die(_("only one -F or -m option is allowed."));
600 strbuf_addbuf(&buf, &(msg.buf));
602 if (!strcmp(msgfile, "-")) {
603 if (strbuf_read(&buf, 0, 1024) < 0)
604 die_errno(_("cannot read '%s'"), msgfile);
606 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
607 die_errno(_("could not open or read '%s'"),
615 object_ref = argc == 2 ? argv[1] : "HEAD";
617 die(_("too many params"));
619 if (get_sha1(object_ref, object))
620 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
622 if (strbuf_check_tag_ref(&ref, tag))
623 die(_("'%s' is not a valid tag name."), tag);
625 if (read_ref(ref.buf, prev))
628 die(_("tag '%s' already exists"), tag);
630 opt.message_given = msg.given || msgfile;
632 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
633 opt.cleanup_mode = CLEANUP_ALL;
634 else if (!strcmp(cleanup_arg, "verbatim"))
635 opt.cleanup_mode = CLEANUP_NONE;
636 else if (!strcmp(cleanup_arg, "whitespace"))
637 opt.cleanup_mode = CLEANUP_SPACE;
639 die(_("Invalid cleanup mode %s"), cleanup_arg);
642 create_tag(object, tag, &buf, &opt, prev, object);
644 lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);
646 die(_("%s: cannot lock the ref"), ref.buf);
647 if (write_ref_sha1(lock, object, NULL) < 0)
648 die(_("%s: cannot update the ref"), ref.buf);
649 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
650 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
652 strbuf_release(&buf);
653 strbuf_release(&ref);