Git 1.7.9
[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 14#include "parse-options.h"
ffc4b801
JK
15#include "diff.h"
16#include "revision.h"
2f47eae2 17#include "gpg-interface.h"
39686585
CR
18
19static const char * const git_tag_usage[] = {
1b1dd23f
SB
20 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
21 "git tag -d <tagname>...",
588d0e83 22 "git tag -l [-n[<num>]] [<pattern>...]",
1b1dd23f 23 "git tag -v <tagname>...",
39686585
CR
24 NULL
25};
62e09ce9 26
62e09ce9 27struct tag_filter {
588d0e83 28 const char **patterns;
62e09ce9 29 int lines;
32c35cfb 30 struct commit_list *with_commit;
62e09ce9
CR
31};
32
588d0e83
JK
33static int match_pattern(const char **patterns, const char *ref)
34{
35 /* no pattern means match everything */
36 if (!*patterns)
37 return 1;
38 for (; *patterns; patterns++)
39 if (!fnmatch(*patterns, ref, 0))
40 return 1;
41 return 0;
42}
43
ffc4b801
JK
44static int in_commit_list(const struct commit_list *want, struct commit *c)
45{
46 for (; want; want = want->next)
47 if (!hashcmp(want->item->object.sha1, c->object.sha1))
48 return 1;
49 return 0;
50}
51
52static int contains_recurse(struct commit *candidate,
c6d72c49 53 const struct commit_list *want)
ffc4b801
JK
54{
55 struct commit_list *p;
56
57 /* was it previously marked as containing a want commit? */
58 if (candidate->object.flags & TMP_MARK)
59 return 1;
60 /* or marked as not possibly containing a want commit? */
61 if (candidate->object.flags & UNINTERESTING)
62 return 0;
63 /* or are we it? */
64 if (in_commit_list(want, candidate))
65 return 1;
66
67 if (parse_commit(candidate) < 0)
68 return 0;
69
70 /* Otherwise recurse and mark ourselves for future traversals. */
71 for (p = candidate->parents; p; p = p->next) {
c6d72c49 72 if (contains_recurse(p->item, want)) {
ffc4b801
JK
73 candidate->object.flags |= TMP_MARK;
74 return 1;
75 }
76 }
77 candidate->object.flags |= UNINTERESTING;
78 return 0;
79}
80
81static int contains(struct commit *candidate, const struct commit_list *want)
82{
c6d72c49 83 return contains_recurse(candidate, want);
ffc4b801
JK
84}
85
62e09ce9
CR
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
588d0e83 91 if (match_pattern(filter->patterns, refname)) {
62e09ce9
CR
92 int i;
93 unsigned long size;
94 enum object_type type;
95 char *buf, *sp, *eol;
96 size_t len;
97
32c35cfb
JG
98 if (filter->with_commit) {
99 struct commit *commit;
100
101 commit = lookup_commit_reference_gently(sha1, 1);
102 if (!commit)
103 return 0;
ffc4b801 104 if (!contains(commit, filter->with_commit))
32c35cfb
JG
105 return 0;
106 }
107
62e09ce9
CR
108 if (!filter->lines) {
109 printf("%s\n", refname);
110 return 0;
111 }
112 printf("%-15s ", refname);
113
e1f14cce
MH
114 buf = read_sha1_file(sha1, &type, &size);
115 if (!buf || !size)
62e09ce9 116 return 0;
e1f14cce
MH
117
118 /* skip header */
119 sp = strstr(buf, "\n\n");
120 if (!sp) {
e317cfaf
CR
121 free(buf);
122 return 0;
123 }
62e09ce9 124 /* only take up to "lines" lines, and strip the signature */
81536b2d 125 size = parse_signature(buf, size);
e317cfaf 126 for (i = 0, sp += 2;
81536b2d 127 i < filter->lines && sp < buf + size;
62e09ce9
CR
128 i++) {
129 if (i)
130 printf("\n ");
131 eol = memchr(sp, '\n', size - (sp - buf));
132 len = eol ? eol - sp : size - (sp - buf);
133 fwrite(sp, len, 1, stdout);
134 if (!eol)
135 break;
136 sp = eol + 1;
137 }
138 putchar('\n');
139 free(buf);
140 }
141
142 return 0;
143}
144
588d0e83 145static int list_tags(const char **patterns, int lines,
32c35cfb 146 struct commit_list *with_commit)
62e09ce9
CR
147{
148 struct tag_filter filter;
62e09ce9 149
588d0e83 150 filter.patterns = patterns;
62e09ce9 151 filter.lines = lines;
32c35cfb 152 filter.with_commit = with_commit;
62e09ce9
CR
153
154 for_each_tag_ref(show_reference, (void *) &filter);
155
62e09ce9
CR
156 return 0;
157}
158
e317cfaf 159typedef int (*each_tag_name_fn)(const char *name, const char *ref,
62e09ce9
CR
160 const unsigned char *sha1);
161
e317cfaf 162static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
62e09ce9
CR
163{
164 const char **p;
165 char ref[PATH_MAX];
166 int had_error = 0;
167 unsigned char sha1[20];
168
169 for (p = argv; *p; p++) {
170 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
171 >= sizeof(ref)) {
d08ebf99 172 error(_("tag name too long: %.*s..."), 50, *p);
62e09ce9
CR
173 had_error = 1;
174 continue;
175 }
c6893323 176 if (read_ref(ref, sha1)) {
d08ebf99 177 error(_("tag '%s' not found."), *p);
62e09ce9
CR
178 had_error = 1;
179 continue;
180 }
181 if (fn(*p, ref, sha1))
182 had_error = 1;
183 }
184 return had_error;
185}
186
187static int delete_tag(const char *name, const char *ref,
188 const unsigned char *sha1)
189{
eca35a25 190 if (delete_ref(ref, sha1, 0))
62e09ce9 191 return 1;
d08ebf99 192 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
62e09ce9
CR
193 return 0;
194}
195
196static int verify_tag(const char *name, const char *ref,
197 const unsigned char *sha1)
198{
a6ccbbdb 199 const char *argv_verify_tag[] = {"verify-tag",
62e09ce9
CR
200 "-v", "SHA1_HEX", NULL};
201 argv_verify_tag[2] = sha1_to_hex(sha1);
202
a6ccbbdb 203 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
d08ebf99 204 return error(_("could not verify the tag '%s'"), name);
62e09ce9
CR
205 return 0;
206}
207
fd17f5b5 208static int do_sign(struct strbuf *buffer)
62e09ce9 209{
2f47eae2 210 return sign_buffer(buffer, buffer, get_signing_key());
62e09ce9
CR
211}
212
213static const char tag_template[] =
7fbff25a 214 N_("\n"
62e09ce9
CR
215 "#\n"
216 "# Write a tag message\n"
d3e05983
KS
217 "# Lines starting with '#' will be ignored.\n"
218 "#\n");
219
220static const char tag_template_nocleanup[] =
221 N_("\n"
222 "#\n"
223 "# Write a tag message\n"
224 "# Lines starting with '#' will be kept; you may remove them"
225 " yourself if you want to.\n"
7fbff25a 226 "#\n");
62e09ce9 227
ef90d6d4 228static int git_tag_config(const char *var, const char *value, void *cb)
62e09ce9 229{
2f47eae2
JH
230 int status = git_gpg_config(var, value, cb);
231 if (status)
232 return status;
ef90d6d4 233 return git_default_config(var, value, cb);
62e09ce9
CR
234}
235
bab8118a
MH
236static void write_tag_body(int fd, const unsigned char *sha1)
237{
238 unsigned long size;
239 enum object_type type;
e10dfb62 240 char *buf, *sp;
bab8118a
MH
241
242 buf = read_sha1_file(sha1, &type, &size);
243 if (!buf)
244 return;
245 /* skip header */
246 sp = strstr(buf, "\n\n");
247
248 if (!sp || !size || type != OBJ_TAG) {
249 free(buf);
250 return;
251 }
252 sp += 2; /* skip the 2 LFs */
e10dfb62 253 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
bab8118a
MH
254
255 free(buf);
256}
257
3927bbe9
JK
258static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
259{
260 if (sign && do_sign(buf) < 0)
d08ebf99 261 return error(_("unable to sign the tag"));
3927bbe9 262 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
d08ebf99 263 return error(_("unable to write tag file"));
3927bbe9
JK
264 return 0;
265}
266
d3e05983
KS
267struct create_tag_options {
268 unsigned int message_given:1;
269 unsigned int sign;
270 enum {
271 CLEANUP_NONE,
272 CLEANUP_SPACE,
273 CLEANUP_ALL
274 } cleanup_mode;
275};
276
62e09ce9 277static void create_tag(const unsigned char *object, const char *tag,
d3e05983 278 struct strbuf *buf, struct create_tag_options *opt,
bd46c9a9 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;
3927bbe9 284 char *path = NULL;
62e09ce9
CR
285
286 type = sha1_object_info(object, NULL);
e317cfaf 287 if (type <= OBJ_NONE)
d08ebf99 288 die(_("bad object type."));
62e09ce9
CR
289
290 header_len = snprintf(header_buf, sizeof(header_buf),
291 "object %s\n"
292 "type %s\n"
293 "tag %s\n"
294 "tagger %s\n\n",
295 sha1_to_hex(object),
296 typename(type),
297 tag,
774751a8 298 git_committer_info(IDENT_ERROR_ON_NO_NAME));
62e09ce9 299
e317cfaf 300 if (header_len > sizeof(header_buf) - 1)
d08ebf99 301 die(_("tag header too big."));
62e09ce9 302
d3e05983 303 if (!opt->message_given) {
62e09ce9
CR
304 int fd;
305
306 /* write the template message before editing: */
a4f34cbb 307 path = git_pathdup("TAG_EDITMSG");
62e09ce9
CR
308 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
309 if (fd < 0)
d08ebf99 310 die_errno(_("could not create file '%s'"), path);
bab8118a
MH
311
312 if (!is_null_sha1(prev))
313 write_tag_body(fd, prev);
d3e05983
KS
314 else if (opt->cleanup_mode == CLEANUP_ALL)
315 write_or_die(fd, _(tag_template),
316 strlen(_(tag_template)));
bab8118a 317 else
d3e05983
KS
318 write_or_die(fd, _(tag_template_nocleanup),
319 strlen(_(tag_template_nocleanup)));
62e09ce9
CR
320 close(fd);
321
7198203a
SB
322 if (launch_editor(path, buf, NULL)) {
323 fprintf(stderr,
d08ebf99 324 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
325 exit(1);
326 }
62e09ce9 327 }
62e09ce9 328
d3e05983
KS
329 if (opt->cleanup_mode != CLEANUP_NONE)
330 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
62e09ce9 331
d3e05983 332 if (!opt->message_given && !buf->len)
d08ebf99 333 die(_("no tag message?"));
62e09ce9 334
fd17f5b5 335 strbuf_insert(buf, 0, header_buf, header_len);
62e09ce9 336
d3e05983 337 if (build_tag_object(buf, opt->sign, result) < 0) {
3927bbe9 338 if (path)
d08ebf99 339 fprintf(stderr, _("The tag message has been left in %s\n"),
3927bbe9
JK
340 path);
341 exit(128);
342 }
343 if (path) {
691f1a28 344 unlink_or_warn(path);
3927bbe9
JK
345 free(path);
346 }
62e09ce9
CR
347}
348
bd46c9a9
JH
349struct msg_arg {
350 int given;
351 struct strbuf buf;
352};
353
354static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
355{
356 struct msg_arg *msg = opt->value;
357
358 if (!arg)
359 return -1;
360 if (msg->buf.len)
361 strbuf_addstr(&(msg->buf), "\n\n");
362 strbuf_addstr(&(msg->buf), arg);
363 msg->given = 1;
364 return 0;
365}
366
4f0accd6
MS
367static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
368{
369 if (name[0] == '-')
8d9c5010 370 return -1;
4f0accd6
MS
371
372 strbuf_reset(sb);
373 strbuf_addf(sb, "refs/tags/%s", name);
374
8d9c5010 375 return check_refname_format(sb->buf, 0);
4f0accd6
MS
376}
377
62e09ce9
CR
378int cmd_tag(int argc, const char **argv, const char *prefix)
379{
f285a2d7 380 struct strbuf buf = STRBUF_INIT;
4f0accd6 381 struct strbuf ref = STRBUF_INIT;
62e09ce9 382 unsigned char object[20], prev[20];
62e09ce9 383 const char *object_ref, *tag;
62e09ce9 384 struct ref_lock *lock;
d3e05983
KS
385 struct create_tag_options opt;
386 char *cleanup_arg = NULL;
387 int annotate = 0, force = 0, lines = -1, list = 0,
388 delete = 0, verify = 0;
dbd0f5c7 389 const char *msgfile = NULL, *keyid = NULL;
bd46c9a9 390 struct msg_arg msg = { 0, STRBUF_INIT };
32c35cfb 391 struct commit_list *with_commit = NULL;
39686585 392 struct option options[] = {
c97eff5a 393 OPT_BOOLEAN('l', "list", &list, "list tag names"),
e3a0ca87
SB
394 { OPTION_INTEGER, 'n', NULL, &lines, "n",
395 "print <n> lines of each tag message",
39686585 396 PARSE_OPT_OPTARG, NULL, 1 },
c97eff5a
MG
397 OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
398 OPT_BOOLEAN('v', "verify", &verify, "verify tags"),
39686585
CR
399
400 OPT_GROUP("Tag creation options"),
c97eff5a 401 OPT_BOOLEAN('a', "annotate", &annotate,
39686585 402 "annotated tag, needs a message"),
c97eff5a 403 OPT_CALLBACK('m', "message", &msg, "message",
3f406175 404 "tag message", parse_msg_arg),
c97eff5a 405 OPT_FILENAME('F', "file", &msgfile, "read message from file"),
d3e05983
KS
406 OPT_BOOLEAN('s', "sign", &opt.sign, "annotated and GPG-signed tag"),
407 OPT_STRING(0, "cleanup", &cleanup_arg, "mode",
408 "how to strip spaces and #comments from message"),
c97eff5a 409 OPT_STRING('u', "local-user", &keyid, "key-id",
39686585 410 "use another key to sign the tag"),
76946b76 411 OPT__FORCE(&force, "replace the tag if exists"),
32c35cfb
JG
412
413 OPT_GROUP("Tag listing options"),
414 {
415 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
416 "print only tags that contain the commit",
417 PARSE_OPT_LASTARG_DEFAULT,
418 parse_opt_with_commit, (intptr_t)"HEAD",
419 },
39686585
CR
420 OPT_END()
421 };
422
ef90d6d4 423 git_config(git_tag_config, NULL);
62e09ce9 424
d3e05983
KS
425 memset(&opt, 0, sizeof(opt));
426
37782920 427 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
62e09ce9 428
be15f505 429 if (keyid) {
d3e05983 430 opt.sign = 1;
2f47eae2 431 set_signing_key(keyid);
be15f505 432 }
d3e05983 433 if (opt.sign)
c1a41b9d 434 annotate = 1;
6fa8342b
ST
435 if (argc == 0 && !(delete || verify))
436 list = 1;
c1a41b9d 437
6fa8342b
ST
438 if ((annotate || msg.given || msgfile || force) &&
439 (list || delete || verify))
440 usage_with_options(git_tag_usage, options);
441
442 if (list + delete + verify > 1)
443 usage_with_options(git_tag_usage, options);
39686585 444 if (list)
588d0e83 445 return list_tags(argv, lines == -1 ? 0 : lines,
32c35cfb 446 with_commit);
6fa8342b 447 if (lines != -1)
d08ebf99 448 die(_("-n option is only allowed with -l."));
32c35cfb 449 if (with_commit)
d08ebf99 450 die(_("--contains option is only allowed with -l."));
39686585
CR
451 if (delete)
452 return for_each_tag_name(argv, delete_tag);
453 if (verify)
454 return for_each_tag_name(argv, verify_tag);
455
bd46c9a9
JH
456 if (msg.given || msgfile) {
457 if (msg.given && msgfile)
d08ebf99 458 die(_("only one -F or -m option is allowed."));
39686585 459 annotate = 1;
bd46c9a9
JH
460 if (msg.given)
461 strbuf_addbuf(&buf, &(msg.buf));
39686585
CR
462 else {
463 if (!strcmp(msgfile, "-")) {
387e7e19 464 if (strbuf_read(&buf, 0, 1024) < 0)
d08ebf99 465 die_errno(_("cannot read '%s'"), msgfile);
387e7e19 466 } else {
39686585 467 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
d08ebf99 468 die_errno(_("could not open or read '%s'"),
d824cbba 469 msgfile);
62e09ce9 470 }
62e09ce9 471 }
62e09ce9
CR
472 }
473
39686585 474 tag = argv[0];
62e09ce9 475
39686585
CR
476 object_ref = argc == 2 ? argv[1] : "HEAD";
477 if (argc > 2)
d08ebf99 478 die(_("too many params"));
62e09ce9
CR
479
480 if (get_sha1(object_ref, object))
d08ebf99 481 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
62e09ce9 482
4f0accd6 483 if (strbuf_check_tag_ref(&ref, tag))
d08ebf99 484 die(_("'%s' is not a valid tag name."), tag);
62e09ce9 485
c6893323 486 if (read_ref(ref.buf, prev))
62e09ce9
CR
487 hashclr(prev);
488 else if (!force)
d08ebf99 489 die(_("tag '%s' already exists"), tag);
62e09ce9 490
d3e05983
KS
491 opt.message_given = msg.given || msgfile;
492
493 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
494 opt.cleanup_mode = CLEANUP_ALL;
495 else if (!strcmp(cleanup_arg, "verbatim"))
496 opt.cleanup_mode = CLEANUP_NONE;
497 else if (!strcmp(cleanup_arg, "whitespace"))
498 opt.cleanup_mode = CLEANUP_SPACE;
499 else
500 die(_("Invalid cleanup mode %s"), cleanup_arg);
501
62e09ce9 502 if (annotate)
d3e05983 503 create_tag(object, tag, &buf, &opt, prev, object);
62e09ce9 504
4f0accd6 505 lock = lock_any_ref_for_update(ref.buf, prev, 0);
62e09ce9 506 if (!lock)
4f0accd6 507 die(_("%s: cannot lock the ref"), ref.buf);
62e09ce9 508 if (write_ref_sha1(lock, object, NULL) < 0)
4f0accd6 509 die(_("%s: cannot update the ref"), ref.buf);
0a043b1f 510 if (force && hashcmp(prev, object))
d08ebf99 511 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
62e09ce9 512
fd17f5b5 513 strbuf_release(&buf);
4f0accd6 514 strbuf_release(&ref);
62e09ce9
CR
515 return 0;
516}