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