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