ref-filter: implement '--contains' option
[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[] = {
9c9b4f2f 22 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
c88bba18 23 N_("git tag -d <tagname>..."),
9c9b4f2f 24 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
c88bba18
NTND
25 "\n\t\t[<pattern>...]"),
26 N_("git tag -v <tagname>..."),
39686585
CR
27 NULL
28};
62e09ce9 29
9ef176b5
NTND
30#define STRCMP_SORT 0 /* must be zero */
31#define VERCMP_SORT 1
32#define SORT_MASK 0x7fff
33#define REVERSE_SORT 0x8000
34
b150794d
JK
35static int tag_sort;
36
62e09ce9 37struct tag_filter {
588d0e83 38 const char **patterns;
62e09ce9 39 int lines;
9ef176b5
NTND
40 int sort;
41 struct string_list tags;
32c35cfb 42 struct commit_list *with_commit;
62e09ce9
CR
43};
44
ae7706b9 45static struct sha1_array points_at;
d96e3c15 46static unsigned int colopts;
ae7706b9 47
588d0e83
JK
48static int match_pattern(const char **patterns, const char *ref)
49{
50 /* no pattern means match everything */
51 if (!*patterns)
52 return 1;
53 for (; *patterns; patterns++)
eb07894f 54 if (!wildmatch(*patterns, ref, 0, NULL))
588d0e83
JK
55 return 1;
56 return 0;
57}
58
68411046
KN
59/*
60 * This is currently duplicated in ref-filter.c, and will eventually be
61 * removed as we port tag.c to use the ref-filter APIs.
62 */
ae7706b9
TG
63static const unsigned char *match_points_at(const char *refname,
64 const unsigned char *sha1)
65{
66 const unsigned char *tagged_sha1 = NULL;
67 struct object *obj;
68
69 if (sha1_array_lookup(&points_at, sha1) >= 0)
70 return sha1;
71 obj = parse_object(sha1);
72 if (!obj)
73 die(_("malformed object at '%s'"), refname);
74 if (obj->type == OBJ_TAG)
75 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
76 if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
77 return tagged_sha1;
78 return NULL;
79}
80
ffc4b801
JK
81static int in_commit_list(const struct commit_list *want, struct commit *c)
82{
83 for (; want; want = want->next)
84 if (!hashcmp(want->item->object.sha1, c->object.sha1))
85 return 1;
86 return 0;
87}
88
ee2bd06b
KN
89/*
90 * The entire code segment for supporting the --contains option has been
91 * copied over to ref-filter.{c,h}. This will be deleted evetually when
92 * we port tag.c to use ref-filter APIs.
93 */
cbc60b67
JJL
94enum contains_result {
95 CONTAINS_UNKNOWN = -1,
96 CONTAINS_NO = 0,
95acfc24 97 CONTAINS_YES = 1
cbc60b67
JJL
98};
99
100/*
101 * Test whether the candidate or one of its parents is contained in the list.
102 * Do not recurse to find out, though, but return -1 if inconclusive.
103 */
104static enum contains_result contains_test(struct commit *candidate,
c6d72c49 105 const struct commit_list *want)
ffc4b801 106{
ffc4b801
JK
107 /* was it previously marked as containing a want commit? */
108 if (candidate->object.flags & TMP_MARK)
109 return 1;
110 /* or marked as not possibly containing a want commit? */
111 if (candidate->object.flags & UNINTERESTING)
112 return 0;
113 /* or are we it? */
cbc60b67
JJL
114 if (in_commit_list(want, candidate)) {
115 candidate->object.flags |= TMP_MARK;
ffc4b801 116 return 1;
cbc60b67 117 }
ffc4b801
JK
118
119 if (parse_commit(candidate) < 0)
120 return 0;
121
cbc60b67
JJL
122 return -1;
123}
124
125/*
126 * Mimicking the real stack, this stack lives on the heap, avoiding stack
127 * overflows.
128 *
129 * At each recursion step, the stack items points to the commits whose
130 * ancestors are to be inspected.
131 */
132struct stack {
133 int nr, alloc;
134 struct stack_entry {
135 struct commit *commit;
136 struct commit_list *parents;
137 } *stack;
138};
139
140static void push_to_stack(struct commit *candidate, struct stack *stack)
141{
142 int index = stack->nr++;
143 ALLOC_GROW(stack->stack, stack->nr, stack->alloc);
144 stack->stack[index].commit = candidate;
145 stack->stack[index].parents = candidate->parents;
ffc4b801
JK
146}
147
cbc60b67
JJL
148static enum contains_result contains(struct commit *candidate,
149 const struct commit_list *want)
ffc4b801 150{
cbc60b67
JJL
151 struct stack stack = { 0, 0, NULL };
152 int result = contains_test(candidate, want);
153
154 if (result != CONTAINS_UNKNOWN)
155 return result;
156
157 push_to_stack(candidate, &stack);
158 while (stack.nr) {
159 struct stack_entry *entry = &stack.stack[stack.nr - 1];
160 struct commit *commit = entry->commit;
161 struct commit_list *parents = entry->parents;
162
163 if (!parents) {
164 commit->object.flags |= UNINTERESTING;
165 stack.nr--;
166 }
167 /*
168 * If we just popped the stack, parents->item has been marked,
169 * therefore contains_test will return a meaningful 0 or 1.
170 */
171 else switch (contains_test(parents->item, want)) {
172 case CONTAINS_YES:
173 commit->object.flags |= TMP_MARK;
174 stack.nr--;
175 break;
176 case CONTAINS_NO:
177 entry->parents = parents->next;
178 break;
179 case CONTAINS_UNKNOWN:
180 push_to_stack(parents->item, &stack);
181 break;
182 }
183 }
184 free(stack.stack);
185 return contains_test(candidate, want);
ffc4b801
JK
186}
187
6c4461e8 188static void show_tag_lines(const struct object_id *oid, int lines)
ca516999
JK
189{
190 int i;
191 unsigned long size;
192 enum object_type type;
193 char *buf, *sp, *eol;
194 size_t len;
195
6c4461e8 196 buf = read_sha1_file(oid->hash, &type, &size);
fb630e04 197 if (!buf)
6c4461e8 198 die_errno("unable to read object %s", oid_to_hex(oid));
31fd8d72
JH
199 if (type != OBJ_COMMIT && type != OBJ_TAG)
200 goto free_return;
201 if (!size)
202 die("an empty %s object %s?",
6c4461e8 203 typename(type), oid_to_hex(oid));
ca516999
JK
204
205 /* skip header */
206 sp = strstr(buf, "\n\n");
31fd8d72
JH
207 if (!sp)
208 goto free_return;
209
210 /* only take up to "lines" lines, and strip the signature from a tag */
211 if (type == OBJ_TAG)
212 size = parse_signature(buf, size);
ca516999
JK
213 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
214 if (i)
215 printf("\n ");
216 eol = memchr(sp, '\n', size - (sp - buf));
217 len = eol ? eol - sp : size - (sp - buf);
218 fwrite(sp, len, 1, stdout);
219 if (!eol)
220 break;
221 sp = eol + 1;
222 }
31fd8d72 223free_return:
ca516999
JK
224 free(buf);
225}
226
6c4461e8 227static int show_reference(const char *refname, const struct object_id *oid,
62e09ce9
CR
228 int flag, void *cb_data)
229{
230 struct tag_filter *filter = cb_data;
231
588d0e83 232 if (match_pattern(filter->patterns, refname)) {
32c35cfb
JG
233 if (filter->with_commit) {
234 struct commit *commit;
235
6c4461e8 236 commit = lookup_commit_reference_gently(oid->hash, 1);
32c35cfb
JG
237 if (!commit)
238 return 0;
ffc4b801 239 if (!contains(commit, filter->with_commit))
32c35cfb
JG
240 return 0;
241 }
242
6c4461e8 243 if (points_at.nr && !match_points_at(refname, oid->hash))
ae7706b9
TG
244 return 0;
245
62e09ce9 246 if (!filter->lines) {
9ef176b5
NTND
247 if (filter->sort)
248 string_list_append(&filter->tags, refname);
249 else
250 printf("%s\n", refname);
62e09ce9
CR
251 return 0;
252 }
253 printf("%-15s ", refname);
6c4461e8 254 show_tag_lines(oid, filter->lines);
62e09ce9 255 putchar('\n');
62e09ce9
CR
256 }
257
258 return 0;
259}
260
9ef176b5
NTND
261static int sort_by_version(const void *a_, const void *b_)
262{
263 const struct string_list_item *a = a_;
264 const struct string_list_item *b = b_;
265 return versioncmp(a->string, b->string);
266}
267
588d0e83 268static int list_tags(const char **patterns, int lines,
9ef176b5 269 struct commit_list *with_commit, int sort)
62e09ce9
CR
270{
271 struct tag_filter filter;
62e09ce9 272
588d0e83 273 filter.patterns = patterns;
62e09ce9 274 filter.lines = lines;
9ef176b5 275 filter.sort = sort;
32c35cfb 276 filter.with_commit = with_commit;
9ef176b5
NTND
277 memset(&filter.tags, 0, sizeof(filter.tags));
278 filter.tags.strdup_strings = 1;
62e09ce9 279
6c4461e8 280 for_each_tag_ref(show_reference, (void *)&filter);
9ef176b5
NTND
281 if (sort) {
282 int i;
283 if ((sort & SORT_MASK) == VERCMP_SORT)
284 qsort(filter.tags.items, filter.tags.nr,
285 sizeof(struct string_list_item), sort_by_version);
286 if (sort & REVERSE_SORT)
287 for (i = filter.tags.nr - 1; i >= 0; i--)
288 printf("%s\n", filter.tags.items[i].string);
289 else
290 for (i = 0; i < filter.tags.nr; i++)
291 printf("%s\n", filter.tags.items[i].string);
292 string_list_clear(&filter.tags, 0);
293 }
62e09ce9
CR
294 return 0;
295}
296
e317cfaf 297typedef int (*each_tag_name_fn)(const char *name, const char *ref,
62e09ce9
CR
298 const unsigned char *sha1);
299
e317cfaf 300static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
62e09ce9
CR
301{
302 const char **p;
303 char ref[PATH_MAX];
304 int had_error = 0;
305 unsigned char sha1[20];
306
307 for (p = argv; *p; p++) {
308 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
309 >= sizeof(ref)) {
d08ebf99 310 error(_("tag name too long: %.*s..."), 50, *p);
62e09ce9
CR
311 had_error = 1;
312 continue;
313 }
c6893323 314 if (read_ref(ref, sha1)) {
d08ebf99 315 error(_("tag '%s' not found."), *p);
62e09ce9
CR
316 had_error = 1;
317 continue;
318 }
319 if (fn(*p, ref, sha1))
320 had_error = 1;
321 }
322 return had_error;
323}
324
325static int delete_tag(const char *name, const char *ref,
326 const unsigned char *sha1)
327{
eca35a25 328 if (delete_ref(ref, sha1, 0))
62e09ce9 329 return 1;
d08ebf99 330 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
62e09ce9
CR
331 return 0;
332}
333
334static int verify_tag(const char *name, const char *ref,
335 const unsigned char *sha1)
336{
a6ccbbdb 337 const char *argv_verify_tag[] = {"verify-tag",
62e09ce9
CR
338 "-v", "SHA1_HEX", NULL};
339 argv_verify_tag[2] = sha1_to_hex(sha1);
340
a6ccbbdb 341 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
d08ebf99 342 return error(_("could not verify the tag '%s'"), name);
62e09ce9
CR
343 return 0;
344}
345
fd17f5b5 346static int do_sign(struct strbuf *buffer)
62e09ce9 347{
2f47eae2 348 return sign_buffer(buffer, buffer, get_signing_key());
62e09ce9
CR
349}
350
351static const char tag_template[] =
d78f340e 352 N_("\nWrite a message for tag:\n %s\n"
eff80a9f 353 "Lines starting with '%c' will be ignored.\n");
d3e05983
KS
354
355static const char tag_template_nocleanup[] =
d78f340e 356 N_("\nWrite a message for tag:\n %s\n"
eff80a9f
JH
357 "Lines starting with '%c' will be kept; you may remove them"
358 " yourself if you want to.\n");
62e09ce9 359
b150794d
JK
360/*
361 * Parse a sort string, and return 0 if parsed successfully. Will return
362 * non-zero when the sort string does not parse into a known type. If var is
363 * given, the error message becomes a warning and includes information about
364 * the configuration value.
365 */
366static int parse_sort_string(const char *var, const char *arg, int *sort)
367{
368 int type = 0, flags = 0;
369
370 if (skip_prefix(arg, "-", &arg))
371 flags |= REVERSE_SORT;
372
373 if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
374 type = VERCMP_SORT;
375 else
376 type = STRCMP_SORT;
377
378 if (strcmp(arg, "refname")) {
379 if (!var)
380 return error(_("unsupported sort specification '%s'"), arg);
381 else {
382 warning(_("unsupported sort specification '%s' in variable '%s'"),
383 var, arg);
384 return -1;
385 }
386 }
387
388 *sort = (type | flags);
389
390 return 0;
391}
392
ef90d6d4 393static int git_tag_config(const char *var, const char *value, void *cb)
62e09ce9 394{
b150794d
JK
395 int status;
396
397 if (!strcmp(var, "tag.sort")) {
398 if (!value)
399 return config_error_nonbool(var);
400 parse_sort_string(var, value, &tag_sort);
401 return 0;
402 }
403
404 status = git_gpg_config(var, value, cb);
2f47eae2
JH
405 if (status)
406 return status;
59556548 407 if (starts_with(var, "column."))
d96e3c15 408 return git_column_config(var, value, "tag", &colopts);
ef90d6d4 409 return git_default_config(var, value, cb);
62e09ce9
CR
410}
411
bab8118a
MH
412static void write_tag_body(int fd, const unsigned char *sha1)
413{
414 unsigned long size;
415 enum object_type type;
e10dfb62 416 char *buf, *sp;
bab8118a
MH
417
418 buf = read_sha1_file(sha1, &type, &size);
419 if (!buf)
420 return;
421 /* skip header */
422 sp = strstr(buf, "\n\n");
423
424 if (!sp || !size || type != OBJ_TAG) {
425 free(buf);
426 return;
427 }
428 sp += 2; /* skip the 2 LFs */
e10dfb62 429 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
bab8118a
MH
430
431 free(buf);
432}
433
3927bbe9
JK
434static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
435{
436 if (sign && do_sign(buf) < 0)
d08ebf99 437 return error(_("unable to sign the tag"));
3927bbe9 438 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
d08ebf99 439 return error(_("unable to write tag file"));
3927bbe9
JK
440 return 0;
441}
442
d3e05983
KS
443struct create_tag_options {
444 unsigned int message_given:1;
445 unsigned int sign;
446 enum {
447 CLEANUP_NONE,
448 CLEANUP_SPACE,
449 CLEANUP_ALL
450 } cleanup_mode;
451};
452
62e09ce9 453static void create_tag(const unsigned char *object, const char *tag,
d3e05983 454 struct strbuf *buf, struct create_tag_options *opt,
bd46c9a9 455 unsigned char *prev, unsigned char *result)
62e09ce9
CR
456{
457 enum object_type type;
fd17f5b5
PH
458 char header_buf[1024];
459 int header_len;
3927bbe9 460 char *path = NULL;
62e09ce9
CR
461
462 type = sha1_object_info(object, NULL);
e317cfaf 463 if (type <= OBJ_NONE)
d08ebf99 464 die(_("bad object type."));
62e09ce9
CR
465
466 header_len = snprintf(header_buf, sizeof(header_buf),
467 "object %s\n"
468 "type %s\n"
469 "tag %s\n"
470 "tagger %s\n\n",
471 sha1_to_hex(object),
472 typename(type),
473 tag,
f9bc573f 474 git_committer_info(IDENT_STRICT));
62e09ce9 475
e317cfaf 476 if (header_len > sizeof(header_buf) - 1)
d08ebf99 477 die(_("tag header too big."));
62e09ce9 478
d3e05983 479 if (!opt->message_given) {
62e09ce9
CR
480 int fd;
481
482 /* write the template message before editing: */
a4f34cbb 483 path = git_pathdup("TAG_EDITMSG");
62e09ce9
CR
484 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
485 if (fd < 0)
d08ebf99 486 die_errno(_("could not create file '%s'"), path);
bab8118a 487
eff80a9f 488 if (!is_null_sha1(prev)) {
bab8118a 489 write_tag_body(fd, prev);
eff80a9f
JH
490 } else {
491 struct strbuf buf = STRBUF_INIT;
492 strbuf_addch(&buf, '\n');
493 if (opt->cleanup_mode == CLEANUP_ALL)
d78f340e 494 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
eff80a9f 495 else
d78f340e 496 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
eff80a9f
JH
497 write_or_die(fd, buf.buf, buf.len);
498 strbuf_release(&buf);
499 }
62e09ce9
CR
500 close(fd);
501
7198203a
SB
502 if (launch_editor(path, buf, NULL)) {
503 fprintf(stderr,
d08ebf99 504 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
505 exit(1);
506 }
62e09ce9 507 }
62e09ce9 508
d3e05983
KS
509 if (opt->cleanup_mode != CLEANUP_NONE)
510 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
62e09ce9 511
d3e05983 512 if (!opt->message_given && !buf->len)
d08ebf99 513 die(_("no tag message?"));
62e09ce9 514
fd17f5b5 515 strbuf_insert(buf, 0, header_buf, header_len);
62e09ce9 516
d3e05983 517 if (build_tag_object(buf, opt->sign, result) < 0) {
3927bbe9 518 if (path)
d08ebf99 519 fprintf(stderr, _("The tag message has been left in %s\n"),
3927bbe9
JK
520 path);
521 exit(128);
522 }
523 if (path) {
691f1a28 524 unlink_or_warn(path);
3927bbe9
JK
525 free(path);
526 }
62e09ce9
CR
527}
528
bd46c9a9
JH
529struct msg_arg {
530 int given;
531 struct strbuf buf;
532};
533
534static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
535{
536 struct msg_arg *msg = opt->value;
537
538 if (!arg)
539 return -1;
540 if (msg->buf.len)
541 strbuf_addstr(&(msg->buf), "\n\n");
542 strbuf_addstr(&(msg->buf), arg);
543 msg->given = 1;
544 return 0;
545}
546
4f0accd6
MS
547static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
548{
549 if (name[0] == '-')
8d9c5010 550 return -1;
4f0accd6
MS
551
552 strbuf_reset(sb);
553 strbuf_addf(sb, "refs/tags/%s", name);
554
8d9c5010 555 return check_refname_format(sb->buf, 0);
4f0accd6
MS
556}
557
9ef176b5
NTND
558static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
559{
560 int *sort = opt->value;
9ef176b5 561
b150794d 562 return parse_sort_string(NULL, arg, sort);
9ef176b5
NTND
563}
564
62e09ce9
CR
565int cmd_tag(int argc, const char **argv, const char *prefix)
566{
f285a2d7 567 struct strbuf buf = STRBUF_INIT;
4f0accd6 568 struct strbuf ref = STRBUF_INIT;
62e09ce9 569 unsigned char object[20], prev[20];
62e09ce9 570 const char *object_ref, *tag;
d3e05983
KS
571 struct create_tag_options opt;
572 char *cleanup_arg = NULL;
e6b722db 573 int annotate = 0, force = 0, lines = -1;
b150794d 574 int cmdmode = 0;
dbd0f5c7 575 const char *msgfile = NULL, *keyid = NULL;
bd46c9a9 576 struct msg_arg msg = { 0, STRBUF_INIT };
32c35cfb 577 struct commit_list *with_commit = NULL;
e5074bfe
RS
578 struct ref_transaction *transaction;
579 struct strbuf err = STRBUF_INIT;
39686585 580 struct option options[] = {
e6b722db 581 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
c88bba18
NTND
582 { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
583 N_("print <n> lines of each tag message"),
39686585 584 PARSE_OPT_OPTARG, NULL, 1 },
e6b722db
JH
585 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
586 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
39686585 587
c88bba18 588 OPT_GROUP(N_("Tag creation options")),
d5d09d47 589 OPT_BOOL('a', "annotate", &annotate,
c88bba18
NTND
590 N_("annotated tag, needs a message")),
591 OPT_CALLBACK('m', "message", &msg, N_("message"),
592 N_("tag message"), parse_msg_arg),
593 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
d5d09d47 594 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
c88bba18
NTND
595 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
596 N_("how to strip spaces and #comments from message")),
e703d711 597 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
c88bba18
NTND
598 N_("use another key to sign the tag")),
599 OPT__FORCE(&force, N_("replace the tag if exists")),
dd059c6c
JK
600
601 OPT_GROUP(N_("Tag listing options")),
c88bba18 602 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
f266c916
KN
603 OPT_CONTAINS(&with_commit, N_("print only tags that contain the commit")),
604 OPT_WITH(&with_commit, N_("print only tags that contain the commit")),
9ef176b5 605 {
b150794d 606 OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
9ef176b5
NTND
607 PARSE_OPT_NONEG, parse_opt_sort
608 },
ae7706b9 609 {
b2172fdf
KN
610 OPTION_CALLBACK, 0, "points-at", &points_at, N_("object"),
611 N_("print only tags of the object"), 0, parse_opt_object_name
ae7706b9 612 },
39686585
CR
613 OPT_END()
614 };
615
ef90d6d4 616 git_config(git_tag_config, NULL);
62e09ce9 617
d3e05983
KS
618 memset(&opt, 0, sizeof(opt));
619
37782920 620 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
62e09ce9 621
be15f505 622 if (keyid) {
d3e05983 623 opt.sign = 1;
2f47eae2 624 set_signing_key(keyid);
be15f505 625 }
d3e05983 626 if (opt.sign)
c1a41b9d 627 annotate = 1;
e6b722db
JH
628 if (argc == 0 && !cmdmode)
629 cmdmode = 'l';
c1a41b9d 630
e6b722db 631 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
6fa8342b
ST
632 usage_with_options(git_tag_usage, options);
633
d96e3c15 634 finalize_colopts(&colopts, -1);
e6b722db 635 if (cmdmode == 'l' && lines != -1) {
d96e3c15
NTND
636 if (explicitly_enable_column(colopts))
637 die(_("--column and -n are incompatible"));
638 colopts = 0;
639 }
e6b722db 640 if (cmdmode == 'l') {
d96e3c15
NTND
641 int ret;
642 if (column_active(colopts)) {
643 struct column_options copts;
644 memset(&copts, 0, sizeof(copts));
645 copts.padding = 2;
646 run_column_filter(colopts, &copts);
647 }
b150794d 648 if (lines != -1 && tag_sort)
9ef176b5 649 die(_("--sort and -n are incompatible"));
b150794d 650 ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
d96e3c15
NTND
651 if (column_active(colopts))
652 stop_column_filter();
653 return ret;
654 }
6fa8342b 655 if (lines != -1)
d08ebf99 656 die(_("-n option is only allowed with -l."));
32c35cfb 657 if (with_commit)
d08ebf99 658 die(_("--contains option is only allowed with -l."));
ae7706b9
TG
659 if (points_at.nr)
660 die(_("--points-at option is only allowed with -l."));
e6b722db 661 if (cmdmode == 'd')
39686585 662 return for_each_tag_name(argv, delete_tag);
e6b722db 663 if (cmdmode == 'v')
39686585
CR
664 return for_each_tag_name(argv, verify_tag);
665
bd46c9a9
JH
666 if (msg.given || msgfile) {
667 if (msg.given && msgfile)
d08ebf99 668 die(_("only one -F or -m option is allowed."));
39686585 669 annotate = 1;
bd46c9a9
JH
670 if (msg.given)
671 strbuf_addbuf(&buf, &(msg.buf));
39686585
CR
672 else {
673 if (!strcmp(msgfile, "-")) {
387e7e19 674 if (strbuf_read(&buf, 0, 1024) < 0)
d08ebf99 675 die_errno(_("cannot read '%s'"), msgfile);
387e7e19 676 } else {
39686585 677 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
d08ebf99 678 die_errno(_("could not open or read '%s'"),
d824cbba 679 msgfile);
62e09ce9 680 }
62e09ce9 681 }
62e09ce9
CR
682 }
683
39686585 684 tag = argv[0];
62e09ce9 685
39686585
CR
686 object_ref = argc == 2 ? argv[1] : "HEAD";
687 if (argc > 2)
d08ebf99 688 die(_("too many params"));
62e09ce9
CR
689
690 if (get_sha1(object_ref, object))
d08ebf99 691 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
62e09ce9 692
4f0accd6 693 if (strbuf_check_tag_ref(&ref, tag))
d08ebf99 694 die(_("'%s' is not a valid tag name."), tag);
62e09ce9 695
c6893323 696 if (read_ref(ref.buf, prev))
62e09ce9
CR
697 hashclr(prev);
698 else if (!force)
d08ebf99 699 die(_("tag '%s' already exists"), tag);
62e09ce9 700
d3e05983
KS
701 opt.message_given = msg.given || msgfile;
702
703 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
704 opt.cleanup_mode = CLEANUP_ALL;
705 else if (!strcmp(cleanup_arg, "verbatim"))
706 opt.cleanup_mode = CLEANUP_NONE;
707 else if (!strcmp(cleanup_arg, "whitespace"))
708 opt.cleanup_mode = CLEANUP_SPACE;
709 else
710 die(_("Invalid cleanup mode %s"), cleanup_arg);
711
62e09ce9 712 if (annotate)
d3e05983 713 create_tag(object, tag, &buf, &opt, prev, object);
62e09ce9 714
e5074bfe
RS
715 transaction = ref_transaction_begin(&err);
716 if (!transaction ||
717 ref_transaction_update(transaction, ref.buf, object, prev,
1d147bdf 718 0, NULL, &err) ||
db7516ab 719 ref_transaction_commit(transaction, &err))
e5074bfe
RS
720 die("%s", err.buf);
721 ref_transaction_free(transaction);
3ae851e6 722 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
d08ebf99 723 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
62e09ce9 724
e5074bfe 725 strbuf_release(&err);
fd17f5b5 726 strbuf_release(&buf);
4f0accd6 727 strbuf_release(&ref);
62e09ce9
CR
728 return 0;
729}