]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/tag.c
tag: do not show ambiguous tag names as "tags/foo"
[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"
d96e3c15 19#include "column.h"
ac4cc866 20#include "ref-filter.h"
39686585
CR
21
22static const char * const git_tag_usage[] = {
9c9b4f2f 23 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
c88bba18 24 N_("git tag -d <tagname>..."),
9c9b4f2f 25 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
5242860f 26 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
c88bba18 27 N_("git tag -v <tagname>..."),
39686585
CR
28 NULL
29};
62e09ce9 30
d96e3c15 31static unsigned int colopts;
ae7706b9 32
df094741 33static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
ca516999 34{
ac4cc866 35 struct ref_array array;
df094741 36 char *to_free = NULL;
ca516999 37 int i;
62e09ce9 38
ac4cc866 39 memset(&array, 0, sizeof(array));
32c35cfb 40
ac4cc866
KN
41 if (filter->lines == -1)
42 filter->lines = 0;
ae7706b9 43
df094741
KN
44 if (!format) {
45 if (filter->lines) {
46 to_free = xstrfmt("%s %%(contents:lines=%d)",
0571979b 47 "%(align:15)%(refname:strip=2)%(end)",
df094741
KN
48 filter->lines);
49 format = to_free;
50 } else
0571979b 51 format = "%(refname:strip=2)";
62e09ce9
CR
52 }
53
b7cc53e9 54 verify_ref_format(format);
008ed7df 55 filter->with_commit_tag_algo = 1;
b7cc53e9
KN
56 filter_refs(&array, filter, FILTER_REFS_TAGS);
57 ref_array_sort(sorting, &array);
62e09ce9 58
b7cc53e9
KN
59 for (i = 0; i < array.nr; i++)
60 show_ref_array_item(array.items[i], format, 0);
61 ref_array_clear(&array);
62 free(to_free);
9ef176b5 63
62e09ce9
CR
64 return 0;
65}
66
e317cfaf 67typedef int (*each_tag_name_fn)(const char *name, const char *ref,
62e09ce9
CR
68 const unsigned char *sha1);
69
e317cfaf 70static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
62e09ce9
CR
71{
72 const char **p;
73 char ref[PATH_MAX];
74 int had_error = 0;
75 unsigned char sha1[20];
76
77 for (p = argv; *p; p++) {
78 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
79 >= sizeof(ref)) {
d08ebf99 80 error(_("tag name too long: %.*s..."), 50, *p);
62e09ce9
CR
81 had_error = 1;
82 continue;
83 }
c6893323 84 if (read_ref(ref, sha1)) {
d08ebf99 85 error(_("tag '%s' not found."), *p);
62e09ce9
CR
86 had_error = 1;
87 continue;
88 }
89 if (fn(*p, ref, sha1))
90 had_error = 1;
91 }
92 return had_error;
93}
94
95static int delete_tag(const char *name, const char *ref,
96 const unsigned char *sha1)
97{
eca35a25 98 if (delete_ref(ref, sha1, 0))
62e09ce9 99 return 1;
d08ebf99 100 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
62e09ce9
CR
101 return 0;
102}
103
104static int verify_tag(const char *name, const char *ref,
105 const unsigned char *sha1)
106{
a6ccbbdb 107 const char *argv_verify_tag[] = {"verify-tag",
62e09ce9
CR
108 "-v", "SHA1_HEX", NULL};
109 argv_verify_tag[2] = sha1_to_hex(sha1);
110
a6ccbbdb 111 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
d08ebf99 112 return error(_("could not verify the tag '%s'"), name);
62e09ce9
CR
113 return 0;
114}
115
fd17f5b5 116static int do_sign(struct strbuf *buffer)
62e09ce9 117{
2f47eae2 118 return sign_buffer(buffer, buffer, get_signing_key());
62e09ce9
CR
119}
120
121static const char tag_template[] =
d78f340e 122 N_("\nWrite a message for tag:\n %s\n"
eff80a9f 123 "Lines starting with '%c' will be ignored.\n");
d3e05983
KS
124
125static const char tag_template_nocleanup[] =
d78f340e 126 N_("\nWrite a message for tag:\n %s\n"
eff80a9f
JH
127 "Lines starting with '%c' will be kept; you may remove them"
128 " yourself if you want to.\n");
62e09ce9 129
b7cc53e9
KN
130/* Parse arg given and add it the ref_sorting array */
131static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
b150794d 132{
b7cc53e9
KN
133 struct ref_sorting *s;
134 int len;
b150794d 135
b7cc53e9
KN
136 s = xcalloc(1, sizeof(*s));
137 s->next = *sorting_tail;
138 *sorting_tail = s;
b150794d 139
b7cc53e9
KN
140 if (*arg == '-') {
141 s->reverse = 1;
142 arg++;
b150794d 143 }
b7cc53e9
KN
144 if (skip_prefix(arg, "version:", &arg) ||
145 skip_prefix(arg, "v:", &arg))
146 s->version = 1;
b150794d 147
b7cc53e9
KN
148 len = strlen(arg);
149 s->atom = parse_ref_filter_atom(arg, arg+len);
b150794d
JK
150
151 return 0;
152}
153
ef90d6d4 154static int git_tag_config(const char *var, const char *value, void *cb)
62e09ce9 155{
b150794d 156 int status;
b7cc53e9 157 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
b150794d
JK
158
159 if (!strcmp(var, "tag.sort")) {
160 if (!value)
161 return config_error_nonbool(var);
b7cc53e9 162 parse_sorting_string(value, sorting_tail);
b150794d
JK
163 return 0;
164 }
165
166 status = git_gpg_config(var, value, cb);
2f47eae2
JH
167 if (status)
168 return status;
59556548 169 if (starts_with(var, "column."))
d96e3c15 170 return git_column_config(var, value, "tag", &colopts);
ef90d6d4 171 return git_default_config(var, value, cb);
62e09ce9
CR
172}
173
bab8118a
MH
174static void write_tag_body(int fd, const unsigned char *sha1)
175{
176 unsigned long size;
177 enum object_type type;
e10dfb62 178 char *buf, *sp;
bab8118a
MH
179
180 buf = read_sha1_file(sha1, &type, &size);
181 if (!buf)
182 return;
183 /* skip header */
184 sp = strstr(buf, "\n\n");
185
186 if (!sp || !size || type != OBJ_TAG) {
187 free(buf);
188 return;
189 }
190 sp += 2; /* skip the 2 LFs */
e10dfb62 191 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
bab8118a
MH
192
193 free(buf);
194}
195
3927bbe9
JK
196static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
197{
198 if (sign && do_sign(buf) < 0)
d08ebf99 199 return error(_("unable to sign the tag"));
3927bbe9 200 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
d08ebf99 201 return error(_("unable to write tag file"));
3927bbe9
JK
202 return 0;
203}
204
d3e05983
KS
205struct create_tag_options {
206 unsigned int message_given:1;
207 unsigned int sign;
208 enum {
209 CLEANUP_NONE,
210 CLEANUP_SPACE,
211 CLEANUP_ALL
212 } cleanup_mode;
213};
214
62e09ce9 215static void create_tag(const unsigned char *object, const char *tag,
d3e05983 216 struct strbuf *buf, struct create_tag_options *opt,
bd46c9a9 217 unsigned char *prev, unsigned char *result)
62e09ce9
CR
218{
219 enum object_type type;
fd17f5b5
PH
220 char header_buf[1024];
221 int header_len;
3927bbe9 222 char *path = NULL;
62e09ce9
CR
223
224 type = sha1_object_info(object, NULL);
e317cfaf 225 if (type <= OBJ_NONE)
d08ebf99 226 die(_("bad object type."));
62e09ce9
CR
227
228 header_len = snprintf(header_buf, sizeof(header_buf),
229 "object %s\n"
230 "type %s\n"
231 "tag %s\n"
232 "tagger %s\n\n",
233 sha1_to_hex(object),
234 typename(type),
235 tag,
f9bc573f 236 git_committer_info(IDENT_STRICT));
62e09ce9 237
e317cfaf 238 if (header_len > sizeof(header_buf) - 1)
d08ebf99 239 die(_("tag header too big."));
62e09ce9 240
d3e05983 241 if (!opt->message_given) {
62e09ce9
CR
242 int fd;
243
244 /* write the template message before editing: */
a4f34cbb 245 path = git_pathdup("TAG_EDITMSG");
62e09ce9
CR
246 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
247 if (fd < 0)
d08ebf99 248 die_errno(_("could not create file '%s'"), path);
bab8118a 249
eff80a9f 250 if (!is_null_sha1(prev)) {
bab8118a 251 write_tag_body(fd, prev);
eff80a9f
JH
252 } else {
253 struct strbuf buf = STRBUF_INIT;
254 strbuf_addch(&buf, '\n');
255 if (opt->cleanup_mode == CLEANUP_ALL)
d78f340e 256 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
eff80a9f 257 else
d78f340e 258 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
eff80a9f
JH
259 write_or_die(fd, buf.buf, buf.len);
260 strbuf_release(&buf);
261 }
62e09ce9
CR
262 close(fd);
263
7198203a
SB
264 if (launch_editor(path, buf, NULL)) {
265 fprintf(stderr,
d08ebf99 266 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
267 exit(1);
268 }
62e09ce9 269 }
62e09ce9 270
d3e05983 271 if (opt->cleanup_mode != CLEANUP_NONE)
63af4a84 272 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
62e09ce9 273
d3e05983 274 if (!opt->message_given && !buf->len)
d08ebf99 275 die(_("no tag message?"));
62e09ce9 276
fd17f5b5 277 strbuf_insert(buf, 0, header_buf, header_len);
62e09ce9 278
d3e05983 279 if (build_tag_object(buf, opt->sign, result) < 0) {
3927bbe9 280 if (path)
d08ebf99 281 fprintf(stderr, _("The tag message has been left in %s\n"),
3927bbe9
JK
282 path);
283 exit(128);
284 }
285 if (path) {
691f1a28 286 unlink_or_warn(path);
3927bbe9
JK
287 free(path);
288 }
62e09ce9
CR
289}
290
bd46c9a9
JH
291struct msg_arg {
292 int given;
293 struct strbuf buf;
294};
295
296static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
297{
298 struct msg_arg *msg = opt->value;
299
300 if (!arg)
301 return -1;
302 if (msg->buf.len)
303 strbuf_addstr(&(msg->buf), "\n\n");
304 strbuf_addstr(&(msg->buf), arg);
305 msg->given = 1;
306 return 0;
307}
308
4f0accd6
MS
309static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
310{
311 if (name[0] == '-')
8d9c5010 312 return -1;
4f0accd6
MS
313
314 strbuf_reset(sb);
315 strbuf_addf(sb, "refs/tags/%s", name);
316
8d9c5010 317 return check_refname_format(sb->buf, 0);
4f0accd6
MS
318}
319
62e09ce9
CR
320int cmd_tag(int argc, const char **argv, const char *prefix)
321{
f285a2d7 322 struct strbuf buf = STRBUF_INIT;
4f0accd6 323 struct strbuf ref = STRBUF_INIT;
62e09ce9 324 unsigned char object[20], prev[20];
62e09ce9 325 const char *object_ref, *tag;
d3e05983
KS
326 struct create_tag_options opt;
327 char *cleanup_arg = NULL;
144c76fa 328 int create_reflog = 0;
ac4cc866 329 int annotate = 0, force = 0;
b150794d 330 int cmdmode = 0;
dbd0f5c7 331 const char *msgfile = NULL, *keyid = NULL;
bd46c9a9 332 struct msg_arg msg = { 0, STRBUF_INIT };
e5074bfe
RS
333 struct ref_transaction *transaction;
334 struct strbuf err = STRBUF_INIT;
ac4cc866 335 struct ref_filter filter;
b7cc53e9 336 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
df094741 337 const char *format = NULL;
39686585 338 struct option options[] = {
e6b722db 339 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
ac4cc866 340 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
c88bba18 341 N_("print <n> lines of each tag message"),
39686585 342 PARSE_OPT_OPTARG, NULL, 1 },
e6b722db
JH
343 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
344 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
39686585 345
c88bba18 346 OPT_GROUP(N_("Tag creation options")),
d5d09d47 347 OPT_BOOL('a', "annotate", &annotate,
c88bba18
NTND
348 N_("annotated tag, needs a message")),
349 OPT_CALLBACK('m', "message", &msg, N_("message"),
350 N_("tag message"), parse_msg_arg),
351 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
d5d09d47 352 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
c88bba18
NTND
353 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
354 N_("how to strip spaces and #comments from message")),
e703d711 355 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
c88bba18
NTND
356 N_("use another key to sign the tag")),
357 OPT__FORCE(&force, N_("replace the tag if exists")),
98c32bd8 358 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
dd059c6c
JK
359
360 OPT_GROUP(N_("Tag listing options")),
c88bba18 361 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
ac4cc866
KN
362 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
363 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
5242860f
KN
364 OPT_MERGED(&filter, N_("print only tags that are merged")),
365 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
b7cc53e9
KN
366 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
367 N_("field name to sort on"), &parse_opt_ref_sorting),
32c35cfb 368 {
ac4cc866 369 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
b2172fdf 370 N_("print only tags of the object"), 0, parse_opt_object_name
ae7706b9 371 },
df094741 372 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
39686585
CR
373 OPT_END()
374 };
375
b7cc53e9 376 git_config(git_tag_config, sorting_tail);
62e09ce9 377
d3e05983 378 memset(&opt, 0, sizeof(opt));
ac4cc866
KN
379 memset(&filter, 0, sizeof(filter));
380 filter.lines = -1;
d3e05983 381
37782920 382 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
62e09ce9 383
be15f505 384 if (keyid) {
d3e05983 385 opt.sign = 1;
2f47eae2 386 set_signing_key(keyid);
be15f505 387 }
d3e05983 388 if (opt.sign)
c1a41b9d 389 annotate = 1;
e6b722db
JH
390 if (argc == 0 && !cmdmode)
391 cmdmode = 'l';
c1a41b9d 392
e6b722db 393 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
6fa8342b
ST
394 usage_with_options(git_tag_usage, options);
395
d96e3c15 396 finalize_colopts(&colopts, -1);
ac4cc866 397 if (cmdmode == 'l' && filter.lines != -1) {
d96e3c15
NTND
398 if (explicitly_enable_column(colopts))
399 die(_("--column and -n are incompatible"));
400 colopts = 0;
401 }
b7cc53e9
KN
402 if (!sorting)
403 sorting = ref_default_sorting();
e6b722db 404 if (cmdmode == 'l') {
d96e3c15
NTND
405 int ret;
406 if (column_active(colopts)) {
407 struct column_options copts;
408 memset(&copts, 0, sizeof(copts));
409 copts.padding = 2;
410 run_column_filter(colopts, &copts);
411 }
ac4cc866 412 filter.name_patterns = argv;
df094741 413 ret = list_tags(&filter, sorting, format);
d96e3c15
NTND
414 if (column_active(colopts))
415 stop_column_filter();
416 return ret;
417 }
ac4cc866 418 if (filter.lines != -1)
d08ebf99 419 die(_("-n option is only allowed with -l."));
ac4cc866 420 if (filter.with_commit)
d08ebf99 421 die(_("--contains option is only allowed with -l."));
ac4cc866 422 if (filter.points_at.nr)
ae7706b9 423 die(_("--points-at option is only allowed with -l."));
5242860f
KN
424 if (filter.merge_commit)
425 die(_("--merged and --no-merged option are only allowed with -l"));
e6b722db 426 if (cmdmode == 'd')
39686585 427 return for_each_tag_name(argv, delete_tag);
e6b722db 428 if (cmdmode == 'v')
39686585
CR
429 return for_each_tag_name(argv, verify_tag);
430
bd46c9a9
JH
431 if (msg.given || msgfile) {
432 if (msg.given && msgfile)
d08ebf99 433 die(_("only one -F or -m option is allowed."));
39686585 434 annotate = 1;
bd46c9a9
JH
435 if (msg.given)
436 strbuf_addbuf(&buf, &(msg.buf));
39686585
CR
437 else {
438 if (!strcmp(msgfile, "-")) {
387e7e19 439 if (strbuf_read(&buf, 0, 1024) < 0)
d08ebf99 440 die_errno(_("cannot read '%s'"), msgfile);
387e7e19 441 } else {
39686585 442 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
d08ebf99 443 die_errno(_("could not open or read '%s'"),
d824cbba 444 msgfile);
62e09ce9 445 }
62e09ce9 446 }
62e09ce9
CR
447 }
448
39686585 449 tag = argv[0];
62e09ce9 450
39686585
CR
451 object_ref = argc == 2 ? argv[1] : "HEAD";
452 if (argc > 2)
d08ebf99 453 die(_("too many params"));
62e09ce9
CR
454
455 if (get_sha1(object_ref, object))
d08ebf99 456 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
62e09ce9 457
4f0accd6 458 if (strbuf_check_tag_ref(&ref, tag))
d08ebf99 459 die(_("'%s' is not a valid tag name."), tag);
62e09ce9 460
c6893323 461 if (read_ref(ref.buf, prev))
62e09ce9
CR
462 hashclr(prev);
463 else if (!force)
d08ebf99 464 die(_("tag '%s' already exists"), tag);
62e09ce9 465
d3e05983
KS
466 opt.message_given = msg.given || msgfile;
467
468 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
469 opt.cleanup_mode = CLEANUP_ALL;
470 else if (!strcmp(cleanup_arg, "verbatim"))
471 opt.cleanup_mode = CLEANUP_NONE;
472 else if (!strcmp(cleanup_arg, "whitespace"))
473 opt.cleanup_mode = CLEANUP_SPACE;
474 else
475 die(_("Invalid cleanup mode %s"), cleanup_arg);
476
62e09ce9 477 if (annotate)
d3e05983 478 create_tag(object, tag, &buf, &opt, prev, object);
62e09ce9 479
e5074bfe
RS
480 transaction = ref_transaction_begin(&err);
481 if (!transaction ||
482 ref_transaction_update(transaction, ref.buf, object, prev,
144c76fa
DT
483 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
484 NULL, &err) ||
db7516ab 485 ref_transaction_commit(transaction, &err))
e5074bfe
RS
486 die("%s", err.buf);
487 ref_transaction_free(transaction);
3ae851e6 488 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
d08ebf99 489 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
62e09ce9 490
e5074bfe 491 strbuf_release(&err);
fd17f5b5 492 strbuf_release(&buf);
4f0accd6 493 strbuf_release(&ref);
62e09ce9
CR
494 return 0;
495}