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