git-commit --allow-empty
[alt-git.git] / builtin-tag.c
blob114c684d246e975852bb5446ea2e7ab682ea036c
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 [-n [<num>]] -l [<pattern>]",
20 "git-tag -v <tagname>...",
21 NULL
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;
30 const char *args[3];
32 editor = getenv("GIT_EDITOR");
33 if (!editor && editor_program)
34 editor = editor_program;
35 if (!editor)
36 editor = getenv("VISUAL");
37 if (!editor)
38 editor = getenv("EDITOR");
40 terminal = getenv("TERM");
41 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
42 fprintf(stderr,
43 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
44 "Please supply the message using either -m or -F option.\n");
45 exit(1);
48 if (!editor)
49 editor = "vi";
51 memset(&child, 0, sizeof(child));
52 child.argv = args;
53 args[0] = editor;
54 args[1] = path;
55 args[2] = NULL;
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));
65 struct tag_filter {
66 const char *pattern;
67 int lines;
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)) {
78 int i;
79 unsigned long size;
80 enum object_type type;
81 char *buf, *sp, *eol;
82 size_t len;
84 if (!filter->lines) {
85 printf("%s\n", refname);
86 return 0;
88 printf("%-15s ", refname);
90 buf = read_sha1_file(sha1, &type, &size);
91 if (!buf || !size)
92 return 0;
94 /* skip header */
95 sp = strstr(buf, "\n\n");
96 if (!sp) {
97 free(buf);
98 return 0;
100 /* only take up to "lines" lines, and strip the signature */
101 for (i = 0, sp += 2;
102 i < filter->lines && sp < buf + size &&
103 prefixcmp(sp, PGP_SIGNATURE "\n");
104 i++) {
105 if (i)
106 printf("\n ");
107 eol = memchr(sp, '\n', size - (sp - buf));
108 len = eol ? eol - sp : size - (sp - buf);
109 fwrite(sp, len, 1, stdout);
110 if (!eol)
111 break;
112 sp = eol + 1;
114 putchar('\n');
115 free(buf);
118 return 0;
121 static int list_tags(const char *pattern, int lines)
123 struct tag_filter filter;
125 if (pattern == NULL)
126 pattern = "*";
128 filter.pattern = pattern;
129 filter.lines = lines;
131 for_each_tag_ref(show_reference, (void *) &filter);
133 return 0;
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)
141 const char **p;
142 char ref[PATH_MAX];
143 int had_error = 0;
144 unsigned char sha1[20];
146 for (p = argv; *p; p++) {
147 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
148 >= sizeof(ref)) {
149 error("tag name too long: %.*s...", 50, *p);
150 had_error = 1;
151 continue;
153 if (!resolve_ref(ref, sha1, 1, NULL)) {
154 error("tag '%s' not found.", *p);
155 had_error = 1;
156 continue;
158 if (fn(*p, ref, sha1))
159 had_error = 1;
161 return had_error;
164 static int delete_tag(const char *name, const char *ref,
165 const unsigned char *sha1)
167 if (delete_ref(ref, sha1))
168 return 1;
169 printf("Deleted tag '%s'\n", name);
170 return 0;
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);
182 return 0;
185 static int do_sign(struct strbuf *buffer)
187 struct child_process gpg;
188 const char *args[4];
189 char *bracket;
190 int len;
192 if (!*signingkey) {
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, '>');
197 if (bracket)
198 bracket[1] = '\0';
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));
206 gpg.argv = args;
207 gpg.in = -1;
208 gpg.out = -1;
209 args[0] = "gpg";
210 args[1] = "-bsau";
211 args[2] = signingkey;
212 args[3] = NULL;
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) {
218 close(gpg.in);
219 finish_command(&gpg);
220 return error("gpg did not accept the tag data");
222 close(gpg.in);
223 gpg.close_in = 0;
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");
229 if (len < 0)
230 return error("could not read the entire signature from gpg.");
232 return 0;
235 static const char tag_template[] =
236 "\n"
237 "#\n"
238 "# Write a tag message\n"
239 "#\n";
241 static int git_tag_config(const char *var, const char *value)
243 if (!strcmp(var, "user.signingkey")) {
244 if (!value)
245 die("user.signingkey without value");
246 if (strlcpy(signingkey, value, sizeof(signingkey))
247 >= sizeof(signingkey))
248 die("user.signingkey value too long");
249 return 0;
252 return git_default_config(var, value);
255 static void write_tag_body(int fd, const unsigned char *sha1)
257 unsigned long size;
258 enum object_type type;
259 char *buf, *sp, *eob;
260 size_t len;
262 buf = read_sha1_file(sha1, &type, &size);
263 if (!buf)
264 return;
265 /* skip header */
266 sp = strstr(buf, "\n\n");
268 if (!sp || !size || type != OBJ_TAG) {
269 free(buf);
270 return;
272 sp += 2; /* skip the 2 LFs */
273 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
274 if (eob)
275 len = eob - sp;
276 else
277 len = buf + size - sp;
278 write_or_die(fd, sp, len);
280 free(buf);
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];
289 int header_len;
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),
296 "object %s\n"
297 "type %s\n"
298 "tag %s\n"
299 "tagger %s\n\n",
300 sha1_to_hex(object),
301 typename(type),
302 tag,
303 git_committer_info(1));
305 if (header_len > sizeof(header_buf) - 1)
306 die("tag header too big.");
308 if (!message) {
309 char *path;
310 int fd;
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);
315 if (fd < 0)
316 die("could not create file '%s': %s",
317 path, strerror(errno));
319 if (!is_null_sha1(prev))
320 write_tag_body(fd, prev);
321 else
322 write_or_die(fd, tag_template, strlen(tag_template));
323 close(fd);
325 launch_editor(path, buf);
327 unlink(path);
328 free(path);
331 stripspace(buf, 1);
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");
344 struct msg_arg {
345 int given;
346 struct strbuf buf;
349 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
351 struct msg_arg *msg = opt->value;
353 if (!arg)
354 return -1;
355 if (msg->buf.len)
356 strbuf_addstr(&(msg->buf), "\n\n");
357 strbuf_addstr(&(msg->buf), arg);
358 msg->given = 1;
359 return 0;
362 int cmd_tag(int argc, const char **argv, const char *prefix)
364 struct strbuf buf;
365 unsigned char object[20], prev[20];
366 char ref[PATH_MAX];
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"),
394 OPT_END()
397 git_config(git_tag_config);
399 argc = parse_options(argc, argv, options, git_tag_usage, 0);
401 if (sign)
402 annotate = 1;
404 if (list)
405 return list_tags(list == no_pattern ? NULL : list, lines);
406 if (delete)
407 return for_each_tag_name(argv, delete_tag);
408 if (verify)
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.");
415 annotate = 1;
416 if (msg.given)
417 strbuf_addbuf(&buf, &(msg.buf));
418 else {
419 if (!strcmp(msgfile, "-")) {
420 if (strbuf_read(&buf, 0, 1024) < 0)
421 die("cannot read %s", msgfile);
422 } else {
423 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
424 die("could not open or read '%s': %s",
425 msgfile, strerror(errno));
430 if (argc == 0) {
431 if (annotate)
432 usage_with_options(git_tag_usage, options);
433 return list_tags(NULL, lines);
435 tag = argv[0];
437 object_ref = argc == 2 ? argv[1] : "HEAD";
438 if (argc > 2)
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))
450 hashclr(prev);
451 else if (!force)
452 die("tag '%s' already exists", tag);
454 if (annotate)
455 create_tag(object, tag, &buf, msg.given || msgfile,
456 sign, prev, object);
458 lock = lock_any_ref_for_update(ref, prev, 0);
459 if (!lock)
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);
465 return 0;