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"
15 static const char builtin_tag_usage[] =
16 "git-tag [-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg> | -F <file>] <tagname> [<head>]";
18 static char signingkey[1000];
20 static void launch_editor(const char *path, struct strbuf *buffer)
22 const char *editor, *terminal;
23 struct child_process child;
27 editor = getenv("GIT_EDITOR");
28 if (!editor && editor_program)
29 editor = editor_program;
31 editor = getenv("VISUAL");
33 editor = getenv("EDITOR");
35 terminal = getenv("TERM");
36 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
38 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
39 "Please supply the message using either -m or -F option.\n");
46 memset(&child, 0, sizeof(child));
52 if (run_command(&child))
53 die("There was a problem with the editor %s.", editor);
55 fd = open(path, O_RDONLY);
57 die("could not open '%s': %s", path, strerror(errno));
58 if (strbuf_read(buffer, fd, 0) < 0) {
59 die("could not read message file '%s': %s", path, strerror(errno));
69 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
71 static int show_reference(const char *refname, const unsigned char *sha1,
72 int flag, void *cb_data)
74 struct tag_filter *filter = cb_data;
76 if (!fnmatch(filter->pattern, refname, 0)) {
79 enum object_type type;
84 printf("%s\n", refname);
87 printf("%-15s ", refname);
89 sp = buf = read_sha1_file(sha1, &type, &size);
97 while (sp + 1 < buf + size &&
98 !(sp[0] == '\n' && sp[1] == '\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 create_tag(const unsigned char *object, const char *tag,
256 struct strbuf *buf, int message, int sign,
257 unsigned char *result)
259 enum object_type type;
260 char header_buf[1024];
263 type = sha1_object_info(object, NULL);
264 if (type <= OBJ_NONE)
265 die("bad object type.");
267 header_len = snprintf(header_buf, sizeof(header_buf),
275 git_committer_info(1));
277 if (header_len > sizeof(header_buf) - 1)
278 die("tag header too big.");
284 /* write the template message before editing: */
285 path = xstrdup(git_path("TAG_EDITMSG"));
286 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
288 die("could not create file '%s': %s",
289 path, strerror(errno));
290 write_or_die(fd, tag_template, strlen(tag_template));
293 launch_editor(path, buf);
299 strbuf_setlen(buf, stripspace(buf->buf, buf->len, 1));
301 if (!message && !buf->len)
302 die("no tag message?");
304 /* insert the header and add the '\n' if needed: */
306 strbuf_addch(buf, '\n');
307 strbuf_insert(buf, 0, header_buf, header_len);
309 if (sign && do_sign(buf) < 0)
310 die("unable to sign the tag");
311 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
312 die("unable to write tag file");
315 int cmd_tag(int argc, const char **argv, const char *prefix)
318 unsigned char object[20], prev[20];
319 int annotate = 0, sign = 0, force = 0, lines = 0, message = 0;
321 const char *object_ref, *tag;
323 struct ref_lock *lock;
325 git_config(git_tag_config);
326 strbuf_init(&buf, 0);
328 for (i = 1; i < argc; i++) {
329 const char *arg = argv[i];
333 if (!strcmp(arg, "-a")) {
337 if (!strcmp(arg, "-s")) {
342 if (!strcmp(arg, "-f")) {
346 if (!strcmp(arg, "-n")) {
347 if (i + 1 == argc || *argv[i + 1] == '-')
351 lines = isdigit(*argv[++i]) ?
355 if (!strcmp(arg, "-m")) {
359 die("option -m needs an argument.");
361 die("only one -F or -m option is allowed.");
362 strbuf_addstr(&buf, argv[i]);
366 if (!strcmp(arg, "-F")) {
372 die("option -F needs an argument.");
374 die("only one -F or -m option is allowed.");
376 if (!strcmp(argv[i], "-"))
379 fd = open(argv[i], O_RDONLY);
381 die("could not open '%s': %s",
382 argv[i], strerror(errno));
384 if (strbuf_read(&buf, fd, 1024) < 0) {
385 die("cannot read %s", argv[i]);
390 if (!strcmp(arg, "-u")) {
395 die("option -u needs an argument.");
396 if (strlcpy(signingkey, argv[i], sizeof(signingkey))
397 >= sizeof(signingkey))
398 die("argument to option -u too long");
401 if (!strcmp(arg, "-l"))
402 return list_tags(argv[i + 1], lines);
403 if (!strcmp(arg, "-d"))
404 return for_each_tag_name(argv + i + 1, delete_tag);
405 if (!strcmp(arg, "-v"))
406 return for_each_tag_name(argv + i + 1, verify_tag);
407 usage(builtin_tag_usage);
412 usage(builtin_tag_usage);
413 return list_tags(NULL, lines);
417 object_ref = i < argc ? argv[i] : "HEAD";
419 die("too many params");
421 if (get_sha1(object_ref, object))
422 die("Failed to resolve '%s' as a valid ref.", object_ref);
424 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
425 die("tag name too long: %.*s...", 50, tag);
426 if (check_ref_format(ref))
427 die("'%s' is not a valid tag name.", tag);
429 if (!resolve_ref(ref, prev, 1, NULL))
432 die("tag '%s' already exists", tag);
435 create_tag(object, tag, &buf, message, sign, object);
437 lock = lock_any_ref_for_update(ref, prev, 0);
439 die("%s: cannot lock the ref", ref);
440 if (write_ref_sha1(lock, object, NULL) < 0)
441 die("%s: cannot update the ref", ref);
443 strbuf_release(&buf);