Export launch_editor() and make it accept ':' as a no-op editor.
[git.git] / builtin-tag.c
blob88a5449e67c9220564fe3461b9f99dc1fd41e9b4
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"
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 void launch_editor(const char *path, struct strbuf *buffer)
22 const char *editor, *terminal;
23 struct child_process child;
24 const char *args[3];
26 editor = getenv("GIT_EDITOR");
27 if (!editor && editor_program)
28 editor = editor_program;
29 if (!editor)
30 editor = getenv("VISUAL");
31 if (!editor)
32 editor = getenv("EDITOR");
34 terminal = getenv("TERM");
35 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
36 fprintf(stderr,
37 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
38 "Please supply the message using either -m or -F option.\n");
39 exit(1);
42 if (!editor)
43 editor = "vi";
45 if (!strcmp(editor, ":"))
46 return;
48 memset(&child, 0, sizeof(child));
49 child.argv = args;
50 args[0] = editor;
51 args[1] = path;
52 args[2] = NULL;
54 if (run_command(&child))
55 die("There was a problem with the editor %s.", editor);
57 if (strbuf_read_file(buffer, path, 0) < 0)
58 die("could not read message file '%s': %s",
59 path, strerror(errno));
62 struct tag_filter {
63 const char *pattern;
64 int lines;
67 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
69 static int show_reference(const char *refname, const unsigned char *sha1,
70 int flag, void *cb_data)
72 struct tag_filter *filter = cb_data;
74 if (!fnmatch(filter->pattern, refname, 0)) {
75 int i;
76 unsigned long size;
77 enum object_type type;
78 char *buf, *sp, *eol;
79 size_t len;
81 if (!filter->lines) {
82 printf("%s\n", refname);
83 return 0;
85 printf("%-15s ", refname);
87 buf = read_sha1_file(sha1, &type, &size);
88 if (!buf || !size)
89 return 0;
91 /* skip header */
92 sp = strstr(buf, "\n\n");
93 if (!sp) {
94 free(buf);
95 return 0;
97 /* only take up to "lines" lines, and strip the signature */
98 for (i = 0, sp += 2;
99 i < filter->lines && sp < buf + size &&
100 prefixcmp(sp, PGP_SIGNATURE "\n");
101 i++) {
102 if (i)
103 printf("\n ");
104 eol = memchr(sp, '\n', size - (sp - buf));
105 len = eol ? eol - sp : size - (sp - buf);
106 fwrite(sp, len, 1, stdout);
107 if (!eol)
108 break;
109 sp = eol + 1;
111 putchar('\n');
112 free(buf);
115 return 0;
118 static int list_tags(const char *pattern, int lines)
120 struct tag_filter filter;
122 if (pattern == NULL)
123 pattern = "*";
125 filter.pattern = pattern;
126 filter.lines = lines;
128 for_each_tag_ref(show_reference, (void *) &filter);
130 return 0;
133 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
134 const unsigned char *sha1);
136 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
138 const char **p;
139 char ref[PATH_MAX];
140 int had_error = 0;
141 unsigned char sha1[20];
143 for (p = argv; *p; p++) {
144 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
145 >= sizeof(ref)) {
146 error("tag name too long: %.*s...", 50, *p);
147 had_error = 1;
148 continue;
150 if (!resolve_ref(ref, sha1, 1, NULL)) {
151 error("tag '%s' not found.", *p);
152 had_error = 1;
153 continue;
155 if (fn(*p, ref, sha1))
156 had_error = 1;
158 return had_error;
161 static int delete_tag(const char *name, const char *ref,
162 const unsigned char *sha1)
164 if (delete_ref(ref, sha1))
165 return 1;
166 printf("Deleted tag '%s'\n", name);
167 return 0;
170 static int verify_tag(const char *name, const char *ref,
171 const unsigned char *sha1)
173 const char *argv_verify_tag[] = {"git-verify-tag",
174 "-v", "SHA1_HEX", NULL};
175 argv_verify_tag[2] = sha1_to_hex(sha1);
177 if (run_command_v_opt(argv_verify_tag, 0))
178 return error("could not verify the tag '%s'", name);
179 return 0;
182 static int do_sign(struct strbuf *buffer)
184 struct child_process gpg;
185 const char *args[4];
186 char *bracket;
187 int len;
189 if (!*signingkey) {
190 if (strlcpy(signingkey, git_committer_info(1),
191 sizeof(signingkey)) > sizeof(signingkey) - 1)
192 return error("committer info too long.");
193 bracket = strchr(signingkey, '>');
194 if (bracket)
195 bracket[1] = '\0';
198 /* When the username signingkey is bad, program could be terminated
199 * because gpg exits without reading and then write gets SIGPIPE. */
200 signal(SIGPIPE, SIG_IGN);
202 memset(&gpg, 0, sizeof(gpg));
203 gpg.argv = args;
204 gpg.in = -1;
205 gpg.out = -1;
206 args[0] = "gpg";
207 args[1] = "-bsau";
208 args[2] = signingkey;
209 args[3] = NULL;
211 if (start_command(&gpg))
212 return error("could not run gpg.");
214 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
215 close(gpg.in);
216 finish_command(&gpg);
217 return error("gpg did not accept the tag data");
219 close(gpg.in);
220 gpg.close_in = 0;
221 len = strbuf_read(buffer, gpg.out, 1024);
223 if (finish_command(&gpg) || !len || len < 0)
224 return error("gpg failed to sign the tag");
226 if (len < 0)
227 return error("could not read the entire signature from gpg.");
229 return 0;
232 static const char tag_template[] =
233 "\n"
234 "#\n"
235 "# Write a tag message\n"
236 "#\n";
238 static int git_tag_config(const char *var, const char *value)
240 if (!strcmp(var, "user.signingkey")) {
241 if (!value)
242 die("user.signingkey without value");
243 if (strlcpy(signingkey, value, sizeof(signingkey))
244 >= sizeof(signingkey))
245 die("user.signingkey value too long");
246 return 0;
249 return git_default_config(var, value);
252 static void write_tag_body(int fd, const unsigned char *sha1)
254 unsigned long size;
255 enum object_type type;
256 char *buf, *sp, *eob;
257 size_t len;
259 buf = read_sha1_file(sha1, &type, &size);
260 if (!buf)
261 return;
262 /* skip header */
263 sp = strstr(buf, "\n\n");
265 if (!sp || !size || type != OBJ_TAG) {
266 free(buf);
267 return;
269 sp += 2; /* skip the 2 LFs */
270 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
271 if (eob)
272 len = eob - sp;
273 else
274 len = buf + size - sp;
275 write_or_die(fd, sp, len);
277 free(buf);
280 static void create_tag(const unsigned char *object, const char *tag,
281 struct strbuf *buf, int message, int sign,
282 unsigned char *prev, unsigned char *result)
284 enum object_type type;
285 char header_buf[1024];
286 int header_len;
288 type = sha1_object_info(object, NULL);
289 if (type <= OBJ_NONE)
290 die("bad object type.");
292 header_len = snprintf(header_buf, sizeof(header_buf),
293 "object %s\n"
294 "type %s\n"
295 "tag %s\n"
296 "tagger %s\n\n",
297 sha1_to_hex(object),
298 typename(type),
299 tag,
300 git_committer_info(1));
302 if (header_len > sizeof(header_buf) - 1)
303 die("tag header too big.");
305 if (!message) {
306 char *path;
307 int fd;
309 /* write the template message before editing: */
310 path = xstrdup(git_path("TAG_EDITMSG"));
311 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
312 if (fd < 0)
313 die("could not create file '%s': %s",
314 path, strerror(errno));
316 if (!is_null_sha1(prev))
317 write_tag_body(fd, prev);
318 else
319 write_or_die(fd, tag_template, strlen(tag_template));
320 close(fd);
322 launch_editor(path, buf);
324 unlink(path);
325 free(path);
328 stripspace(buf, 1);
330 if (!message && !buf->len)
331 die("no tag message?");
333 strbuf_insert(buf, 0, header_buf, header_len);
335 if (sign && do_sign(buf) < 0)
336 die("unable to sign the tag");
337 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
338 die("unable to write tag file");
341 int cmd_tag(int argc, const char **argv, const char *prefix)
343 struct strbuf buf;
344 unsigned char object[20], prev[20];
345 int annotate = 0, sign = 0, force = 0, lines = 0, message = 0;
346 char ref[PATH_MAX];
347 const char *object_ref, *tag;
348 int i;
349 struct ref_lock *lock;
351 git_config(git_tag_config);
352 strbuf_init(&buf, 0);
354 for (i = 1; i < argc; i++) {
355 const char *arg = argv[i];
357 if (arg[0] != '-')
358 break;
359 if (!strcmp(arg, "-a")) {
360 annotate = 1;
361 continue;
363 if (!strcmp(arg, "-s")) {
364 annotate = 1;
365 sign = 1;
366 continue;
368 if (!strcmp(arg, "-f")) {
369 force = 1;
370 continue;
372 if (!strcmp(arg, "-n")) {
373 if (i + 1 == argc || *argv[i + 1] == '-')
374 /* no argument */
375 lines = 1;
376 else
377 lines = isdigit(*argv[++i]) ?
378 atoi(argv[i]) : 1;
379 continue;
381 if (!strcmp(arg, "-m")) {
382 annotate = 1;
383 i++;
384 if (i == argc)
385 die("option -m needs an argument.");
386 if (message)
387 die("only one -F or -m option is allowed.");
388 strbuf_addstr(&buf, argv[i]);
389 message = 1;
390 continue;
392 if (!strcmp(arg, "-F")) {
393 annotate = 1;
394 i++;
395 if (i == argc)
396 die("option -F needs an argument.");
397 if (message)
398 die("only one -F or -m option is allowed.");
400 if (!strcmp(argv[i], "-")) {
401 if (strbuf_read(&buf, 0, 1024) < 0)
402 die("cannot read %s", argv[i]);
403 } else {
404 if (strbuf_read_file(&buf, argv[i], 1024) < 0)
405 die("could not open or read '%s': %s",
406 argv[i], strerror(errno));
408 message = 1;
409 continue;
411 if (!strcmp(arg, "-u")) {
412 annotate = 1;
413 sign = 1;
414 i++;
415 if (i == argc)
416 die("option -u needs an argument.");
417 if (strlcpy(signingkey, argv[i], sizeof(signingkey))
418 >= sizeof(signingkey))
419 die("argument to option -u too long");
420 continue;
422 if (!strcmp(arg, "-l"))
423 return list_tags(argv[i + 1], lines);
424 if (!strcmp(arg, "-d"))
425 return for_each_tag_name(argv + i + 1, delete_tag);
426 if (!strcmp(arg, "-v"))
427 return for_each_tag_name(argv + i + 1, verify_tag);
428 usage(builtin_tag_usage);
431 if (i == argc) {
432 if (annotate)
433 usage(builtin_tag_usage);
434 return list_tags(NULL, lines);
436 tag = argv[i++];
438 object_ref = i < argc ? argv[i] : "HEAD";
439 if (i + 1 < argc)
440 die("too many params");
442 if (get_sha1(object_ref, object))
443 die("Failed to resolve '%s' as a valid ref.", object_ref);
445 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
446 die("tag name too long: %.*s...", 50, tag);
447 if (check_ref_format(ref))
448 die("'%s' is not a valid tag name.", tag);
450 if (!resolve_ref(ref, prev, 1, NULL))
451 hashclr(prev);
452 else if (!force)
453 die("tag '%s' already exists", tag);
455 if (annotate)
456 create_tag(object, tag, &buf, message, 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;