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];
31 struct commit_list *with_commit;
34 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
36 static int in_commit_list(const struct commit_list *want, struct commit *c)
38 for (; want; want = want->next)
39 if (!hashcmp(want->item->object.sha1, c->object.sha1))
44 static int contains_recurse(struct commit *candidate,
45 const struct commit_list *want)
47 struct commit_list *p;
49 /* was it previously marked as containing a want commit? */
50 if (candidate->object.flags & TMP_MARK)
52 /* or marked as not possibly containing a want commit? */
53 if (candidate->object.flags & UNINTERESTING)
56 if (in_commit_list(want, candidate))
59 if (parse_commit(candidate) < 0)
62 /* Otherwise recurse and mark ourselves for future traversals. */
63 for (p = candidate->parents; p; p = p->next) {
64 if (contains_recurse(p->item, want)) {
65 candidate->object.flags |= TMP_MARK;
69 candidate->object.flags |= UNINTERESTING;
73 static int contains(struct commit *candidate, const struct commit_list *want)
75 return contains_recurse(candidate, want);
78 static int show_reference(const char *refname, const unsigned char *sha1,
79 int flag, void *cb_data)
81 struct tag_filter *filter = cb_data;
83 if (!fnmatch(filter->pattern, refname, 0)) {
86 enum object_type type;
90 if (filter->with_commit) {
91 struct commit *commit;
93 commit = lookup_commit_reference_gently(sha1, 1);
96 if (!contains(commit, filter->with_commit))
100 if (!filter->lines) {
101 printf("%s\n", refname);
104 printf("%-15s ", refname);
106 buf = read_sha1_file(sha1, &type, &size);
111 sp = strstr(buf, "\n\n");
116 /* only take up to "lines" lines, and strip the signature */
118 i < filter->lines && sp < buf + size &&
119 prefixcmp(sp, PGP_SIGNATURE "\n");
123 eol = memchr(sp, '\n', size - (sp - buf));
124 len = eol ? eol - sp : size - (sp - buf);
125 fwrite(sp, len, 1, stdout);
137 static int list_tags(const char *pattern, int lines,
138 struct commit_list *with_commit)
140 struct tag_filter filter;
145 filter.pattern = pattern;
146 filter.lines = lines;
147 filter.with_commit = with_commit;
149 for_each_tag_ref(show_reference, (void *) &filter);
154 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
155 const unsigned char *sha1);
157 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
162 unsigned char sha1[20];
164 for (p = argv; *p; p++) {
165 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
167 error("tag name too long: %.*s...", 50, *p);
171 if (!resolve_ref(ref, sha1, 1, NULL)) {
172 error("tag '%s' not found.", *p);
176 if (fn(*p, ref, sha1))
182 static int delete_tag(const char *name, const char *ref,
183 const unsigned char *sha1)
185 if (delete_ref(ref, sha1, 0))
187 printf("Deleted tag '%s' (was %s)\n", name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
191 static int verify_tag(const char *name, const char *ref,
192 const unsigned char *sha1)
194 const char *argv_verify_tag[] = {"verify-tag",
195 "-v", "SHA1_HEX", NULL};
196 argv_verify_tag[2] = sha1_to_hex(sha1);
198 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
199 return error("could not verify the tag '%s'", name);
203 static int do_sign(struct strbuf *buffer)
205 struct child_process gpg;
212 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
213 sizeof(signingkey)) > sizeof(signingkey) - 1)
214 return error("committer info too long.");
215 bracket = strchr(signingkey, '>');
220 /* When the username signingkey is bad, program could be terminated
221 * because gpg exits without reading and then write gets SIGPIPE. */
222 signal(SIGPIPE, SIG_IGN);
224 memset(&gpg, 0, sizeof(gpg));
230 args[2] = signingkey;
233 if (start_command(&gpg))
234 return error("could not run gpg.");
236 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
239 finish_command(&gpg);
240 return error("gpg did not accept the tag data");
243 len = strbuf_read(buffer, gpg.out, 1024);
246 if (finish_command(&gpg) || !len || len < 0)
247 return error("gpg failed to sign the tag");
249 /* Strip CR from the line endings, in case we are on Windows. */
250 for (i = j = 0; i < buffer->len; i++)
251 if (buffer->buf[i] != '\r') {
253 buffer->buf[j] = buffer->buf[i];
256 strbuf_setlen(buffer, j);
261 static const char tag_template[] =
264 "# Write a tag message\n"
267 static void set_signingkey(const char *value)
269 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
270 die("signing key value too long (%.10s...)", value);
273 static int git_tag_config(const char *var, const char *value, void *cb)
275 if (!strcmp(var, "user.signingkey")) {
277 return config_error_nonbool(var);
278 set_signingkey(value);
282 return git_default_config(var, value, cb);
285 static void write_tag_body(int fd, const unsigned char *sha1)
288 enum object_type type;
289 char *buf, *sp, *eob;
292 buf = read_sha1_file(sha1, &type, &size);
296 sp = strstr(buf, "\n\n");
298 if (!sp || !size || type != OBJ_TAG) {
302 sp += 2; /* skip the 2 LFs */
303 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
307 len = buf + size - sp;
308 write_or_die(fd, sp, len);
313 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
315 if (sign && do_sign(buf) < 0)
316 return error("unable to sign the tag");
317 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
318 return error("unable to write tag file");
322 static void create_tag(const unsigned char *object, const char *tag,
323 struct strbuf *buf, int message, int sign,
324 unsigned char *prev, unsigned char *result)
326 enum object_type type;
327 char header_buf[1024];
331 type = sha1_object_info(object, NULL);
332 if (type <= OBJ_NONE)
333 die("bad object type.");
335 header_len = snprintf(header_buf, sizeof(header_buf),
343 git_committer_info(IDENT_ERROR_ON_NO_NAME));
345 if (header_len > sizeof(header_buf) - 1)
346 die("tag header too big.");
351 /* write the template message before editing: */
352 path = git_pathdup("TAG_EDITMSG");
353 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
355 die_errno("could not create file '%s'", path);
357 if (!is_null_sha1(prev))
358 write_tag_body(fd, prev);
360 write_or_die(fd, tag_template, strlen(tag_template));
363 if (launch_editor(path, buf, NULL)) {
365 "Please supply the message using either -m or -F option.\n");
372 if (!message && !buf->len)
373 die("no tag message?");
375 strbuf_insert(buf, 0, header_buf, header_len);
377 if (build_tag_object(buf, sign, result) < 0) {
379 fprintf(stderr, "The tag message has been left in %s\n",
384 unlink_or_warn(path);
394 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
396 struct msg_arg *msg = opt->value;
401 strbuf_addstr(&(msg->buf), "\n\n");
402 strbuf_addstr(&(msg->buf), arg);
407 int cmd_tag(int argc, const char **argv, const char *prefix)
409 struct strbuf buf = STRBUF_INIT;
410 unsigned char object[20], prev[20];
412 const char *object_ref, *tag;
413 struct ref_lock *lock;
415 int annotate = 0, sign = 0, force = 0, lines = -1,
416 list = 0, delete = 0, verify = 0;
417 const char *msgfile = NULL, *keyid = NULL;
418 struct msg_arg msg = { 0, STRBUF_INIT };
419 struct commit_list *with_commit = NULL;
420 struct option options[] = {
421 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
422 { OPTION_INTEGER, 'n', NULL, &lines, "n",
423 "print <n> lines of each tag message",
424 PARSE_OPT_OPTARG, NULL, 1 },
425 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
426 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
428 OPT_GROUP("Tag creation options"),
429 OPT_BOOLEAN('a', NULL, &annotate,
430 "annotated tag, needs a message"),
431 OPT_CALLBACK('m', NULL, &msg, "msg",
432 "message for the tag", parse_msg_arg),
433 OPT_FILENAME('F', NULL, &msgfile, "message in a file"),
434 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
435 OPT_STRING('u', NULL, &keyid, "key-id",
436 "use another key to sign the tag"),
437 OPT_BOOLEAN('f', "force", &force, "replace the tag if exists"),
439 OPT_GROUP("Tag listing options"),
441 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
442 "print only tags that contain the commit",
443 PARSE_OPT_LASTARG_DEFAULT,
444 parse_opt_with_commit, (intptr_t)"HEAD",
449 git_config(git_tag_config, NULL);
451 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
455 set_signingkey(keyid);
459 if (argc == 0 && !(delete || verify))
462 if ((annotate || msg.given || msgfile || force) &&
463 (list || delete || verify))
464 usage_with_options(git_tag_usage, options);
466 if (list + delete + verify > 1)
467 usage_with_options(git_tag_usage, options);
469 return list_tags(argv[0], lines == -1 ? 0 : lines,
472 die("-n option is only allowed with -l.");
474 die("--contains option is only allowed with -l.");
476 return for_each_tag_name(argv, delete_tag);
478 return for_each_tag_name(argv, verify_tag);
480 if (msg.given || msgfile) {
481 if (msg.given && msgfile)
482 die("only one -F or -m option is allowed.");
485 strbuf_addbuf(&buf, &(msg.buf));
487 if (!strcmp(msgfile, "-")) {
488 if (strbuf_read(&buf, 0, 1024) < 0)
489 die_errno("cannot read '%s'", msgfile);
491 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
492 die_errno("could not open or read '%s'",
500 object_ref = argc == 2 ? argv[1] : "HEAD";
502 die("too many params");
504 if (get_sha1(object_ref, object))
505 die("Failed to resolve '%s' as a valid ref.", object_ref);
507 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
508 die("tag name too long: %.*s...", 50, tag);
509 if (check_ref_format(ref))
510 die("'%s' is not a valid tag name.", tag);
512 if (!resolve_ref(ref, prev, 1, NULL))
515 die("tag '%s' already exists", tag);
518 create_tag(object, tag, &buf, msg.given || msgfile,
521 lock = lock_any_ref_for_update(ref, prev, 0);
523 die("%s: cannot lock the ref", ref);
524 if (write_ref_sha1(lock, object, NULL) < 0)
525 die("%s: cannot update the ref", ref);
526 if (force && hashcmp(prev, object))
527 printf("Updated tag '%s' (was %s)\n", tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
529 strbuf_release(&buf);