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 [-n [<num>]] -l [<pattern>]",
20 "git-tag -v <tagname>...",
24 static char signingkey[1000];
26 static void launch_editor(const char *path, struct strbuf *buffer)
28 const char *editor, *terminal;
29 struct child_process child;
32 editor = getenv("GIT_EDITOR");
33 if (!editor && editor_program)
34 editor = editor_program;
36 editor = getenv("VISUAL");
38 editor = getenv("EDITOR");
40 terminal = getenv("TERM");
41 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
43 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
44 "Please supply the message using either -m or -F option.\n");
51 memset(&child, 0, sizeof(child));
57 if (run_command(&child))
58 die("There was a problem with the editor %s.", editor);
60 if (strbuf_read_file(buffer, path, 0) < 0)
61 die("could not read message file '%s': %s",
62 path, strerror(errno));
70 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
72 static int show_reference(const char *refname, const unsigned char *sha1,
73 int flag, void *cb_data)
75 struct tag_filter *filter = cb_data;
77 if (!fnmatch(filter->pattern, refname, 0)) {
80 enum object_type type;
85 printf("%s\n", refname);
88 printf("%-15s ", refname);
90 buf = read_sha1_file(sha1, &type, &size);
95 sp = strstr(buf, "\n\n");
100 /* only take up to "lines" lines, and strip the signature */
102 i < filter->lines && sp < buf + size &&
103 prefixcmp(sp, PGP_SIGNATURE "\n");
107 eol = memchr(sp, '\n', size - (sp - buf));
108 len = eol ? eol - sp : size - (sp - buf);
109 fwrite(sp, len, 1, stdout);
121 static int list_tags(const char *pattern, int lines)
123 struct tag_filter filter;
128 filter.pattern = pattern;
129 filter.lines = lines;
131 for_each_tag_ref(show_reference, (void *) &filter);
136 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
137 const unsigned char *sha1);
139 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
144 unsigned char sha1[20];
146 for (p = argv; *p; p++) {
147 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
149 error("tag name too long: %.*s...", 50, *p);
153 if (!resolve_ref(ref, sha1, 1, NULL)) {
154 error("tag '%s' not found.", *p);
158 if (fn(*p, ref, sha1))
164 static int delete_tag(const char *name, const char *ref,
165 const unsigned char *sha1)
167 if (delete_ref(ref, sha1))
169 printf("Deleted tag '%s'\n", name);
173 static int verify_tag(const char *name, const char *ref,
174 const unsigned char *sha1)
176 const char *argv_verify_tag[] = {"git-verify-tag",
177 "-v", "SHA1_HEX", NULL};
178 argv_verify_tag[2] = sha1_to_hex(sha1);
180 if (run_command_v_opt(argv_verify_tag, 0))
181 return error("could not verify the tag '%s'", name);
185 static int do_sign(struct strbuf *buffer)
187 struct child_process gpg;
193 if (strlcpy(signingkey, git_committer_info(1),
194 sizeof(signingkey)) > sizeof(signingkey) - 1)
195 return error("committer info too long.");
196 bracket = strchr(signingkey, '>');
201 /* When the username signingkey is bad, program could be terminated
202 * because gpg exits without reading and then write gets SIGPIPE. */
203 signal(SIGPIPE, SIG_IGN);
205 memset(&gpg, 0, sizeof(gpg));
211 args[2] = signingkey;
214 if (start_command(&gpg))
215 return error("could not run gpg.");
217 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
219 finish_command(&gpg);
220 return error("gpg did not accept the tag data");
224 len = strbuf_read(buffer, gpg.out, 1024);
226 if (finish_command(&gpg) || !len || len < 0)
227 return error("gpg failed to sign the tag");
230 return error("could not read the entire signature from gpg.");
235 static const char tag_template[] =
238 "# Write a tag message\n"
241 static int git_tag_config(const char *var, const char *value)
243 if (!strcmp(var, "user.signingkey")) {
245 die("user.signingkey without value");
246 if (strlcpy(signingkey, value, sizeof(signingkey))
247 >= sizeof(signingkey))
248 die("user.signingkey value too long");
252 return git_default_config(var, value);
255 static void write_tag_body(int fd, const unsigned char *sha1)
258 enum object_type type;
259 char *buf, *sp, *eob;
262 buf = read_sha1_file(sha1, &type, &size);
266 sp = strstr(buf, "\n\n");
268 if (!sp || !size || type != OBJ_TAG) {
272 sp += 2; /* skip the 2 LFs */
273 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
277 len = buf + size - sp;
278 write_or_die(fd, sp, len);
283 static void create_tag(const unsigned char *object, const char *tag,
284 struct strbuf *buf, int message, int sign,
285 unsigned char *prev, unsigned char *result)
287 enum object_type type;
288 char header_buf[1024];
291 type = sha1_object_info(object, NULL);
292 if (type <= OBJ_NONE)
293 die("bad object type.");
295 header_len = snprintf(header_buf, sizeof(header_buf),
303 git_committer_info(1));
305 if (header_len > sizeof(header_buf) - 1)
306 die("tag header too big.");
312 /* write the template message before editing: */
313 path = xstrdup(git_path("TAG_EDITMSG"));
314 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
316 die("could not create file '%s': %s",
317 path, strerror(errno));
319 if (!is_null_sha1(prev))
320 write_tag_body(fd, prev);
322 write_or_die(fd, tag_template, strlen(tag_template));
325 launch_editor(path, buf);
333 if (!message && !buf->len)
334 die("no tag message?");
336 strbuf_insert(buf, 0, header_buf, header_len);
338 if (sign && do_sign(buf) < 0)
339 die("unable to sign the tag");
340 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
341 die("unable to write tag file");
349 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
351 struct msg_arg *msg = opt->value;
356 strbuf_addstr(&(msg->buf), "\n\n");
357 strbuf_addstr(&(msg->buf), arg);
362 int cmd_tag(int argc, const char **argv, const char *prefix)
365 unsigned char object[20], prev[20];
367 const char *object_ref, *tag;
368 struct ref_lock *lock;
370 int annotate = 0, sign = 0, force = 0, lines = 0,
371 delete = 0, verify = 0;
372 char *list = NULL, *msgfile = NULL, *keyid = NULL;
373 const char *no_pattern = "NO_PATTERN";
374 struct msg_arg msg = { 0, STRBUF_INIT };
375 struct option options[] = {
376 { OPTION_STRING, 'l', NULL, &list, "pattern", "list tag names",
377 PARSE_OPT_OPTARG, NULL, (intptr_t) no_pattern },
378 { OPTION_INTEGER, 'n', NULL, &lines, NULL,
379 "print n lines of each tag message",
380 PARSE_OPT_OPTARG, NULL, 1 },
381 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
382 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
384 OPT_GROUP("Tag creation options"),
385 OPT_BOOLEAN('a', NULL, &annotate,
386 "annotated tag, needs a message"),
387 OPT_CALLBACK('m', NULL, &msg, "msg",
388 "message for the tag", parse_msg_arg),
389 OPT_STRING('F', NULL, &msgfile, "file", "message in a file"),
390 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
391 OPT_STRING('u', NULL, &keyid, "key-id",
392 "use another key to sign the tag"),
393 OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
397 git_config(git_tag_config);
399 argc = parse_options(argc, argv, options, git_tag_usage, 0);
405 return list_tags(list == no_pattern ? NULL : list, lines);
407 return for_each_tag_name(argv, delete_tag);
409 return for_each_tag_name(argv, verify_tag);
411 strbuf_init(&buf, 0);
412 if (msg.given || msgfile) {
413 if (msg.given && msgfile)
414 die("only one -F or -m option is allowed.");
417 strbuf_addbuf(&buf, &(msg.buf));
419 if (!strcmp(msgfile, "-")) {
420 if (strbuf_read(&buf, 0, 1024) < 0)
421 die("cannot read %s", msgfile);
423 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
424 die("could not open or read '%s': %s",
425 msgfile, strerror(errno));
432 usage_with_options(git_tag_usage, options);
433 return list_tags(NULL, lines);
437 object_ref = argc == 2 ? argv[1] : "HEAD";
439 die("too many params");
441 if (get_sha1(object_ref, object))
442 die("Failed to resolve '%s' as a valid ref.", object_ref);
444 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
445 die("tag name too long: %.*s...", 50, tag);
446 if (check_ref_format(ref))
447 die("'%s' is not a valid tag name.", tag);
449 if (!resolve_ref(ref, prev, 1, NULL))
452 die("tag '%s' already exists", tag);
455 create_tag(object, tag, &buf, msg.given || msgfile,
458 lock = lock_any_ref_for_update(ref, prev, 0);
460 die("%s: cannot lock the ref", ref);
461 if (write_ref_sha1(lock, object, NULL) < 0)
462 die("%s: cannot update the ref", ref);
464 strbuf_release(&buf);