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"
19 static const char * const git_tag_usage[] = {
20 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
21 "git tag -d <tagname>...",
22 "git tag -l [-n[<num>]] [<pattern>...]",
23 "git tag -v <tagname>...",
28 const char **patterns;
30 struct commit_list *with_commit;
33 static int match_pattern(const char **patterns, const char *ref)
35 /* no pattern means match everything */
38 for (; *patterns; patterns++)
39 if (!fnmatch(*patterns, ref, 0))
44 static int in_commit_list(const struct commit_list *want, struct commit *c)
46 for (; want; want = want->next)
47 if (!hashcmp(want->item->object.sha1, c->object.sha1))
52 static int contains_recurse(struct commit *candidate,
53 const struct commit_list *want)
55 struct commit_list *p;
57 /* was it previously marked as containing a want commit? */
58 if (candidate->object.flags & TMP_MARK)
60 /* or marked as not possibly containing a want commit? */
61 if (candidate->object.flags & UNINTERESTING)
64 if (in_commit_list(want, candidate))
67 if (parse_commit(candidate) < 0)
70 /* Otherwise recurse and mark ourselves for future traversals. */
71 for (p = candidate->parents; p; p = p->next) {
72 if (contains_recurse(p->item, want)) {
73 candidate->object.flags |= TMP_MARK;
77 candidate->object.flags |= UNINTERESTING;
81 static int contains(struct commit *candidate, const struct commit_list *want)
83 return contains_recurse(candidate, want);
86 static int show_reference(const char *refname, const unsigned char *sha1,
87 int flag, void *cb_data)
89 struct tag_filter *filter = cb_data;
91 if (match_pattern(filter->patterns, refname)) {
94 enum object_type type;
98 if (filter->with_commit) {
99 struct commit *commit;
101 commit = lookup_commit_reference_gently(sha1, 1);
104 if (!contains(commit, filter->with_commit))
108 if (!filter->lines) {
109 printf("%s\n", refname);
112 printf("%-15s ", refname);
114 buf = read_sha1_file(sha1, &type, &size);
119 sp = strstr(buf, "\n\n");
124 /* only take up to "lines" lines, and strip the signature */
125 size = parse_signature(buf, size);
127 i < filter->lines && sp < buf + size;
131 eol = memchr(sp, '\n', size - (sp - buf));
132 len = eol ? eol - sp : size - (sp - buf);
133 fwrite(sp, len, 1, stdout);
145 static int list_tags(const char **patterns, int lines,
146 struct commit_list *with_commit)
148 struct tag_filter filter;
150 filter.patterns = patterns;
151 filter.lines = lines;
152 filter.with_commit = with_commit;
154 for_each_tag_ref(show_reference, (void *) &filter);
159 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
160 const unsigned char *sha1);
162 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
167 unsigned char sha1[20];
169 for (p = argv; *p; p++) {
170 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
172 error(_("tag name too long: %.*s..."), 50, *p);
176 if (read_ref(ref, sha1)) {
177 error(_("tag '%s' not found."), *p);
181 if (fn(*p, ref, sha1))
187 static int delete_tag(const char *name, const char *ref,
188 const unsigned char *sha1)
190 if (delete_ref(ref, sha1, 0))
192 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
196 static int verify_tag(const char *name, const char *ref,
197 const unsigned char *sha1)
199 const char *argv_verify_tag[] = {"verify-tag",
200 "-v", "SHA1_HEX", NULL};
201 argv_verify_tag[2] = sha1_to_hex(sha1);
203 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
204 return error(_("could not verify the tag '%s'"), name);
208 static int do_sign(struct strbuf *buffer)
210 return sign_buffer(buffer, buffer, get_signing_key());
213 static const char tag_template[] =
216 "# Write a tag message\n"
217 "# Lines starting with '#' will be ignored.\n"
220 static const char tag_template_nocleanup[] =
223 "# Write a tag message\n"
224 "# Lines starting with '#' will be kept; you may remove them"
225 " yourself if you want to.\n"
228 static int git_tag_config(const char *var, const char *value, void *cb)
230 int status = git_gpg_config(var, value, cb);
233 return git_default_config(var, value, cb);
236 static void write_tag_body(int fd, const unsigned char *sha1)
239 enum object_type type;
242 buf = read_sha1_file(sha1, &type, &size);
246 sp = strstr(buf, "\n\n");
248 if (!sp || !size || type != OBJ_TAG) {
252 sp += 2; /* skip the 2 LFs */
253 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
258 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
260 if (sign && do_sign(buf) < 0)
261 return error(_("unable to sign the tag"));
262 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
263 return error(_("unable to write tag file"));
267 struct create_tag_options {
268 unsigned int message_given:1;
277 static void create_tag(const unsigned char *object, const char *tag,
278 struct strbuf *buf, struct create_tag_options *opt,
279 unsigned char *prev, unsigned char *result)
281 enum object_type type;
282 char header_buf[1024];
286 type = sha1_object_info(object, NULL);
287 if (type <= OBJ_NONE)
288 die(_("bad object type."));
290 header_len = snprintf(header_buf, sizeof(header_buf),
298 git_committer_info(IDENT_ERROR_ON_NO_NAME));
300 if (header_len > sizeof(header_buf) - 1)
301 die(_("tag header too big."));
303 if (!opt->message_given) {
306 /* write the template message before editing: */
307 path = git_pathdup("TAG_EDITMSG");
308 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
310 die_errno(_("could not create file '%s'"), path);
312 if (!is_null_sha1(prev))
313 write_tag_body(fd, prev);
314 else if (opt->cleanup_mode == CLEANUP_ALL)
315 write_or_die(fd, _(tag_template),
316 strlen(_(tag_template)));
318 write_or_die(fd, _(tag_template_nocleanup),
319 strlen(_(tag_template_nocleanup)));
322 if (launch_editor(path, buf, NULL)) {
324 _("Please supply the message using either -m or -F option.\n"));
329 if (opt->cleanup_mode != CLEANUP_NONE)
330 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
332 if (!opt->message_given && !buf->len)
333 die(_("no tag message?"));
335 strbuf_insert(buf, 0, header_buf, header_len);
337 if (build_tag_object(buf, opt->sign, result) < 0) {
339 fprintf(stderr, _("The tag message has been left in %s\n"),
344 unlink_or_warn(path);
354 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
356 struct msg_arg *msg = opt->value;
361 strbuf_addstr(&(msg->buf), "\n\n");
362 strbuf_addstr(&(msg->buf), arg);
367 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
373 strbuf_addf(sb, "refs/tags/%s", name);
375 return check_refname_format(sb->buf, 0);
378 int cmd_tag(int argc, const char **argv, const char *prefix)
380 struct strbuf buf = STRBUF_INIT;
381 struct strbuf ref = STRBUF_INIT;
382 unsigned char object[20], prev[20];
383 const char *object_ref, *tag;
384 struct ref_lock *lock;
385 struct create_tag_options opt;
386 char *cleanup_arg = NULL;
387 int annotate = 0, force = 0, lines = -1, list = 0,
388 delete = 0, verify = 0;
389 const char *msgfile = NULL, *keyid = NULL;
390 struct msg_arg msg = { 0, STRBUF_INIT };
391 struct commit_list *with_commit = NULL;
392 struct option options[] = {
393 OPT_BOOLEAN('l', "list", &list, "list tag names"),
394 { OPTION_INTEGER, 'n', NULL, &lines, "n",
395 "print <n> lines of each tag message",
396 PARSE_OPT_OPTARG, NULL, 1 },
397 OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
398 OPT_BOOLEAN('v', "verify", &verify, "verify tags"),
400 OPT_GROUP("Tag creation options"),
401 OPT_BOOLEAN('a', "annotate", &annotate,
402 "annotated tag, needs a message"),
403 OPT_CALLBACK('m', "message", &msg, "message",
404 "tag message", parse_msg_arg),
405 OPT_FILENAME('F', "file", &msgfile, "read message from file"),
406 OPT_BOOLEAN('s', "sign", &opt.sign, "annotated and GPG-signed tag"),
407 OPT_STRING(0, "cleanup", &cleanup_arg, "mode",
408 "how to strip spaces and #comments from message"),
409 OPT_STRING('u', "local-user", &keyid, "key-id",
410 "use another key to sign the tag"),
411 OPT__FORCE(&force, "replace the tag if exists"),
413 OPT_GROUP("Tag listing options"),
415 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
416 "print only tags that contain the commit",
417 PARSE_OPT_LASTARG_DEFAULT,
418 parse_opt_with_commit, (intptr_t)"HEAD",
423 git_config(git_tag_config, NULL);
425 memset(&opt, 0, sizeof(opt));
427 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
431 set_signing_key(keyid);
435 if (argc == 0 && !(delete || verify))
438 if ((annotate || msg.given || msgfile || force) &&
439 (list || delete || verify))
440 usage_with_options(git_tag_usage, options);
442 if (list + delete + verify > 1)
443 usage_with_options(git_tag_usage, options);
445 return list_tags(argv, lines == -1 ? 0 : lines,
448 die(_("-n option is only allowed with -l."));
450 die(_("--contains option is only allowed with -l."));
452 return for_each_tag_name(argv, delete_tag);
454 return for_each_tag_name(argv, verify_tag);
456 if (msg.given || msgfile) {
457 if (msg.given && msgfile)
458 die(_("only one -F or -m option is allowed."));
461 strbuf_addbuf(&buf, &(msg.buf));
463 if (!strcmp(msgfile, "-")) {
464 if (strbuf_read(&buf, 0, 1024) < 0)
465 die_errno(_("cannot read '%s'"), msgfile);
467 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
468 die_errno(_("could not open or read '%s'"),
476 object_ref = argc == 2 ? argv[1] : "HEAD";
478 die(_("too many params"));
480 if (get_sha1(object_ref, object))
481 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
483 if (strbuf_check_tag_ref(&ref, tag))
484 die(_("'%s' is not a valid tag name."), tag);
486 if (read_ref(ref.buf, prev))
489 die(_("tag '%s' already exists"), tag);
491 opt.message_given = msg.given || msgfile;
493 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
494 opt.cleanup_mode = CLEANUP_ALL;
495 else if (!strcmp(cleanup_arg, "verbatim"))
496 opt.cleanup_mode = CLEANUP_NONE;
497 else if (!strcmp(cleanup_arg, "whitespace"))
498 opt.cleanup_mode = CLEANUP_SPACE;
500 die(_("Invalid cleanup mode %s"), cleanup_arg);
503 create_tag(object, tag, &buf, &opt, prev, object);
505 lock = lock_any_ref_for_update(ref.buf, prev, 0);
507 die(_("%s: cannot lock the ref"), ref.buf);
508 if (write_ref_sha1(lock, object, NULL) < 0)
509 die(_("%s: cannot update the ref"), ref.buf);
510 if (force && hashcmp(prev, object))
511 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
513 strbuf_release(&buf);
514 strbuf_release(&ref);