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 <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:strip=2)%(end)",
52 format = "%(refname:strip=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);
71 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
76 unsigned char sha1[20];
78 for (p = argv; *p; p++) {
79 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
81 error(_("tag name too long: %.*s..."), 50, *p);
85 if (read_ref(ref, sha1)) {
86 error(_("tag '%s' not found."), *p);
90 if (fn(*p, ref, sha1))
96 static int delete_tag(const char *name, const char *ref,
97 const unsigned char *sha1)
99 if (delete_ref(ref, sha1, 0))
101 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
105 static int verify_tag(const char *name, const char *ref,
106 const unsigned char *sha1)
108 return gpg_verify_tag(sha1, name, GPG_VERIFY_VERBOSE);
111 static int do_sign(struct strbuf *buffer)
113 return sign_buffer(buffer, buffer, get_signing_key());
116 static const char tag_template[] =
117 N_("\nWrite a message for tag:\n %s\n"
118 "Lines starting with '%c' will be ignored.\n");
120 static const char tag_template_nocleanup[] =
121 N_("\nWrite a message for tag:\n %s\n"
122 "Lines starting with '%c' will be kept; you may remove them"
123 " yourself if you want to.\n");
125 /* Parse arg given and add it the ref_sorting array */
126 static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
128 struct ref_sorting *s;
131 s = xcalloc(1, sizeof(*s));
132 s->next = *sorting_tail;
139 if (skip_prefix(arg, "version:", &arg) ||
140 skip_prefix(arg, "v:", &arg))
144 s->atom = parse_ref_filter_atom(arg, arg+len);
149 static int git_tag_config(const char *var, const char *value, void *cb)
152 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
154 if (!strcmp(var, "tag.sort")) {
156 return config_error_nonbool(var);
157 parse_sorting_string(value, sorting_tail);
161 status = git_gpg_config(var, value, cb);
164 if (!strcmp(var, "tag.forcesignannotated")) {
165 force_sign_annotate = git_config_bool(var, value);
169 if (starts_with(var, "column."))
170 return git_column_config(var, value, "tag", &colopts);
171 return git_default_config(var, value, cb);
174 static void write_tag_body(int fd, const unsigned char *sha1)
177 enum object_type type;
180 buf = read_sha1_file(sha1, &type, &size);
184 sp = strstr(buf, "\n\n");
186 if (!sp || !size || type != OBJ_TAG) {
190 sp += 2; /* skip the 2 LFs */
191 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
196 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
198 if (sign && do_sign(buf) < 0)
199 return error(_("unable to sign the tag"));
200 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
201 return error(_("unable to write tag file"));
205 struct create_tag_options {
206 unsigned int message_given:1;
215 static void create_tag(const unsigned char *object, const char *tag,
216 struct strbuf *buf, struct create_tag_options *opt,
217 unsigned char *prev, unsigned char *result)
219 enum object_type type;
220 char header_buf[1024];
224 type = sha1_object_info(object, NULL);
225 if (type <= OBJ_NONE)
226 die(_("bad object type."));
228 header_len = snprintf(header_buf, sizeof(header_buf),
236 git_committer_info(IDENT_STRICT));
238 if (header_len > sizeof(header_buf) - 1)
239 die(_("tag header too big."));
241 if (!opt->message_given) {
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 (!is_null_sha1(prev)) {
251 write_tag_body(fd, prev);
253 struct strbuf buf = STRBUF_INIT;
254 strbuf_addch(&buf, '\n');
255 if (opt->cleanup_mode == CLEANUP_ALL)
256 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
258 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
259 write_or_die(fd, buf.buf, buf.len);
260 strbuf_release(&buf);
264 if (launch_editor(path, buf, NULL)) {
266 _("Please supply the message using either -m or -F option.\n"));
271 if (opt->cleanup_mode != CLEANUP_NONE)
272 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
274 if (!opt->message_given && !buf->len)
275 die(_("no tag message?"));
277 strbuf_insert(buf, 0, header_buf, header_len);
279 if (build_tag_object(buf, opt->sign, result) < 0) {
281 fprintf(stderr, _("The tag message has been left in %s\n"),
286 unlink_or_warn(path);
296 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
298 struct msg_arg *msg = opt->value;
303 strbuf_addstr(&(msg->buf), "\n\n");
304 strbuf_addstr(&(msg->buf), arg);
309 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
315 strbuf_addf(sb, "refs/tags/%s", name);
317 return check_refname_format(sb->buf, 0);
320 int cmd_tag(int argc, const char **argv, const char *prefix)
322 struct strbuf buf = STRBUF_INIT;
323 struct strbuf ref = STRBUF_INIT;
324 unsigned char object[20], prev[20];
325 const char *object_ref, *tag;
326 struct create_tag_options opt;
327 char *cleanup_arg = NULL;
328 int create_reflog = 0;
329 int annotate = 0, force = 0;
330 int cmdmode = 0, create_tag_object = 0;
331 const char *msgfile = NULL, *keyid = NULL;
332 struct msg_arg msg = { 0, STRBUF_INIT };
333 struct ref_transaction *transaction;
334 struct strbuf err = STRBUF_INIT;
335 struct ref_filter filter;
336 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
337 const char *format = NULL;
339 struct option options[] = {
340 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
341 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
342 N_("print <n> lines of each tag message"),
343 PARSE_OPT_OPTARG, NULL, 1 },
344 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
345 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
347 OPT_GROUP(N_("Tag creation options")),
348 OPT_BOOL('a', "annotate", &annotate,
349 N_("annotated tag, needs a message")),
350 OPT_CALLBACK('m', "message", &msg, N_("message"),
351 N_("tag message"), parse_msg_arg),
352 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
353 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
354 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
355 N_("how to strip spaces and #comments from message")),
356 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
357 N_("use another key to sign the tag")),
358 OPT__FORCE(&force, N_("replace the tag if exists")),
359 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
361 OPT_GROUP(N_("Tag listing options")),
362 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
363 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
364 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
365 OPT_MERGED(&filter, N_("print only tags that are merged")),
366 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
367 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
368 N_("field name to sort on"), &parse_opt_ref_sorting),
370 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
371 N_("print only tags of the object"), 0, parse_opt_object_name
373 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
374 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
378 git_config(git_tag_config, sorting_tail);
380 memset(&opt, 0, sizeof(opt));
381 memset(&filter, 0, sizeof(filter));
384 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
388 set_signing_key(keyid);
390 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
392 if (argc == 0 && !cmdmode)
395 if ((create_tag_object || force) && (cmdmode != 0))
396 usage_with_options(git_tag_usage, options);
398 finalize_colopts(&colopts, -1);
399 if (cmdmode == 'l' && filter.lines != -1) {
400 if (explicitly_enable_column(colopts))
401 die(_("--column and -n are incompatible"));
405 sorting = ref_default_sorting();
406 sorting->ignore_case = icase;
407 filter.ignore_case = icase;
408 if (cmdmode == 'l') {
410 if (column_active(colopts)) {
411 struct column_options copts;
412 memset(&copts, 0, sizeof(copts));
414 run_column_filter(colopts, &copts);
416 filter.name_patterns = argv;
417 ret = list_tags(&filter, sorting, format);
418 if (column_active(colopts))
419 stop_column_filter();
422 if (filter.lines != -1)
423 die(_("-n option is only allowed with -l."));
424 if (filter.with_commit)
425 die(_("--contains option is only allowed with -l."));
426 if (filter.points_at.nr)
427 die(_("--points-at option is only allowed with -l."));
428 if (filter.merge_commit)
429 die(_("--merged and --no-merged option are only allowed with -l"));
431 return for_each_tag_name(argv, delete_tag);
433 return for_each_tag_name(argv, verify_tag);
435 if (msg.given || msgfile) {
436 if (msg.given && msgfile)
437 die(_("only one -F or -m option is allowed."));
439 strbuf_addbuf(&buf, &(msg.buf));
441 if (!strcmp(msgfile, "-")) {
442 if (strbuf_read(&buf, 0, 1024) < 0)
443 die_errno(_("cannot read '%s'"), msgfile);
445 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
446 die_errno(_("could not open or read '%s'"),
454 object_ref = argc == 2 ? argv[1] : "HEAD";
456 die(_("too many params"));
458 if (get_sha1(object_ref, object))
459 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
461 if (strbuf_check_tag_ref(&ref, tag))
462 die(_("'%s' is not a valid tag name."), tag);
464 if (read_ref(ref.buf, prev))
467 die(_("tag '%s' already exists"), tag);
469 opt.message_given = msg.given || msgfile;
471 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
472 opt.cleanup_mode = CLEANUP_ALL;
473 else if (!strcmp(cleanup_arg, "verbatim"))
474 opt.cleanup_mode = CLEANUP_NONE;
475 else if (!strcmp(cleanup_arg, "whitespace"))
476 opt.cleanup_mode = CLEANUP_SPACE;
478 die(_("Invalid cleanup mode %s"), cleanup_arg);
480 if (create_tag_object) {
481 if (force_sign_annotate && !annotate)
483 create_tag(object, tag, &buf, &opt, prev, object);
486 transaction = ref_transaction_begin(&err);
488 ref_transaction_update(transaction, ref.buf, object, prev,
489 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
491 ref_transaction_commit(transaction, &err))
493 ref_transaction_free(transaction);
494 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
495 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
497 strbuf_release(&err);
498 strbuf_release(&buf);
499 strbuf_release(&ref);