Move launch_editor() from builtin-tag.c to editor.c
[git/git.git] / builtin-tag.c
1 /*
2 * Builtin "git tag"
3 *
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 */
8
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"
15
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
22 };
23
24 static char signingkey[1000];
25
26 struct tag_filter {
27 const char *pattern;
28 int lines;
29 };
30
31 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
32
33 static int show_reference(const char *refname, const unsigned char *sha1,
34 int flag, void *cb_data)
35 {
36 struct tag_filter *filter = cb_data;
37
38 if (!fnmatch(filter->pattern, refname, 0)) {
39 int i;
40 unsigned long size;
41 enum object_type type;
42 char *buf, *sp, *eol;
43 size_t len;
44
45 if (!filter->lines) {
46 printf("%s\n", refname);
47 return 0;
48 }
49 printf("%-15s ", refname);
50
51 buf = read_sha1_file(sha1, &type, &size);
52 if (!buf || !size)
53 return 0;
54
55 /* skip header */
56 sp = strstr(buf, "\n\n");
57 if (!sp) {
58 free(buf);
59 return 0;
60 }
61 /* only take up to "lines" lines, and strip the signature */
62 for (i = 0, sp += 2;
63 i < filter->lines && sp < buf + size &&
64 prefixcmp(sp, PGP_SIGNATURE "\n");
65 i++) {
66 if (i)
67 printf("\n ");
68 eol = memchr(sp, '\n', size - (sp - buf));
69 len = eol ? eol - sp : size - (sp - buf);
70 fwrite(sp, len, 1, stdout);
71 if (!eol)
72 break;
73 sp = eol + 1;
74 }
75 putchar('\n');
76 free(buf);
77 }
78
79 return 0;
80 }
81
82 static int list_tags(const char *pattern, int lines)
83 {
84 struct tag_filter filter;
85
86 if (pattern == NULL)
87 pattern = "*";
88
89 filter.pattern = pattern;
90 filter.lines = lines;
91
92 for_each_tag_ref(show_reference, (void *) &filter);
93
94 return 0;
95 }
96
97 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
98 const unsigned char *sha1);
99
100 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
101 {
102 const char **p;
103 char ref[PATH_MAX];
104 int had_error = 0;
105 unsigned char sha1[20];
106
107 for (p = argv; *p; p++) {
108 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
109 >= sizeof(ref)) {
110 error("tag name too long: %.*s...", 50, *p);
111 had_error = 1;
112 continue;
113 }
114 if (!resolve_ref(ref, sha1, 1, NULL)) {
115 error("tag '%s' not found.", *p);
116 had_error = 1;
117 continue;
118 }
119 if (fn(*p, ref, sha1))
120 had_error = 1;
121 }
122 return had_error;
123 }
124
125 static int delete_tag(const char *name, const char *ref,
126 const unsigned char *sha1)
127 {
128 if (delete_ref(ref, sha1))
129 return 1;
130 printf("Deleted tag '%s'\n", name);
131 return 0;
132 }
133
134 static int verify_tag(const char *name, const char *ref,
135 const unsigned char *sha1)
136 {
137 const char *argv_verify_tag[] = {"git-verify-tag",
138 "-v", "SHA1_HEX", NULL};
139 argv_verify_tag[2] = sha1_to_hex(sha1);
140
141 if (run_command_v_opt(argv_verify_tag, 0))
142 return error("could not verify the tag '%s'", name);
143 return 0;
144 }
145
146 static int do_sign(struct strbuf *buffer)
147 {
148 struct child_process gpg;
149 const char *args[4];
150 char *bracket;
151 int len;
152 int i, j;
153
154 if (!*signingkey) {
155 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
156 sizeof(signingkey)) > sizeof(signingkey) - 1)
157 return error("committer info too long.");
158 bracket = strchr(signingkey, '>');
159 if (bracket)
160 bracket[1] = '\0';
161 }
162
163 /* When the username signingkey is bad, program could be terminated
164 * because gpg exits without reading and then write gets SIGPIPE. */
165 signal(SIGPIPE, SIG_IGN);
166
167 memset(&gpg, 0, sizeof(gpg));
168 gpg.argv = args;
169 gpg.in = -1;
170 gpg.out = -1;
171 args[0] = "gpg";
172 args[1] = "-bsau";
173 args[2] = signingkey;
174 args[3] = NULL;
175
176 if (start_command(&gpg))
177 return error("could not run gpg.");
178
179 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
180 close(gpg.in);
181 close(gpg.out);
182 finish_command(&gpg);
183 return error("gpg did not accept the tag data");
184 }
185 close(gpg.in);
186 len = strbuf_read(buffer, gpg.out, 1024);
187 close(gpg.out);
188
189 if (finish_command(&gpg) || !len || len < 0)
190 return error("gpg failed to sign the tag");
191
192 /* Strip CR from the line endings, in case we are on Windows. */
193 for (i = j = 0; i < buffer->len; i++)
194 if (buffer->buf[i] != '\r') {
195 if (i != j)
196 buffer->buf[j] = buffer->buf[i];
197 j++;
198 }
199 strbuf_setlen(buffer, j);
200
201 return 0;
202 }
203
204 static const char tag_template[] =
205 "\n"
206 "#\n"
207 "# Write a tag message\n"
208 "#\n";
209
210 static void set_signingkey(const char *value)
211 {
212 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
213 die("signing key value too long (%.10s...)", value);
214 }
215
216 static int git_tag_config(const char *var, const char *value, void *cb)
217 {
218 if (!strcmp(var, "user.signingkey")) {
219 if (!value)
220 return config_error_nonbool(var);
221 set_signingkey(value);
222 return 0;
223 }
224
225 return git_default_config(var, value, cb);
226 }
227
228 static void write_tag_body(int fd, const unsigned char *sha1)
229 {
230 unsigned long size;
231 enum object_type type;
232 char *buf, *sp, *eob;
233 size_t len;
234
235 buf = read_sha1_file(sha1, &type, &size);
236 if (!buf)
237 return;
238 /* skip header */
239 sp = strstr(buf, "\n\n");
240
241 if (!sp || !size || type != OBJ_TAG) {
242 free(buf);
243 return;
244 }
245 sp += 2; /* skip the 2 LFs */
246 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
247 if (eob)
248 len = eob - sp;
249 else
250 len = buf + size - sp;
251 write_or_die(fd, sp, len);
252
253 free(buf);
254 }
255
256 static void create_tag(const unsigned char *object, const char *tag,
257 struct strbuf *buf, int message, int sign,
258 unsigned char *prev, unsigned char *result)
259 {
260 enum object_type type;
261 char header_buf[1024];
262 int header_len;
263
264 type = sha1_object_info(object, NULL);
265 if (type <= OBJ_NONE)
266 die("bad object type.");
267
268 header_len = snprintf(header_buf, sizeof(header_buf),
269 "object %s\n"
270 "type %s\n"
271 "tag %s\n"
272 "tagger %s\n\n",
273 sha1_to_hex(object),
274 typename(type),
275 tag,
276 git_committer_info(IDENT_ERROR_ON_NO_NAME));
277
278 if (header_len > sizeof(header_buf) - 1)
279 die("tag header too big.");
280
281 if (!message) {
282 char *path;
283 int fd;
284
285 /* write the template message before editing: */
286 path = xstrdup(git_path("TAG_EDITMSG"));
287 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
288 if (fd < 0)
289 die("could not create file '%s': %s",
290 path, strerror(errno));
291
292 if (!is_null_sha1(prev))
293 write_tag_body(fd, prev);
294 else
295 write_or_die(fd, tag_template, strlen(tag_template));
296 close(fd);
297
298 launch_editor(path, buf, NULL);
299
300 unlink(path);
301 free(path);
302 }
303
304 stripspace(buf, 1);
305
306 if (!message && !buf->len)
307 die("no tag message?");
308
309 strbuf_insert(buf, 0, header_buf, header_len);
310
311 if (sign && do_sign(buf) < 0)
312 die("unable to sign the tag");
313 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
314 die("unable to write tag file");
315 }
316
317 struct msg_arg {
318 int given;
319 struct strbuf buf;
320 };
321
322 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
323 {
324 struct msg_arg *msg = opt->value;
325
326 if (!arg)
327 return -1;
328 if (msg->buf.len)
329 strbuf_addstr(&(msg->buf), "\n\n");
330 strbuf_addstr(&(msg->buf), arg);
331 msg->given = 1;
332 return 0;
333 }
334
335 int cmd_tag(int argc, const char **argv, const char *prefix)
336 {
337 struct strbuf buf;
338 unsigned char object[20], prev[20];
339 char ref[PATH_MAX];
340 const char *object_ref, *tag;
341 struct ref_lock *lock;
342
343 int annotate = 0, sign = 0, force = 0, lines = 0,
344 list = 0, delete = 0, verify = 0;
345 char *msgfile = NULL, *keyid = NULL;
346 struct msg_arg msg = { 0, STRBUF_INIT };
347 struct option options[] = {
348 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
349 { OPTION_INTEGER, 'n', NULL, &lines, NULL,
350 "print n lines of each tag message",
351 PARSE_OPT_OPTARG, NULL, 1 },
352 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
353 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
354
355 OPT_GROUP("Tag creation options"),
356 OPT_BOOLEAN('a', NULL, &annotate,
357 "annotated tag, needs a message"),
358 OPT_CALLBACK('m', NULL, &msg, "msg",
359 "message for the tag", parse_msg_arg),
360 OPT_STRING('F', NULL, &msgfile, "file", "message in a file"),
361 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
362 OPT_STRING('u', NULL, &keyid, "key-id",
363 "use another key to sign the tag"),
364 OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
365 OPT_END()
366 };
367
368 git_config(git_tag_config, NULL);
369
370 argc = parse_options(argc, argv, options, git_tag_usage, 0);
371
372 if (keyid) {
373 sign = 1;
374 set_signingkey(keyid);
375 }
376 if (sign)
377 annotate = 1;
378
379 if (list)
380 return list_tags(argv[0], lines);
381 if (delete)
382 return for_each_tag_name(argv, delete_tag);
383 if (verify)
384 return for_each_tag_name(argv, verify_tag);
385
386 strbuf_init(&buf, 0);
387 if (msg.given || msgfile) {
388 if (msg.given && msgfile)
389 die("only one -F or -m option is allowed.");
390 annotate = 1;
391 if (msg.given)
392 strbuf_addbuf(&buf, &(msg.buf));
393 else {
394 if (!strcmp(msgfile, "-")) {
395 if (strbuf_read(&buf, 0, 1024) < 0)
396 die("cannot read %s", msgfile);
397 } else {
398 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
399 die("could not open or read '%s': %s",
400 msgfile, strerror(errno));
401 }
402 }
403 }
404
405 if (argc == 0) {
406 if (annotate)
407 usage_with_options(git_tag_usage, options);
408 return list_tags(NULL, lines);
409 }
410 tag = argv[0];
411
412 object_ref = argc == 2 ? argv[1] : "HEAD";
413 if (argc > 2)
414 die("too many params");
415
416 if (get_sha1(object_ref, object))
417 die("Failed to resolve '%s' as a valid ref.", object_ref);
418
419 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
420 die("tag name too long: %.*s...", 50, tag);
421 if (check_ref_format(ref))
422 die("'%s' is not a valid tag name.", tag);
423
424 if (!resolve_ref(ref, prev, 1, NULL))
425 hashclr(prev);
426 else if (!force)
427 die("tag '%s' already exists", tag);
428
429 if (annotate)
430 create_tag(object, tag, &buf, msg.given || msgfile,
431 sign, prev, object);
432
433 lock = lock_any_ref_for_update(ref, prev, 0);
434 if (!lock)
435 die("%s: cannot lock the ref", ref);
436 if (write_ref_sha1(lock, object, NULL) < 0)
437 die("%s: cannot update the ref", ref);
438
439 strbuf_release(&buf);
440 return 0;
441 }