Merge branch 'master' of git://repo.or.cz/git-gui
[git/peff.git] / builtin-tag.c
blob729389bbd6120e4b395fbc6f916f8d13965191d6
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 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 const char *args[] = { editor, path, NULL };
52 if (run_command_v_opt_cd_env(args, 0, NULL, env))
53 die("There was a problem with the editor %s.", editor);
56 if (strbuf_read_file(buffer, path, 0) < 0)
57 die("could not read message file '%s': %s",
58 path, strerror(errno));
61 struct tag_filter {
62 const char *pattern;
63 int lines;
66 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
68 static int show_reference(const char *refname, const unsigned char *sha1,
69 int flag, void *cb_data)
71 struct tag_filter *filter = cb_data;
73 if (!fnmatch(filter->pattern, refname, 0)) {
74 int i;
75 unsigned long size;
76 enum object_type type;
77 char *buf, *sp, *eol;
78 size_t len;
80 if (!filter->lines) {
81 printf("%s\n", refname);
82 return 0;
84 printf("%-15s ", refname);
86 buf = read_sha1_file(sha1, &type, &size);
87 if (!buf || !size)
88 return 0;
90 /* skip header */
91 sp = strstr(buf, "\n\n");
92 if (!sp) {
93 free(buf);
94 return 0;
96 /* only take up to "lines" lines, and strip the signature */
97 for (i = 0, sp += 2;
98 i < filter->lines && sp < buf + size &&
99 prefixcmp(sp, PGP_SIGNATURE "\n");
100 i++) {
101 if (i)
102 printf("\n ");
103 eol = memchr(sp, '\n', size - (sp - buf));
104 len = eol ? eol - sp : size - (sp - buf);
105 fwrite(sp, len, 1, stdout);
106 if (!eol)
107 break;
108 sp = eol + 1;
110 putchar('\n');
111 free(buf);
114 return 0;
117 static int list_tags(const char *pattern, int lines)
119 struct tag_filter filter;
121 if (pattern == NULL)
122 pattern = "*";
124 filter.pattern = pattern;
125 filter.lines = lines;
127 for_each_tag_ref(show_reference, (void *) &filter);
129 return 0;
132 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
133 const unsigned char *sha1);
135 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
137 const char **p;
138 char ref[PATH_MAX];
139 int had_error = 0;
140 unsigned char sha1[20];
142 for (p = argv; *p; p++) {
143 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
144 >= sizeof(ref)) {
145 error("tag name too long: %.*s...", 50, *p);
146 had_error = 1;
147 continue;
149 if (!resolve_ref(ref, sha1, 1, NULL)) {
150 error("tag '%s' not found.", *p);
151 had_error = 1;
152 continue;
154 if (fn(*p, ref, sha1))
155 had_error = 1;
157 return had_error;
160 static int delete_tag(const char *name, const char *ref,
161 const unsigned char *sha1)
163 if (delete_ref(ref, sha1))
164 return 1;
165 printf("Deleted tag '%s'\n", name);
166 return 0;
169 static int verify_tag(const char *name, const char *ref,
170 const unsigned char *sha1)
172 const char *argv_verify_tag[] = {"git-verify-tag",
173 "-v", "SHA1_HEX", NULL};
174 argv_verify_tag[2] = sha1_to_hex(sha1);
176 if (run_command_v_opt(argv_verify_tag, 0))
177 return error("could not verify the tag '%s'", name);
178 return 0;
181 static int do_sign(struct strbuf *buffer)
183 struct child_process gpg;
184 const char *args[4];
185 char *bracket;
186 int len;
188 if (!*signingkey) {
189 if (strlcpy(signingkey, git_committer_info(1),
190 sizeof(signingkey)) > sizeof(signingkey) - 1)
191 return error("committer info too long.");
192 bracket = strchr(signingkey, '>');
193 if (bracket)
194 bracket[1] = '\0';
197 /* When the username signingkey is bad, program could be terminated
198 * because gpg exits without reading and then write gets SIGPIPE. */
199 signal(SIGPIPE, SIG_IGN);
201 memset(&gpg, 0, sizeof(gpg));
202 gpg.argv = args;
203 gpg.in = -1;
204 gpg.out = -1;
205 args[0] = "gpg";
206 args[1] = "-bsau";
207 args[2] = signingkey;
208 args[3] = NULL;
210 if (start_command(&gpg))
211 return error("could not run gpg.");
213 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
214 close(gpg.in);
215 finish_command(&gpg);
216 return error("gpg did not accept the tag data");
218 close(gpg.in);
219 gpg.close_in = 0;
220 len = strbuf_read(buffer, gpg.out, 1024);
222 if (finish_command(&gpg) || !len || len < 0)
223 return error("gpg failed to sign the tag");
225 if (len < 0)
226 return error("could not read the entire signature from gpg.");
228 return 0;
231 static const char tag_template[] =
232 "\n"
233 "#\n"
234 "# Write a tag message\n"
235 "#\n";
237 static int git_tag_config(const char *var, const char *value)
239 if (!strcmp(var, "user.signingkey")) {
240 if (!value)
241 die("user.signingkey without value");
242 if (strlcpy(signingkey, value, sizeof(signingkey))
243 >= sizeof(signingkey))
244 die("user.signingkey value too long");
245 return 0;
248 return git_default_config(var, value);
251 static void write_tag_body(int fd, const unsigned char *sha1)
253 unsigned long size;
254 enum object_type type;
255 char *buf, *sp, *eob;
256 size_t len;
258 buf = read_sha1_file(sha1, &type, &size);
259 if (!buf)
260 return;
261 /* skip header */
262 sp = strstr(buf, "\n\n");
264 if (!sp || !size || type != OBJ_TAG) {
265 free(buf);
266 return;
268 sp += 2; /* skip the 2 LFs */
269 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
270 if (eob)
271 len = eob - sp;
272 else
273 len = buf + size - sp;
274 write_or_die(fd, sp, len);
276 free(buf);
279 static void create_tag(const unsigned char *object, const char *tag,
280 struct strbuf *buf, int message, int sign,
281 unsigned char *prev, unsigned char *result)
283 enum object_type type;
284 char header_buf[1024];
285 int header_len;
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),
292 "object %s\n"
293 "type %s\n"
294 "tag %s\n"
295 "tagger %s\n\n",
296 sha1_to_hex(object),
297 typename(type),
298 tag,
299 git_committer_info(1));
301 if (header_len > sizeof(header_buf) - 1)
302 die("tag header too big.");
304 if (!message) {
305 char *path;
306 int fd;
308 /* write the template message before editing: */
309 path = xstrdup(git_path("TAG_EDITMSG"));
310 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
311 if (fd < 0)
312 die("could not create file '%s': %s",
313 path, strerror(errno));
315 if (!is_null_sha1(prev))
316 write_tag_body(fd, prev);
317 else
318 write_or_die(fd, tag_template, strlen(tag_template));
319 close(fd);
321 launch_editor(path, buf, NULL);
323 unlink(path);
324 free(path);
327 stripspace(buf, 1);
329 if (!message && !buf->len)
330 die("no tag message?");
332 strbuf_insert(buf, 0, header_buf, header_len);
334 if (sign && do_sign(buf) < 0)
335 die("unable to sign the tag");
336 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
337 die("unable to write tag file");
340 struct msg_arg {
341 int given;
342 struct strbuf buf;
345 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
347 struct msg_arg *msg = opt->value;
349 if (!arg)
350 return -1;
351 if (msg->buf.len)
352 strbuf_addstr(&(msg->buf), "\n\n");
353 strbuf_addstr(&(msg->buf), arg);
354 msg->given = 1;
355 return 0;
358 int cmd_tag(int argc, const char **argv, const char *prefix)
360 struct strbuf buf;
361 unsigned char object[20], prev[20];
362 char ref[PATH_MAX];
363 const char *object_ref, *tag;
364 struct ref_lock *lock;
366 int annotate = 0, sign = 0, force = 0, lines = 0,
367 delete = 0, verify = 0;
368 char *list = NULL, *msgfile = NULL, *keyid = NULL;
369 const char *no_pattern = "NO_PATTERN";
370 struct msg_arg msg = { 0, STRBUF_INIT };
371 struct option options[] = {
372 { OPTION_STRING, 'l', NULL, &list, "pattern", "list tag names",
373 PARSE_OPT_OPTARG, NULL, (intptr_t) no_pattern },
374 { OPTION_INTEGER, 'n', NULL, &lines, NULL,
375 "print n lines of each tag message",
376 PARSE_OPT_OPTARG, NULL, 1 },
377 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
378 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
380 OPT_GROUP("Tag creation options"),
381 OPT_BOOLEAN('a', NULL, &annotate,
382 "annotated tag, needs a message"),
383 OPT_CALLBACK('m', NULL, &msg, "msg",
384 "message for the tag", parse_msg_arg),
385 OPT_STRING('F', NULL, &msgfile, "file", "message in a file"),
386 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
387 OPT_STRING('u', NULL, &keyid, "key-id",
388 "use another key to sign the tag"),
389 OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
390 OPT_END()
393 git_config(git_tag_config);
395 argc = parse_options(argc, argv, options, git_tag_usage, 0);
397 if (sign)
398 annotate = 1;
400 if (list)
401 return list_tags(list == no_pattern ? NULL : list, lines);
402 if (delete)
403 return for_each_tag_name(argv, delete_tag);
404 if (verify)
405 return for_each_tag_name(argv, verify_tag);
407 strbuf_init(&buf, 0);
408 if (msg.given || msgfile) {
409 if (msg.given && msgfile)
410 die("only one -F or -m option is allowed.");
411 annotate = 1;
412 if (msg.given)
413 strbuf_addbuf(&buf, &(msg.buf));
414 else {
415 if (!strcmp(msgfile, "-")) {
416 if (strbuf_read(&buf, 0, 1024) < 0)
417 die("cannot read %s", msgfile);
418 } else {
419 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
420 die("could not open or read '%s': %s",
421 msgfile, strerror(errno));
426 if (argc == 0) {
427 if (annotate)
428 usage_with_options(git_tag_usage, options);
429 return list_tags(NULL, lines);
431 tag = argv[0];
433 object_ref = argc == 2 ? argv[1] : "HEAD";
434 if (argc > 2)
435 die("too many params");
437 if (get_sha1(object_ref, object))
438 die("Failed to resolve '%s' as a valid ref.", object_ref);
440 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
441 die("tag name too long: %.*s...", 50, tag);
442 if (check_ref_format(ref))
443 die("'%s' is not a valid tag name.", tag);
445 if (!resolve_ref(ref, prev, 1, NULL))
446 hashclr(prev);
447 else if (!force)
448 die("tag '%s' already exists", tag);
450 if (annotate)
451 create_tag(object, tag, &buf, msg.given || msgfile,
452 sign, prev, object);
454 lock = lock_any_ref_for_update(ref, prev, 0);
455 if (!lock)
456 die("%s: cannot lock the ref", ref);
457 if (write_ref_sha1(lock, object, NULL) < 0)
458 die("%s: cannot update the ref", ref);
460 strbuf_release(&buf);
461 return 0;