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"
16 static const char * const git_tag_usage[] = {
17 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
18 "git tag -d <tagname>...",
19 "git tag -l [-n[<num>]] [<pattern>]",
20 "git tag -v <tagname>...",
24 static char signingkey[1000];
29 struct commit_list *with_commit;
32 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
34 static int show_reference(const char *refname, const unsigned char *sha1,
35 int flag, void *cb_data)
37 struct tag_filter *filter = cb_data;
39 if (!fnmatch(filter->pattern, refname, 0)) {
42 enum object_type type;
46 if (filter->with_commit) {
47 struct commit *commit;
49 commit = lookup_commit_reference_gently(sha1, 1);
52 if (!is_descendant_of(commit, filter->with_commit))
57 printf("%s\n", refname);
60 printf("%-15s ", refname);
62 buf = read_sha1_file(sha1, &type, &size);
67 sp = strstr(buf, "\n\n");
72 /* only take up to "lines" lines, and strip the signature */
74 i < filter->lines && sp < buf + size &&
75 prefixcmp(sp, PGP_SIGNATURE "\n");
79 eol = memchr(sp, '\n', size - (sp - buf));
80 len = eol ? eol - sp : size - (sp - buf);
81 fwrite(sp, len, 1, stdout);
93 static int list_tags(const char *pattern, int lines,
94 struct commit_list *with_commit)
96 struct tag_filter filter;
101 filter.pattern = pattern;
102 filter.lines = lines;
103 filter.with_commit = with_commit;
105 for_each_tag_ref(show_reference, (void *) &filter);
110 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
111 const unsigned char *sha1);
113 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
118 unsigned char sha1[20];
120 for (p = argv; *p; p++) {
121 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
123 error("tag name too long: %.*s...", 50, *p);
127 if (!resolve_ref(ref, sha1, 1, NULL)) {
128 error("tag '%s' not found.", *p);
132 if (fn(*p, ref, sha1))
138 static int delete_tag(const char *name, const char *ref,
139 const unsigned char *sha1)
141 if (delete_ref(ref, sha1, 0))
143 printf("Deleted tag '%s'\n", name);
147 static int verify_tag(const char *name, const char *ref,
148 const unsigned char *sha1)
150 const char *argv_verify_tag[] = {"git-verify-tag",
151 "-v", "SHA1_HEX", NULL};
152 argv_verify_tag[2] = sha1_to_hex(sha1);
154 if (run_command_v_opt(argv_verify_tag, 0))
155 return error("could not verify the tag '%s'", name);
159 static int do_sign(struct strbuf *buffer)
161 struct child_process gpg;
168 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
169 sizeof(signingkey)) > sizeof(signingkey) - 1)
170 return error("committer info too long.");
171 bracket = strchr(signingkey, '>');
176 /* When the username signingkey is bad, program could be terminated
177 * because gpg exits without reading and then write gets SIGPIPE. */
178 signal(SIGPIPE, SIG_IGN);
180 memset(&gpg, 0, sizeof(gpg));
186 args[2] = signingkey;
189 if (start_command(&gpg))
190 return error("could not run gpg.");
192 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
195 finish_command(&gpg);
196 return error("gpg did not accept the tag data");
199 len = strbuf_read(buffer, gpg.out, 1024);
202 if (finish_command(&gpg) || !len || len < 0)
203 return error("gpg failed to sign the tag");
205 /* Strip CR from the line endings, in case we are on Windows. */
206 for (i = j = 0; i < buffer->len; i++)
207 if (buffer->buf[i] != '\r') {
209 buffer->buf[j] = buffer->buf[i];
212 strbuf_setlen(buffer, j);
217 static const char tag_template[] =
220 "# Write a tag message\n"
223 static void set_signingkey(const char *value)
225 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
226 die("signing key value too long (%.10s...)", value);
229 static int git_tag_config(const char *var, const char *value, void *cb)
231 if (!strcmp(var, "user.signingkey")) {
233 return config_error_nonbool(var);
234 set_signingkey(value);
238 return git_default_config(var, value, cb);
241 static void write_tag_body(int fd, const unsigned char *sha1)
244 enum object_type type;
245 char *buf, *sp, *eob;
248 buf = read_sha1_file(sha1, &type, &size);
252 sp = strstr(buf, "\n\n");
254 if (!sp || !size || type != OBJ_TAG) {
258 sp += 2; /* skip the 2 LFs */
259 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
263 len = buf + size - sp;
264 write_or_die(fd, sp, len);
269 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
271 if (sign && do_sign(buf) < 0)
272 return error("unable to sign the tag");
273 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
274 return error("unable to write tag file");
278 static void create_tag(const unsigned char *object, const char *tag,
279 struct strbuf *buf, int message, int sign,
280 unsigned char *prev, unsigned char *result)
282 enum object_type type;
283 char header_buf[1024];
287 type = sha1_object_info(object, NULL);
288 if (type <= OBJ_NONE)
289 die("bad object type.");
291 header_len = snprintf(header_buf, sizeof(header_buf),
299 git_committer_info(IDENT_ERROR_ON_NO_NAME));
301 if (header_len > sizeof(header_buf) - 1)
302 die("tag header too big.");
307 /* write the template message before editing: */
308 path = git_pathdup("TAG_EDITMSG");
309 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
311 die("could not create file '%s': %s",
312 path, strerror(errno));
314 if (!is_null_sha1(prev))
315 write_tag_body(fd, prev);
317 write_or_die(fd, tag_template, strlen(tag_template));
320 if (launch_editor(path, buf, NULL)) {
322 "Please supply the message using either -m or -F option.\n");
329 if (!message && !buf->len)
330 die("no tag message?");
332 strbuf_insert(buf, 0, header_buf, header_len);
334 if (build_tag_object(buf, sign, result) < 0) {
336 fprintf(stderr, "The tag message has been left in %s\n",
341 unlink_or_warn(path);
351 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
353 struct msg_arg *msg = opt->value;
358 strbuf_addstr(&(msg->buf), "\n\n");
359 strbuf_addstr(&(msg->buf), arg);
364 int cmd_tag(int argc, const char **argv, const char *prefix)
366 struct strbuf buf = STRBUF_INIT;
367 unsigned char object[20], prev[20];
369 const char *object_ref, *tag;
370 struct ref_lock *lock;
372 int annotate = 0, sign = 0, force = 0, lines = -1,
373 list = 0, delete = 0, verify = 0;
374 const char *msgfile = NULL, *keyid = NULL;
375 struct msg_arg msg = { 0, STRBUF_INIT };
376 struct commit_list *with_commit = NULL;
377 struct option options[] = {
378 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
379 { OPTION_INTEGER, 'n', NULL, &lines, "n",
380 "print <n> lines of each tag message",
381 PARSE_OPT_OPTARG, NULL, 1 },
382 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
383 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
385 OPT_GROUP("Tag creation options"),
386 OPT_BOOLEAN('a', NULL, &annotate,
387 "annotated tag, needs a message"),
388 OPT_CALLBACK('m', NULL, &msg, "msg",
389 "message for the tag", parse_msg_arg),
390 OPT_FILENAME('F', NULL, &msgfile, "message in a file"),
391 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
392 OPT_STRING('u', NULL, &keyid, "key-id",
393 "use another key to sign the tag"),
394 OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
396 OPT_GROUP("Tag listing options"),
398 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
399 "print only tags that contain the commit",
400 PARSE_OPT_LASTARG_DEFAULT,
401 parse_opt_with_commit, (intptr_t)"HEAD",
406 git_config(git_tag_config, NULL);
408 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
412 set_signingkey(keyid);
416 if (argc == 0 && !(delete || verify))
419 if ((annotate || msg.given || msgfile || force) &&
420 (list || delete || verify))
421 usage_with_options(git_tag_usage, options);
423 if (list + delete + verify > 1)
424 usage_with_options(git_tag_usage, options);
426 return list_tags(argv[0], lines == -1 ? 0 : lines,
429 die("-n option is only allowed with -l.");
431 die("--contains option is only allowed with -l.");
433 return for_each_tag_name(argv, delete_tag);
435 return for_each_tag_name(argv, verify_tag);
437 if (msg.given || msgfile) {
438 if (msg.given && msgfile)
439 die("only one -F or -m option is allowed.");
442 strbuf_addbuf(&buf, &(msg.buf));
444 if (!strcmp(msgfile, "-")) {
445 if (strbuf_read(&buf, 0, 1024) < 0)
446 die("cannot read %s", msgfile);
448 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
449 die("could not open or read '%s': %s",
450 msgfile, strerror(errno));
457 object_ref = argc == 2 ? argv[1] : "HEAD";
459 die("too many params");
461 if (get_sha1(object_ref, object))
462 die("Failed to resolve '%s' as a valid ref.", object_ref);
464 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
465 die("tag name too long: %.*s...", 50, tag);
466 if (check_ref_format(ref))
467 die("'%s' is not a valid tag name.", tag);
469 if (!resolve_ref(ref, prev, 1, NULL))
472 die("tag '%s' already exists", tag);
475 create_tag(object, tag, &buf, msg.given || msgfile,
478 lock = lock_any_ref_for_update(ref, prev, 0);
480 die("%s: cannot lock the ref", ref);
481 if (write_ref_sha1(lock, object, NULL) < 0)
482 die("%s: cannot update the ref", ref);
484 strbuf_release(&buf);