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