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