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"
20 #include "ref-filter.h"
22 static const char * const git_tag_usage[] = {
23 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
24 N_("git tag -d <tagname>..."),
25 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
26 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
27 N_("git tag -v [--format=<format>] <tagname>..."),
31 static unsigned int colopts;
32 static int force_sign_annotate;
34 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
36 struct ref_array array;
40 memset(&array, 0, sizeof(array));
42 if (filter->lines == -1)
47 to_free = xstrfmt("%s %%(contents:lines=%d)",
48 "%(align:15)%(refname:lstrip=2)%(end)",
52 format = "%(refname:lstrip=2)";
55 verify_ref_format(format);
56 filter->with_commit_tag_algo = 1;
57 filter_refs(&array, filter, FILTER_REFS_TAGS);
58 ref_array_sort(sorting, &array);
60 for (i = 0; i < array.nr; i++)
61 show_ref_array_item(array.items[i], format, 0);
62 ref_array_clear(&array);
68 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
69 const unsigned char *sha1, const void *cb_data);
71 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
77 unsigned char sha1[20];
79 for (p = argv; *p; p++) {
80 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
82 error(_("tag name too long: %.*s..."), 50, *p);
86 if (read_ref(ref, sha1)) {
87 error(_("tag '%s' not found."), *p);
91 if (fn(*p, ref, sha1, cb_data))
97 static int delete_tag(const char *name, const char *ref,
98 const unsigned char *sha1, const void *cb_data)
100 if (delete_ref(NULL, ref, sha1, 0))
102 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
106 static int verify_tag(const char *name, const char *ref,
107 const unsigned char *sha1, const void *cb_data)
110 const char *fmt_pretty = cb_data;
111 flags = GPG_VERIFY_VERBOSE;
114 flags = GPG_VERIFY_OMIT_STATUS;
116 if (gpg_verify_tag(sha1, name, flags))
120 pretty_print_ref(name, sha1, fmt_pretty);
125 static int do_sign(struct strbuf *buffer)
127 return sign_buffer(buffer, buffer, get_signing_key());
130 static const char tag_template[] =
131 N_("\nWrite a message for tag:\n %s\n"
132 "Lines starting with '%c' will be ignored.\n");
134 static const char tag_template_nocleanup[] =
135 N_("\nWrite a message for tag:\n %s\n"
136 "Lines starting with '%c' will be kept; you may remove them"
137 " yourself if you want to.\n");
139 /* Parse arg given and add it the ref_sorting array */
140 static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
142 struct ref_sorting *s;
145 s = xcalloc(1, sizeof(*s));
146 s->next = *sorting_tail;
153 if (skip_prefix(arg, "version:", &arg) ||
154 skip_prefix(arg, "v:", &arg))
158 s->atom = parse_ref_filter_atom(arg, arg+len);
163 static int git_tag_config(const char *var, const char *value, void *cb)
166 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
168 if (!strcmp(var, "tag.sort")) {
170 return config_error_nonbool(var);
171 parse_sorting_string(value, sorting_tail);
175 status = git_gpg_config(var, value, cb);
178 if (!strcmp(var, "tag.forcesignannotated")) {
179 force_sign_annotate = git_config_bool(var, value);
183 if (starts_with(var, "column."))
184 return git_column_config(var, value, "tag", &colopts);
185 return git_default_config(var, value, cb);
188 static void write_tag_body(int fd, const unsigned char *sha1)
191 enum object_type type;
194 buf = read_sha1_file(sha1, &type, &size);
198 sp = strstr(buf, "\n\n");
200 if (!sp || !size || type != OBJ_TAG) {
204 sp += 2; /* skip the 2 LFs */
205 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
210 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
212 if (sign && do_sign(buf) < 0)
213 return error(_("unable to sign the tag"));
214 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
215 return error(_("unable to write tag file"));
219 struct create_tag_options {
220 unsigned int message_given:1;
229 static void create_tag(const unsigned char *object, const char *tag,
230 struct strbuf *buf, struct create_tag_options *opt,
231 unsigned char *prev, unsigned char *result)
233 enum object_type type;
234 char header_buf[1024];
238 type = sha1_object_info(object, NULL);
239 if (type <= OBJ_NONE)
240 die(_("bad object type."));
242 header_len = snprintf(header_buf, sizeof(header_buf),
250 git_committer_info(IDENT_STRICT));
252 if (header_len > sizeof(header_buf) - 1)
253 die(_("tag header too big."));
255 if (!opt->message_given) {
258 /* write the template message before editing: */
259 path = git_pathdup("TAG_EDITMSG");
260 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
262 die_errno(_("could not create file '%s'"), path);
264 if (!is_null_sha1(prev)) {
265 write_tag_body(fd, prev);
267 struct strbuf buf = STRBUF_INIT;
268 strbuf_addch(&buf, '\n');
269 if (opt->cleanup_mode == CLEANUP_ALL)
270 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
272 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
273 write_or_die(fd, buf.buf, buf.len);
274 strbuf_release(&buf);
278 if (launch_editor(path, buf, NULL)) {
280 _("Please supply the message using either -m or -F option.\n"));
285 if (opt->cleanup_mode != CLEANUP_NONE)
286 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
288 if (!opt->message_given && !buf->len)
289 die(_("no tag message?"));
291 strbuf_insert(buf, 0, header_buf, header_len);
293 if (build_tag_object(buf, opt->sign, result) < 0) {
295 fprintf(stderr, _("The tag message has been left in %s\n"),
300 unlink_or_warn(path);
305 static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb)
307 enum object_type type;
312 const char *subject_start;
314 char *rla = getenv("GIT_REFLOG_ACTION");
316 strbuf_addstr(sb, rla);
318 strbuf_addstr(sb, _("tag: tagging "));
319 strbuf_add_unique_abbrev(sb, sha1, DEFAULT_ABBREV);
322 strbuf_addstr(sb, " (");
323 type = sha1_object_info(sha1, NULL);
326 strbuf_addstr(sb, _("object of unknown type"));
329 if ((buf = read_sha1_file(sha1, &type, &size)) != NULL) {
330 subject_len = find_commit_subject(buf, &subject_start);
331 strbuf_insert(sb, sb->len, subject_start, subject_len);
333 strbuf_addstr(sb, _("commit object"));
337 if ((c = lookup_commit_reference(sha1)) != NULL)
338 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
341 strbuf_addstr(sb, _("tree object"));
344 strbuf_addstr(sb, _("blob object"));
347 strbuf_addstr(sb, _("other tag object"));
350 strbuf_addch(sb, ')');
358 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
360 struct msg_arg *msg = opt->value;
365 strbuf_addstr(&(msg->buf), "\n\n");
366 strbuf_addstr(&(msg->buf), arg);
371 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
377 strbuf_addf(sb, "refs/tags/%s", name);
379 return check_refname_format(sb->buf, 0);
382 int cmd_tag(int argc, const char **argv, const char *prefix)
384 struct strbuf buf = STRBUF_INIT;
385 struct strbuf ref = STRBUF_INIT;
386 struct strbuf reflog_msg = STRBUF_INIT;
387 unsigned char object[20], prev[20];
388 const char *object_ref, *tag;
389 struct create_tag_options opt;
390 char *cleanup_arg = NULL;
391 int create_reflog = 0;
392 int annotate = 0, force = 0;
393 int cmdmode = 0, create_tag_object = 0;
394 const char *msgfile = NULL, *keyid = NULL;
395 struct msg_arg msg = { 0, STRBUF_INIT };
396 struct ref_transaction *transaction;
397 struct strbuf err = STRBUF_INIT;
398 struct ref_filter filter;
399 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
400 const char *format = NULL;
402 struct option options[] = {
403 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
404 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
405 N_("print <n> lines of each tag message"),
406 PARSE_OPT_OPTARG, NULL, 1 },
407 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
408 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
410 OPT_GROUP(N_("Tag creation options")),
411 OPT_BOOL('a', "annotate", &annotate,
412 N_("annotated tag, needs a message")),
413 OPT_CALLBACK('m', "message", &msg, N_("message"),
414 N_("tag message"), parse_msg_arg),
415 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
416 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
417 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
418 N_("how to strip spaces and #comments from message")),
419 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
420 N_("use another key to sign the tag")),
421 OPT__FORCE(&force, N_("replace the tag if exists")),
422 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
424 OPT_GROUP(N_("Tag listing options")),
425 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
426 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
427 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
428 OPT_MERGED(&filter, N_("print only tags that are merged")),
429 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
430 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
431 N_("field name to sort on"), &parse_opt_ref_sorting),
433 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
434 N_("print only tags of the object"), 0, parse_opt_object_name
436 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
437 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
441 setup_ref_filter_porcelain_msg();
443 git_config(git_tag_config, sorting_tail);
445 memset(&opt, 0, sizeof(opt));
446 memset(&filter, 0, sizeof(filter));
449 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
453 set_signing_key(keyid);
455 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
457 if (argc == 0 && !cmdmode)
460 if ((create_tag_object || force) && (cmdmode != 0))
461 usage_with_options(git_tag_usage, options);
463 finalize_colopts(&colopts, -1);
464 if (cmdmode == 'l' && filter.lines != -1) {
465 if (explicitly_enable_column(colopts))
466 die(_("--column and -n are incompatible"));
470 sorting = ref_default_sorting();
471 sorting->ignore_case = icase;
472 filter.ignore_case = icase;
473 if (cmdmode == 'l') {
475 if (column_active(colopts)) {
476 struct column_options copts;
477 memset(&copts, 0, sizeof(copts));
479 run_column_filter(colopts, &copts);
481 filter.name_patterns = argv;
482 ret = list_tags(&filter, sorting, format);
483 if (column_active(colopts))
484 stop_column_filter();
487 if (filter.lines != -1)
488 die(_("-n option is only allowed with -l."));
489 if (filter.with_commit)
490 die(_("--contains option is only allowed with -l."));
491 if (filter.points_at.nr)
492 die(_("--points-at option is only allowed with -l."));
493 if (filter.merge_commit)
494 die(_("--merged and --no-merged option are only allowed with -l"));
496 return for_each_tag_name(argv, delete_tag, NULL);
497 if (cmdmode == 'v') {
499 verify_ref_format(format);
500 return for_each_tag_name(argv, verify_tag, format);
503 if (msg.given || msgfile) {
504 if (msg.given && msgfile)
505 die(_("only one -F or -m option is allowed."));
507 strbuf_addbuf(&buf, &(msg.buf));
509 if (!strcmp(msgfile, "-")) {
510 if (strbuf_read(&buf, 0, 1024) < 0)
511 die_errno(_("cannot read '%s'"), msgfile);
513 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
514 die_errno(_("could not open or read '%s'"),
522 object_ref = argc == 2 ? argv[1] : "HEAD";
524 die(_("too many params"));
526 if (get_sha1(object_ref, object))
527 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
529 if (strbuf_check_tag_ref(&ref, tag))
530 die(_("'%s' is not a valid tag name."), tag);
532 if (read_ref(ref.buf, prev))
535 die(_("tag '%s' already exists"), tag);
537 opt.message_given = msg.given || msgfile;
539 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
540 opt.cleanup_mode = CLEANUP_ALL;
541 else if (!strcmp(cleanup_arg, "verbatim"))
542 opt.cleanup_mode = CLEANUP_NONE;
543 else if (!strcmp(cleanup_arg, "whitespace"))
544 opt.cleanup_mode = CLEANUP_SPACE;
546 die(_("Invalid cleanup mode %s"), cleanup_arg);
548 create_reflog_msg(object, &reflog_msg);
550 if (create_tag_object) {
551 if (force_sign_annotate && !annotate)
553 create_tag(object, tag, &buf, &opt, prev, object);
556 transaction = ref_transaction_begin(&err);
558 ref_transaction_update(transaction, ref.buf, object, prev,
559 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
560 reflog_msg.buf, &err) ||
561 ref_transaction_commit(transaction, &err))
563 ref_transaction_free(transaction);
564 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
565 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
567 strbuf_release(&err);
568 strbuf_release(&buf);
569 strbuf_release(&ref);
570 strbuf_release(&reflog_msg);