Merge branch 'maint'
[git/platforms.git] / builtin-tag.c
blobc2cca6cb6dc46a221c65092d7c419445d6442b5f
1 /*
2 * Builtin "git tag"
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.
7 */
9 #include "cache.h"
10 #include "builtin.h"
11 #include "refs.h"
12 #include "tag.h"
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>...",
21 NULL
24 static char signingkey[1000];
26 void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
28 const char *editor, *terminal;
30 editor = getenv("GIT_EDITOR");
31 if (!editor && editor_program)
32 editor = editor_program;
33 if (!editor)
34 editor = getenv("VISUAL");
35 if (!editor)
36 editor = getenv("EDITOR");
38 terminal = getenv("TERM");
39 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
40 fprintf(stderr,
41 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
42 "Please supply the message using either -m or -F option.\n");
43 exit(1);
46 if (!editor)
47 editor = "vi";
49 if (strcmp(editor, ":")) {
50 size_t len = strlen(editor);
51 int i = 0;
52 const char *args[6];
53 struct strbuf arg0;
55 strbuf_init(&arg0, 0);
56 if (strcspn(editor, "$ \t'") != len) {
57 /* there are specials */
58 strbuf_addf(&arg0, "%s \"$@\"", editor);
59 args[i++] = "sh";
60 args[i++] = "-c";
61 args[i++] = arg0.buf;
63 args[i++] = editor;
64 args[i++] = path;
65 args[i] = NULL;
67 if (run_command_v_opt_cd_env(args, 0, NULL, env))
68 die("There was a problem with the editor %s.", editor);
69 strbuf_release(&arg0);
72 if (!buffer)
73 return;
74 if (strbuf_read_file(buffer, path, 0) < 0)
75 die("could not read message file '%s': %s",
76 path, strerror(errno));
79 struct tag_filter {
80 const char *pattern;
81 int lines;
84 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
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 (!fnmatch(filter->pattern, refname, 0)) {
92 int i;
93 unsigned long size;
94 enum object_type type;
95 char *buf, *sp, *eol;
96 size_t len;
98 if (!filter->lines) {
99 printf("%s\n", refname);
100 return 0;
102 printf("%-15s ", refname);
104 buf = read_sha1_file(sha1, &type, &size);
105 if (!buf || !size)
106 return 0;
108 /* skip header */
109 sp = strstr(buf, "\n\n");
110 if (!sp) {
111 free(buf);
112 return 0;
114 /* only take up to "lines" lines, and strip the signature */
115 for (i = 0, sp += 2;
116 i < filter->lines && sp < buf + size &&
117 prefixcmp(sp, PGP_SIGNATURE "\n");
118 i++) {
119 if (i)
120 printf("\n ");
121 eol = memchr(sp, '\n', size - (sp - buf));
122 len = eol ? eol - sp : size - (sp - buf);
123 fwrite(sp, len, 1, stdout);
124 if (!eol)
125 break;
126 sp = eol + 1;
128 putchar('\n');
129 free(buf);
132 return 0;
135 static int list_tags(const char *pattern, int lines)
137 struct tag_filter filter;
139 if (pattern == NULL)
140 pattern = "*";
142 filter.pattern = pattern;
143 filter.lines = lines;
145 for_each_tag_ref(show_reference, (void *) &filter);
147 return 0;
150 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
151 const unsigned char *sha1);
153 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
155 const char **p;
156 char ref[PATH_MAX];
157 int had_error = 0;
158 unsigned char sha1[20];
160 for (p = argv; *p; p++) {
161 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
162 >= sizeof(ref)) {
163 error("tag name too long: %.*s...", 50, *p);
164 had_error = 1;
165 continue;
167 if (!resolve_ref(ref, sha1, 1, NULL)) {
168 error("tag '%s' not found.", *p);
169 had_error = 1;
170 continue;
172 if (fn(*p, ref, sha1))
173 had_error = 1;
175 return had_error;
178 static int delete_tag(const char *name, const char *ref,
179 const unsigned char *sha1)
181 if (delete_ref(ref, sha1))
182 return 1;
183 printf("Deleted tag '%s'\n", name);
184 return 0;
187 static int verify_tag(const char *name, const char *ref,
188 const unsigned char *sha1)
190 const char *argv_verify_tag[] = {"git-verify-tag",
191 "-v", "SHA1_HEX", NULL};
192 argv_verify_tag[2] = sha1_to_hex(sha1);
194 if (run_command_v_opt(argv_verify_tag, 0))
195 return error("could not verify the tag '%s'", name);
196 return 0;
199 static int do_sign(struct strbuf *buffer)
201 struct child_process gpg;
202 const char *args[4];
203 char *bracket;
204 int len;
205 int i, j;
207 if (!*signingkey) {
208 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
209 sizeof(signingkey)) > sizeof(signingkey) - 1)
210 return error("committer info too long.");
211 bracket = strchr(signingkey, '>');
212 if (bracket)
213 bracket[1] = '\0';
216 /* When the username signingkey is bad, program could be terminated
217 * because gpg exits without reading and then write gets SIGPIPE. */
218 signal(SIGPIPE, SIG_IGN);
220 memset(&gpg, 0, sizeof(gpg));
221 gpg.argv = args;
222 gpg.in = -1;
223 gpg.out = -1;
224 args[0] = "gpg";
225 args[1] = "-bsau";
226 args[2] = signingkey;
227 args[3] = NULL;
229 if (start_command(&gpg))
230 return error("could not run gpg.");
232 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
233 close(gpg.in);
234 close(gpg.out);
235 finish_command(&gpg);
236 return error("gpg did not accept the tag data");
238 close(gpg.in);
239 len = strbuf_read(buffer, gpg.out, 1024);
240 close(gpg.out);
242 if (finish_command(&gpg) || !len || len < 0)
243 return error("gpg failed to sign the tag");
245 /* Strip CR from the line endings, in case we are on Windows. */
246 for (i = j = 0; i < buffer->len; i++)
247 if (buffer->buf[i] != '\r') {
248 if (i != j)
249 buffer->buf[j] = buffer->buf[i];
250 j++;
252 strbuf_setlen(buffer, j);
254 return 0;
257 static const char tag_template[] =
258 "\n"
259 "#\n"
260 "# Write a tag message\n"
261 "#\n";
263 static void set_signingkey(const char *value)
265 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
266 die("signing key value too long (%.10s...)", value);
269 static int git_tag_config(const char *var, const char *value, void *cb)
271 if (!strcmp(var, "user.signingkey")) {
272 if (!value)
273 return config_error_nonbool(var);
274 set_signingkey(value);
275 return 0;
278 return git_default_config(var, value, cb);
281 static void write_tag_body(int fd, const unsigned char *sha1)
283 unsigned long size;
284 enum object_type type;
285 char *buf, *sp, *eob;
286 size_t len;
288 buf = read_sha1_file(sha1, &type, &size);
289 if (!buf)
290 return;
291 /* skip header */
292 sp = strstr(buf, "\n\n");
294 if (!sp || !size || type != OBJ_TAG) {
295 free(buf);
296 return;
298 sp += 2; /* skip the 2 LFs */
299 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
300 if (eob)
301 len = eob - sp;
302 else
303 len = buf + size - sp;
304 write_or_die(fd, sp, len);
306 free(buf);
309 static void create_tag(const unsigned char *object, const char *tag,
310 struct strbuf *buf, int message, int sign,
311 unsigned char *prev, unsigned char *result)
313 enum object_type type;
314 char header_buf[1024];
315 int header_len;
317 type = sha1_object_info(object, NULL);
318 if (type <= OBJ_NONE)
319 die("bad object type.");
321 header_len = snprintf(header_buf, sizeof(header_buf),
322 "object %s\n"
323 "type %s\n"
324 "tag %s\n"
325 "tagger %s\n\n",
326 sha1_to_hex(object),
327 typename(type),
328 tag,
329 git_committer_info(IDENT_ERROR_ON_NO_NAME));
331 if (header_len > sizeof(header_buf) - 1)
332 die("tag header too big.");
334 if (!message) {
335 char *path;
336 int fd;
338 /* write the template message before editing: */
339 path = xstrdup(git_path("TAG_EDITMSG"));
340 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
341 if (fd < 0)
342 die("could not create file '%s': %s",
343 path, strerror(errno));
345 if (!is_null_sha1(prev))
346 write_tag_body(fd, prev);
347 else
348 write_or_die(fd, tag_template, strlen(tag_template));
349 close(fd);
351 launch_editor(path, buf, NULL);
353 unlink(path);
354 free(path);
357 stripspace(buf, 1);
359 if (!message && !buf->len)
360 die("no tag message?");
362 strbuf_insert(buf, 0, header_buf, header_len);
364 if (sign && do_sign(buf) < 0)
365 die("unable to sign the tag");
366 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
367 die("unable to write tag file");
370 struct msg_arg {
371 int given;
372 struct strbuf buf;
375 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
377 struct msg_arg *msg = opt->value;
379 if (!arg)
380 return -1;
381 if (msg->buf.len)
382 strbuf_addstr(&(msg->buf), "\n\n");
383 strbuf_addstr(&(msg->buf), arg);
384 msg->given = 1;
385 return 0;
388 int cmd_tag(int argc, const char **argv, const char *prefix)
390 struct strbuf buf;
391 unsigned char object[20], prev[20];
392 char ref[PATH_MAX];
393 const char *object_ref, *tag;
394 struct ref_lock *lock;
396 int annotate = 0, sign = 0, force = 0, lines = 0,
397 list = 0, delete = 0, verify = 0;
398 char *msgfile = NULL, *keyid = NULL;
399 struct msg_arg msg = { 0, STRBUF_INIT };
400 struct option options[] = {
401 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
402 { OPTION_INTEGER, 'n', NULL, &lines, NULL,
403 "print n lines of each tag message",
404 PARSE_OPT_OPTARG, NULL, 1 },
405 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
406 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
408 OPT_GROUP("Tag creation options"),
409 OPT_BOOLEAN('a', NULL, &annotate,
410 "annotated tag, needs a message"),
411 OPT_CALLBACK('m', NULL, &msg, "msg",
412 "message for the tag", parse_msg_arg),
413 OPT_STRING('F', NULL, &msgfile, "file", "message in a file"),
414 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
415 OPT_STRING('u', NULL, &keyid, "key-id",
416 "use another key to sign the tag"),
417 OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
418 OPT_END()
421 git_config(git_tag_config, NULL);
423 argc = parse_options(argc, argv, options, git_tag_usage, 0);
425 if (keyid) {
426 sign = 1;
427 set_signingkey(keyid);
429 if (sign)
430 annotate = 1;
432 if (list)
433 return list_tags(argv[0], lines);
434 if (delete)
435 return for_each_tag_name(argv, delete_tag);
436 if (verify)
437 return for_each_tag_name(argv, verify_tag);
439 strbuf_init(&buf, 0);
440 if (msg.given || msgfile) {
441 if (msg.given && msgfile)
442 die("only one -F or -m option is allowed.");
443 annotate = 1;
444 if (msg.given)
445 strbuf_addbuf(&buf, &(msg.buf));
446 else {
447 if (!strcmp(msgfile, "-")) {
448 if (strbuf_read(&buf, 0, 1024) < 0)
449 die("cannot read %s", msgfile);
450 } else {
451 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
452 die("could not open or read '%s': %s",
453 msgfile, strerror(errno));
458 if (argc == 0) {
459 if (annotate)
460 usage_with_options(git_tag_usage, options);
461 return list_tags(NULL, lines);
463 tag = argv[0];
465 object_ref = argc == 2 ? argv[1] : "HEAD";
466 if (argc > 2)
467 die("too many params");
469 if (get_sha1(object_ref, object))
470 die("Failed to resolve '%s' as a valid ref.", object_ref);
472 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
473 die("tag name too long: %.*s...", 50, tag);
474 if (check_ref_format(ref))
475 die("'%s' is not a valid tag name.", tag);
477 if (!resolve_ref(ref, prev, 1, NULL))
478 hashclr(prev);
479 else if (!force)
480 die("tag '%s' already exists", tag);
482 if (annotate)
483 create_tag(object, tag, &buf, msg.given || msgfile,
484 sign, prev, object);
486 lock = lock_any_ref_for_update(ref, prev, 0);
487 if (!lock)
488 die("%s: cannot lock the ref", ref);
489 if (write_ref_sha1(lock, object, NULL) < 0)
490 die("%s: cannot update the ref", ref);
492 strbuf_release(&buf);
493 return 0;