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"
18 static const char * const git_tag_usage[] = {
19 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
20 "git tag -d <tagname>...",
21 "git tag -l [-n[<num>]] [<pattern>...]",
22 "git tag -v <tagname>...",
26 static char signingkey[1000];
28 static int core_clock_skew = 86400;
31 const char **patterns;
33 struct commit_list *with_commit;
36 static int match_pattern(const char **patterns, const char *ref)
38 /* no pattern means match everything */
41 for (; *patterns; patterns++)
42 if (!fnmatch(*patterns, ref, 0))
47 static int in_commit_list(const struct commit_list *want, struct commit *c)
49 for (; want; want = want->next)
50 if (!hashcmp(want->item->object.sha1, c->object.sha1))
55 static int contains_recurse(struct commit *candidate,
56 const struct commit_list *want,
59 struct commit_list *p;
61 /* was it previously marked as containing a want commit? */
62 if (candidate->object.flags & TMP_MARK)
64 /* or marked as not possibly containing a want commit? */
65 if (candidate->object.flags & UNINTERESTING)
68 if (in_commit_list(want, candidate))
71 if (parse_commit(candidate) < 0)
74 /* stop searching if we go too far back in time */
75 if (candidate->date < cutoff)
78 /* Otherwise recurse and mark ourselves for future traversals. */
79 for (p = candidate->parents; p; p = p->next) {
80 if (contains_recurse(p->item, want, cutoff)) {
81 candidate->object.flags |= TMP_MARK;
85 candidate->object.flags |= UNINTERESTING;
89 static int contains(struct commit *candidate, const struct commit_list *want)
91 unsigned long cutoff = 0;
93 if (core_clock_skew >= 0) {
94 const struct commit_list *c;
95 unsigned long min_date = ULONG_MAX;
96 for (c = want; c; c = c->next) {
97 if (parse_commit(c->item) < 0)
99 if (c->item->date < min_date)
100 min_date = c->item->date;
102 if (min_date > core_clock_skew)
103 cutoff = min_date - core_clock_skew;
106 return contains_recurse(candidate, want, cutoff);
109 static int show_reference(const char *refname, const unsigned char *sha1,
110 int flag, void *cb_data)
112 struct tag_filter *filter = cb_data;
114 if (match_pattern(filter->patterns, refname)) {
117 enum object_type type;
118 char *buf, *sp, *eol;
121 if (filter->with_commit) {
122 struct commit *commit;
124 commit = lookup_commit_reference_gently(sha1, 1);
127 if (!contains(commit, filter->with_commit))
131 if (!filter->lines) {
132 printf("%s\n", refname);
135 printf("%-15s ", refname);
137 buf = read_sha1_file(sha1, &type, &size);
142 sp = strstr(buf, "\n\n");
147 /* only take up to "lines" lines, and strip the signature */
148 size = parse_signature(buf, size);
150 i < filter->lines && sp < buf + size;
154 eol = memchr(sp, '\n', size - (sp - buf));
155 len = eol ? eol - sp : size - (sp - buf);
156 fwrite(sp, len, 1, stdout);
168 static int list_tags(const char **patterns, int lines,
169 struct commit_list *with_commit)
171 struct tag_filter filter;
173 filter.patterns = patterns;
174 filter.lines = lines;
175 filter.with_commit = with_commit;
177 for_each_tag_ref(show_reference, (void *) &filter);
182 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
183 const unsigned char *sha1);
185 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
190 unsigned char sha1[20];
192 for (p = argv; *p; p++) {
193 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
195 error(_("tag name too long: %.*s..."), 50, *p);
199 if (!resolve_ref(ref, sha1, 1, NULL)) {
200 error(_("tag '%s' not found."), *p);
204 if (fn(*p, ref, sha1))
210 static int delete_tag(const char *name, const char *ref,
211 const unsigned char *sha1)
213 if (delete_ref(ref, sha1, 0))
215 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
219 static int verify_tag(const char *name, const char *ref,
220 const unsigned char *sha1)
222 const char *argv_verify_tag[] = {"verify-tag",
223 "-v", "SHA1_HEX", NULL};
224 argv_verify_tag[2] = sha1_to_hex(sha1);
226 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
227 return error(_("could not verify the tag '%s'"), name);
231 static int do_sign(struct strbuf *buffer)
233 struct child_process gpg;
240 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
241 sizeof(signingkey)) > sizeof(signingkey) - 1)
242 return error(_("committer info too long."));
243 bracket = strchr(signingkey, '>');
248 /* When the username signingkey is bad, program could be terminated
249 * because gpg exits without reading and then write gets SIGPIPE. */
250 signal(SIGPIPE, SIG_IGN);
252 memset(&gpg, 0, sizeof(gpg));
258 args[2] = signingkey;
261 if (start_command(&gpg))
262 return error(_("could not run gpg."));
264 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
267 finish_command(&gpg);
268 return error(_("gpg did not accept the tag data"));
271 len = strbuf_read(buffer, gpg.out, 1024);
274 if (finish_command(&gpg) || !len || len < 0)
275 return error(_("gpg failed to sign the tag"));
277 /* Strip CR from the line endings, in case we are on Windows. */
278 for (i = j = 0; i < buffer->len; i++)
279 if (buffer->buf[i] != '\r') {
281 buffer->buf[j] = buffer->buf[i];
284 strbuf_setlen(buffer, j);
289 static const char tag_template[] =
292 "# Write a tag message\n"
295 static void set_signingkey(const char *value)
297 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
298 die(_("signing key value too long (%.10s...)"), value);
301 static int git_tag_config(const char *var, const char *value, void *cb)
303 if (!strcmp(var, "user.signingkey")) {
305 return config_error_nonbool(var);
306 set_signingkey(value);
310 if (!strcmp(var, "core.clockskew")) {
311 if (!value || !strcmp(value, "none"))
312 core_clock_skew = -1;
314 core_clock_skew = git_config_int(var, value);
318 return git_default_config(var, value, cb);
321 static void write_tag_body(int fd, const unsigned char *sha1)
324 enum object_type type;
327 buf = read_sha1_file(sha1, &type, &size);
331 sp = strstr(buf, "\n\n");
333 if (!sp || !size || type != OBJ_TAG) {
337 sp += 2; /* skip the 2 LFs */
338 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
343 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
345 if (sign && do_sign(buf) < 0)
346 return error(_("unable to sign the tag"));
347 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
348 return error(_("unable to write tag file"));
352 static void create_tag(const unsigned char *object, const char *tag,
353 struct strbuf *buf, int message, int sign,
354 unsigned char *prev, unsigned char *result)
356 enum object_type type;
357 char header_buf[1024];
361 type = sha1_object_info(object, NULL);
362 if (type <= OBJ_NONE)
363 die(_("bad object type."));
365 header_len = snprintf(header_buf, sizeof(header_buf),
373 git_committer_info(IDENT_ERROR_ON_NO_NAME));
375 if (header_len > sizeof(header_buf) - 1)
376 die(_("tag header too big."));
381 /* write the template message before editing: */
382 path = git_pathdup("TAG_EDITMSG");
383 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
385 die_errno(_("could not create file '%s'"), path);
387 if (!is_null_sha1(prev))
388 write_tag_body(fd, prev);
390 write_or_die(fd, _(tag_template), strlen(_(tag_template)));
393 if (launch_editor(path, buf, NULL)) {
395 _("Please supply the message using either -m or -F option.\n"));
402 if (!message && !buf->len)
403 die(_("no tag message?"));
405 strbuf_insert(buf, 0, header_buf, header_len);
407 if (build_tag_object(buf, sign, result) < 0) {
409 fprintf(stderr, _("The tag message has been left in %s\n"),
414 unlink_or_warn(path);
424 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
426 struct msg_arg *msg = opt->value;
431 strbuf_addstr(&(msg->buf), "\n\n");
432 strbuf_addstr(&(msg->buf), arg);
437 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
440 return CHECK_REF_FORMAT_ERROR;
443 strbuf_addf(sb, "refs/tags/%s", name);
445 return check_ref_format(sb->buf);
448 int cmd_tag(int argc, const char **argv, const char *prefix)
450 struct strbuf buf = STRBUF_INIT;
451 struct strbuf ref = STRBUF_INIT;
452 unsigned char object[20], prev[20];
453 const char *object_ref, *tag;
454 struct ref_lock *lock;
456 int annotate = 0, sign = 0, force = 0, lines = -1,
457 list = 0, delete = 0, verify = 0;
458 const char *msgfile = NULL, *keyid = NULL;
459 struct msg_arg msg = { 0, STRBUF_INIT };
460 struct commit_list *with_commit = NULL;
461 struct option options[] = {
462 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
463 { OPTION_INTEGER, 'n', NULL, &lines, "n",
464 "print <n> lines of each tag message",
465 PARSE_OPT_OPTARG, NULL, 1 },
466 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
467 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
469 OPT_GROUP("Tag creation options"),
470 OPT_BOOLEAN('a', NULL, &annotate,
471 "annotated tag, needs a message"),
472 OPT_CALLBACK('m', NULL, &msg, "message",
473 "tag message", parse_msg_arg),
474 OPT_FILENAME('F', NULL, &msgfile, "read message from file"),
475 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
476 OPT_STRING('u', NULL, &keyid, "key-id",
477 "use another key to sign the tag"),
478 OPT__FORCE(&force, "replace the tag if exists"),
480 OPT_GROUP("Tag listing options"),
482 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
483 "print only tags that contain the commit",
484 PARSE_OPT_LASTARG_DEFAULT,
485 parse_opt_with_commit, (intptr_t)"HEAD",
490 git_config(git_tag_config, NULL);
492 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
496 set_signingkey(keyid);
500 if (argc == 0 && !(delete || verify))
503 if ((annotate || msg.given || msgfile || force) &&
504 (list || delete || verify))
505 usage_with_options(git_tag_usage, options);
507 if (list + delete + verify > 1)
508 usage_with_options(git_tag_usage, options);
510 return list_tags(argv, lines == -1 ? 0 : lines,
513 die(_("-n option is only allowed with -l."));
515 die(_("--contains option is only allowed with -l."));
517 return for_each_tag_name(argv, delete_tag);
519 return for_each_tag_name(argv, verify_tag);
521 if (msg.given || msgfile) {
522 if (msg.given && msgfile)
523 die(_("only one -F or -m option is allowed."));
526 strbuf_addbuf(&buf, &(msg.buf));
528 if (!strcmp(msgfile, "-")) {
529 if (strbuf_read(&buf, 0, 1024) < 0)
530 die_errno(_("cannot read '%s'"), msgfile);
532 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
533 die_errno(_("could not open or read '%s'"),
541 object_ref = argc == 2 ? argv[1] : "HEAD";
543 die(_("too many params"));
545 if (get_sha1(object_ref, object))
546 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
548 if (strbuf_check_tag_ref(&ref, tag))
549 die(_("'%s' is not a valid tag name."), tag);
551 if (!resolve_ref(ref.buf, prev, 1, NULL))
554 die(_("tag '%s' already exists"), tag);
557 create_tag(object, tag, &buf, msg.given || msgfile,
560 lock = lock_any_ref_for_update(ref.buf, prev, 0);
562 die(_("%s: cannot lock the ref"), ref.buf);
563 if (write_ref_sha1(lock, object, NULL) < 0)
564 die(_("%s: cannot update the ref"), ref.buf);
565 if (force && hashcmp(prev, object))
566 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
568 strbuf_release(&buf);
569 strbuf_release(&ref);