blob: e633f4efdbb8963449fddb5c3357fa657283430e [file] [log] [blame]
Carlos Rica62e09ce2007-07-20 01:42:28 +02001/*
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"
Carlos Rica39686582007-11-09 14:42:56 +010014#include "parse-options.h"
Jeff Kingffc4b802011-06-11 19:04:08 +000015#include "diff.h"
16#include "revision.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -070017#include "gpg-interface.h"
Tom Grennanae7706b2012-02-08 15:03:43 -080018#include "sha1-array.h"
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +070019#include "column.h"
Carlos Rica62e09ce2007-07-20 01:42:28 +020020
Carlos Rica39686582007-11-09 14:42:56 +010021static const char * const git_tag_usage[] = {
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +070022 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>..."),
Carlos Rica39686582007-11-09 14:42:56 +010027 NULL
28};
Carlos Rica62e09ce2007-07-20 01:42:28 +020029
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +070030#define STRCMP_SORT 0 /* must be zero */
31#define VERCMP_SORT 1
32#define SORT_MASK 0x7fff
33#define REVERSE_SORT 0x8000
34
Jacob Kellerb1507942014-07-16 14:48:02 -070035static int tag_sort;
36
Carlos Rica62e09ce2007-07-20 01:42:28 +020037struct tag_filter {
Jeff King588d0e82011-06-20 12:59:28 -040038 const char **patterns;
Carlos Rica62e09ce2007-07-20 01:42:28 +020039 int lines;
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +070040 int sort;
41 struct string_list tags;
Jake Goulding32c35cf2009-01-26 09:13:25 -050042 struct commit_list *with_commit;
Carlos Rica62e09ce2007-07-20 01:42:28 +020043};
44
Tom Grennanae7706b2012-02-08 15:03:43 -080045static struct sha1_array points_at;
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +070046static unsigned int colopts;
Tom Grennanae7706b2012-02-08 15:03:43 -080047
Jeff King588d0e82011-06-20 12:59:28 -040048static 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++)
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +070054 if (!wildmatch(*patterns, ref, 0, NULL))
Jeff King588d0e82011-06-20 12:59:28 -040055 return 1;
56 return 0;
57}
58
Tom Grennanae7706b2012-02-08 15:03:43 -080059static const unsigned char *match_points_at(const char *refname,
60 const unsigned char *sha1)
61{
62 const unsigned char *tagged_sha1 = NULL;
63 struct object *obj;
64
65 if (sha1_array_lookup(&points_at, sha1) >= 0)
66 return sha1;
67 obj = parse_object(sha1);
68 if (!obj)
69 die(_("malformed object at '%s'"), refname);
70 if (obj->type == OBJ_TAG)
71 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
72 if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
73 return tagged_sha1;
74 return NULL;
75}
76
Jeff Kingffc4b802011-06-11 19:04:08 +000077static int in_commit_list(const struct commit_list *want, struct commit *c)
78{
79 for (; want; want = want->next)
80 if (!hashcmp(want->item->object.sha1, c->object.sha1))
81 return 1;
82 return 0;
83}
84
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +020085enum contains_result {
86 CONTAINS_UNKNOWN = -1,
87 CONTAINS_NO = 0,
Ronnie Sahlberg95acfc22014-07-02 11:24:05 -070088 CONTAINS_YES = 1
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +020089};
90
91/*
92 * Test whether the candidate or one of its parents is contained in the list.
93 * Do not recurse to find out, though, but return -1 if inconclusive.
94 */
95static enum contains_result contains_test(struct commit *candidate,
Junio C Hamanoc6d72c42011-07-14 11:02:06 -070096 const struct commit_list *want)
Jeff Kingffc4b802011-06-11 19:04:08 +000097{
Jeff Kingffc4b802011-06-11 19:04:08 +000098 /* was it previously marked as containing a want commit? */
99 if (candidate->object.flags & TMP_MARK)
100 return 1;
101 /* or marked as not possibly containing a want commit? */
102 if (candidate->object.flags & UNINTERESTING)
103 return 0;
104 /* or are we it? */
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +0200105 if (in_commit_list(want, candidate)) {
106 candidate->object.flags |= TMP_MARK;
Jeff Kingffc4b802011-06-11 19:04:08 +0000107 return 1;
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +0200108 }
Jeff Kingffc4b802011-06-11 19:04:08 +0000109
110 if (parse_commit(candidate) < 0)
111 return 0;
112
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +0200113 return -1;
Jeff Kingffc4b802011-06-11 19:04:08 +0000114}
115
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +0200116/*
117 * Mimicking the real stack, this stack lives on the heap, avoiding stack
118 * overflows.
119 *
120 * At each recursion step, the stack items points to the commits whose
121 * ancestors are to be inspected.
122 */
123struct stack {
124 int nr, alloc;
125 struct stack_entry {
126 struct commit *commit;
127 struct commit_list *parents;
128 } *stack;
129};
130
131static void push_to_stack(struct commit *candidate, struct stack *stack)
Jeff Kingffc4b802011-06-11 19:04:08 +0000132{
Jean-Jacques Lafaycbc60b62014-04-24 14:24:39 +0200133 int index = stack->nr++;
134 ALLOC_GROW(stack->stack, stack->nr, stack->alloc);
135 stack->stack[index].commit = candidate;
136 stack->stack[index].parents = candidate->parents;
137}
138
139static enum contains_result contains(struct commit *candidate,
140 const struct commit_list *want)
141{
142 struct stack stack = { 0, 0, NULL };
143 int result = contains_test(candidate, want);
144
145 if (result != CONTAINS_UNKNOWN)
146 return result;
147
148 push_to_stack(candidate, &stack);
149 while (stack.nr) {
150 struct stack_entry *entry = &stack.stack[stack.nr - 1];
151 struct commit *commit = entry->commit;
152 struct commit_list *parents = entry->parents;
153
154 if (!parents) {
155 commit->object.flags |= UNINTERESTING;
156 stack.nr--;
157 }
158 /*
159 * If we just popped the stack, parents->item has been marked,
160 * therefore contains_test will return a meaningful 0 or 1.
161 */
162 else switch (contains_test(parents->item, want)) {
163 case CONTAINS_YES:
164 commit->object.flags |= TMP_MARK;
165 stack.nr--;
166 break;
167 case CONTAINS_NO:
168 entry->parents = parents->next;
169 break;
170 case CONTAINS_UNKNOWN:
171 push_to_stack(parents->item, &stack);
172 break;
173 }
174 }
175 free(stack.stack);
176 return contains_test(candidate, want);
Jeff Kingffc4b802011-06-11 19:04:08 +0000177}
178
Jeff Kingca516992012-02-06 03:13:12 -0500179static void show_tag_lines(const unsigned char *sha1, int lines)
180{
181 int i;
182 unsigned long size;
183 enum object_type type;
184 char *buf, *sp, *eol;
185 size_t len;
186
187 buf = read_sha1_file(sha1, &type, &size);
Jeff Kingfb630e02012-02-06 03:13:42 -0500188 if (!buf)
189 die_errno("unable to read object %s", sha1_to_hex(sha1));
Junio C Hamano31fd8d72012-02-06 10:13:27 -0800190 if (type != OBJ_COMMIT && type != OBJ_TAG)
191 goto free_return;
192 if (!size)
193 die("an empty %s object %s?",
194 typename(type), sha1_to_hex(sha1));
Jeff Kingca516992012-02-06 03:13:12 -0500195
196 /* skip header */
197 sp = strstr(buf, "\n\n");
Junio C Hamano31fd8d72012-02-06 10:13:27 -0800198 if (!sp)
199 goto free_return;
200
201 /* only take up to "lines" lines, and strip the signature from a tag */
202 if (type == OBJ_TAG)
203 size = parse_signature(buf, size);
Jeff Kingca516992012-02-06 03:13:12 -0500204 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
205 if (i)
206 printf("\n ");
207 eol = memchr(sp, '\n', size - (sp - buf));
208 len = eol ? eol - sp : size - (sp - buf);
209 fwrite(sp, len, 1, stdout);
210 if (!eol)
211 break;
212 sp = eol + 1;
213 }
Junio C Hamano31fd8d72012-02-06 10:13:27 -0800214free_return:
Jeff Kingca516992012-02-06 03:13:12 -0500215 free(buf);
216}
217
Carlos Rica62e09ce2007-07-20 01:42:28 +0200218static int show_reference(const char *refname, const unsigned char *sha1,
219 int flag, void *cb_data)
220{
221 struct tag_filter *filter = cb_data;
222
Jeff King588d0e82011-06-20 12:59:28 -0400223 if (match_pattern(filter->patterns, refname)) {
Jake Goulding32c35cf2009-01-26 09:13:25 -0500224 if (filter->with_commit) {
225 struct commit *commit;
226
227 commit = lookup_commit_reference_gently(sha1, 1);
228 if (!commit)
229 return 0;
Jeff Kingffc4b802011-06-11 19:04:08 +0000230 if (!contains(commit, filter->with_commit))
Jake Goulding32c35cf2009-01-26 09:13:25 -0500231 return 0;
232 }
233
Tom Grennanae7706b2012-02-08 15:03:43 -0800234 if (points_at.nr && !match_points_at(refname, sha1))
235 return 0;
236
Carlos Rica62e09ce2007-07-20 01:42:28 +0200237 if (!filter->lines) {
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700238 if (filter->sort)
239 string_list_append(&filter->tags, refname);
240 else
241 printf("%s\n", refname);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200242 return 0;
243 }
244 printf("%-15s ", refname);
Jeff Kingca516992012-02-06 03:13:12 -0500245 show_tag_lines(sha1, filter->lines);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200246 putchar('\n');
Carlos Rica62e09ce2007-07-20 01:42:28 +0200247 }
248
249 return 0;
250}
251
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700252static int sort_by_version(const void *a_, const void *b_)
253{
254 const struct string_list_item *a = a_;
255 const struct string_list_item *b = b_;
256 return versioncmp(a->string, b->string);
257}
258
Jeff King588d0e82011-06-20 12:59:28 -0400259static int list_tags(const char **patterns, int lines,
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700260 struct commit_list *with_commit, int sort)
Carlos Rica62e09ce2007-07-20 01:42:28 +0200261{
262 struct tag_filter filter;
Carlos Rica62e09ce2007-07-20 01:42:28 +0200263
Jeff King588d0e82011-06-20 12:59:28 -0400264 filter.patterns = patterns;
Carlos Rica62e09ce2007-07-20 01:42:28 +0200265 filter.lines = lines;
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700266 filter.sort = sort;
Jake Goulding32c35cf2009-01-26 09:13:25 -0500267 filter.with_commit = with_commit;
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700268 memset(&filter.tags, 0, sizeof(filter.tags));
269 filter.tags.strdup_strings = 1;
Carlos Rica62e09ce2007-07-20 01:42:28 +0200270
271 for_each_tag_ref(show_reference, (void *) &filter);
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700272 if (sort) {
273 int i;
274 if ((sort & SORT_MASK) == VERCMP_SORT)
275 qsort(filter.tags.items, filter.tags.nr,
276 sizeof(struct string_list_item), sort_by_version);
277 if (sort & REVERSE_SORT)
278 for (i = filter.tags.nr - 1; i >= 0; i--)
279 printf("%s\n", filter.tags.items[i].string);
280 else
281 for (i = 0; i < filter.tags.nr; i++)
282 printf("%s\n", filter.tags.items[i].string);
283 string_list_clear(&filter.tags, 0);
284 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200285 return 0;
286}
287
Carlos Ricae317cfa2007-07-21 14:13:12 +0200288typedef int (*each_tag_name_fn)(const char *name, const char *ref,
Carlos Rica62e09ce2007-07-20 01:42:28 +0200289 const unsigned char *sha1);
290
Carlos Ricae317cfa2007-07-21 14:13:12 +0200291static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
Carlos Rica62e09ce2007-07-20 01:42:28 +0200292{
293 const char **p;
294 char ref[PATH_MAX];
295 int had_error = 0;
296 unsigned char sha1[20];
297
298 for (p = argv; *p; p++) {
299 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
300 >= sizeof(ref)) {
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000301 error(_("tag name too long: %.*s..."), 50, *p);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200302 had_error = 1;
303 continue;
304 }
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700305 if (read_ref(ref, sha1)) {
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000306 error(_("tag '%s' not found."), *p);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200307 had_error = 1;
308 continue;
309 }
310 if (fn(*p, ref, sha1))
311 had_error = 1;
312 }
313 return had_error;
314}
315
316static int delete_tag(const char *name, const char *ref,
317 const unsigned char *sha1)
318{
Miklos Vajnaeca35a22008-10-26 03:33:56 +0100319 if (delete_ref(ref, sha1, 0))
Carlos Rica62e09ce2007-07-20 01:42:28 +0200320 return 1;
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000321 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200322 return 0;
323}
324
325static int verify_tag(const char *name, const char *ref,
326 const unsigned char *sha1)
327{
Jonathan Niedera6ccbbd2010-04-15 04:36:25 -0500328 const char *argv_verify_tag[] = {"verify-tag",
Carlos Rica62e09ce2007-07-20 01:42:28 +0200329 "-v", "SHA1_HEX", NULL};
330 argv_verify_tag[2] = sha1_to_hex(sha1);
331
Jonathan Niedera6ccbbd2010-04-15 04:36:25 -0500332 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000333 return error(_("could not verify the tag '%s'"), name);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200334 return 0;
335}
336
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200337static int do_sign(struct strbuf *buffer)
Carlos Rica62e09ce2007-07-20 01:42:28 +0200338{
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700339 return sign_buffer(buffer, buffer, get_signing_key());
Carlos Rica62e09ce2007-07-20 01:42:28 +0200340}
341
342static const char tag_template[] =
Thorsten Glaserd78f3402013-08-30 00:03:10 +0200343 N_("\nWrite a message for tag:\n %s\n"
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100344 "Lines starting with '%c' will be ignored.\n");
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200345
346static const char tag_template_nocleanup[] =
Thorsten Glaserd78f3402013-08-30 00:03:10 +0200347 N_("\nWrite a message for tag:\n %s\n"
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100348 "Lines starting with '%c' will be kept; you may remove them"
349 " yourself if you want to.\n");
Carlos Rica62e09ce2007-07-20 01:42:28 +0200350
Jacob Kellerb1507942014-07-16 14:48:02 -0700351/*
352 * Parse a sort string, and return 0 if parsed successfully. Will return
353 * non-zero when the sort string does not parse into a known type. If var is
354 * given, the error message becomes a warning and includes information about
355 * the configuration value.
356 */
357static int parse_sort_string(const char *var, const char *arg, int *sort)
358{
359 int type = 0, flags = 0;
360
361 if (skip_prefix(arg, "-", &arg))
362 flags |= REVERSE_SORT;
363
364 if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
365 type = VERCMP_SORT;
366 else
367 type = STRCMP_SORT;
368
369 if (strcmp(arg, "refname")) {
370 if (!var)
371 return error(_("unsupported sort specification '%s'"), arg);
372 else {
373 warning(_("unsupported sort specification '%s' in variable '%s'"),
374 var, arg);
375 return -1;
376 }
377 }
378
379 *sort = (type | flags);
380
381 return 0;
382}
383
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100384static int git_tag_config(const char *var, const char *value, void *cb)
Carlos Rica62e09ce2007-07-20 01:42:28 +0200385{
Jacob Kellerb1507942014-07-16 14:48:02 -0700386 int status;
387
388 if (!strcmp(var, "tag.sort")) {
389 if (!value)
390 return config_error_nonbool(var);
391 parse_sort_string(var, value, &tag_sort);
392 return 0;
393 }
394
395 status = git_gpg_config(var, value, cb);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700396 if (status)
397 return status;
Christian Couder59556542013-11-30 21:55:40 +0100398 if (starts_with(var, "column."))
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +0700399 return git_column_config(var, value, "tag", &colopts);
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100400 return git_default_config(var, value, cb);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200401}
402
Mike Hommeybab81182007-11-04 01:11:14 +0100403static void write_tag_body(int fd, const unsigned char *sha1)
404{
405 unsigned long size;
406 enum object_type type;
Michael J Grubere10dfb62010-11-10 12:17:28 +0100407 char *buf, *sp;
Mike Hommeybab81182007-11-04 01:11:14 +0100408
409 buf = read_sha1_file(sha1, &type, &size);
410 if (!buf)
411 return;
412 /* skip header */
413 sp = strstr(buf, "\n\n");
414
415 if (!sp || !size || type != OBJ_TAG) {
416 free(buf);
417 return;
418 }
419 sp += 2; /* skip the 2 LFs */
Michael J Grubere10dfb62010-11-10 12:17:28 +0100420 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
Mike Hommeybab81182007-11-04 01:11:14 +0100421
422 free(buf);
423}
424
Jeff King3927bbe2008-12-06 14:40:34 -0500425static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
426{
427 if (sign && do_sign(buf) < 0)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000428 return error(_("unable to sign the tag"));
Jeff King3927bbe2008-12-06 14:40:34 -0500429 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000430 return error(_("unable to write tag file"));
Jeff King3927bbe2008-12-06 14:40:34 -0500431 return 0;
432}
433
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200434struct create_tag_options {
435 unsigned int message_given:1;
436 unsigned int sign;
437 enum {
438 CLEANUP_NONE,
439 CLEANUP_SPACE,
440 CLEANUP_ALL
441 } cleanup_mode;
442};
443
Carlos Rica62e09ce2007-07-20 01:42:28 +0200444static void create_tag(const unsigned char *object, const char *tag,
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200445 struct strbuf *buf, struct create_tag_options *opt,
Junio C Hamanobd46c9a2007-11-22 23:16:51 -0800446 unsigned char *prev, unsigned char *result)
Carlos Rica62e09ce2007-07-20 01:42:28 +0200447{
448 enum object_type type;
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200449 char header_buf[1024];
450 int header_len;
Jeff King3927bbe2008-12-06 14:40:34 -0500451 char *path = NULL;
Carlos Rica62e09ce2007-07-20 01:42:28 +0200452
453 type = sha1_object_info(object, NULL);
Carlos Ricae317cfa2007-07-21 14:13:12 +0200454 if (type <= OBJ_NONE)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000455 die(_("bad object type."));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200456
457 header_len = snprintf(header_buf, sizeof(header_buf),
458 "object %s\n"
459 "type %s\n"
460 "tag %s\n"
461 "tagger %s\n\n",
462 sha1_to_hex(object),
463 typename(type),
464 tag,
Jeff Kingf9bc5732012-05-24 19:28:40 -0400465 git_committer_info(IDENT_STRICT));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200466
Carlos Ricae317cfa2007-07-21 14:13:12 +0200467 if (header_len > sizeof(header_buf) - 1)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000468 die(_("tag header too big."));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200469
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200470 if (!opt->message_given) {
Carlos Rica62e09ce2007-07-20 01:42:28 +0200471 int fd;
472
473 /* write the template message before editing: */
Alex Riesena4f34cb2008-10-27 11:22:09 +0100474 path = git_pathdup("TAG_EDITMSG");
Carlos Rica62e09ce2007-07-20 01:42:28 +0200475 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
476 if (fd < 0)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000477 die_errno(_("could not create file '%s'"), path);
Mike Hommeybab81182007-11-04 01:11:14 +0100478
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100479 if (!is_null_sha1(prev)) {
Mike Hommeybab81182007-11-04 01:11:14 +0100480 write_tag_body(fd, prev);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100481 } else {
482 struct strbuf buf = STRBUF_INIT;
483 strbuf_addch(&buf, '\n');
484 if (opt->cleanup_mode == CLEANUP_ALL)
Thorsten Glaserd78f3402013-08-30 00:03:10 +0200485 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100486 else
Thorsten Glaserd78f3402013-08-30 00:03:10 +0200487 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100488 write_or_die(fd, buf.buf, buf.len);
489 strbuf_release(&buf);
490 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200491 close(fd);
492
Stephan Beyer71982032008-07-25 18:28:42 +0200493 if (launch_editor(path, buf, NULL)) {
494 fprintf(stderr,
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000495 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +0200496 exit(1);
497 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200498 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200499
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200500 if (opt->cleanup_mode != CLEANUP_NONE)
501 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200502
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200503 if (!opt->message_given && !buf->len)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000504 die(_("no tag message?"));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200505
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200506 strbuf_insert(buf, 0, header_buf, header_len);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200507
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200508 if (build_tag_object(buf, opt->sign, result) < 0) {
Jeff King3927bbe2008-12-06 14:40:34 -0500509 if (path)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000510 fprintf(stderr, _("The tag message has been left in %s\n"),
Jeff King3927bbe2008-12-06 14:40:34 -0500511 path);
512 exit(128);
513 }
514 if (path) {
Alex Riesen691f1a22009-04-29 23:22:56 +0200515 unlink_or_warn(path);
Jeff King3927bbe2008-12-06 14:40:34 -0500516 free(path);
517 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200518}
519
Junio C Hamanobd46c9a2007-11-22 23:16:51 -0800520struct msg_arg {
521 int given;
522 struct strbuf buf;
523};
524
525static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
526{
527 struct msg_arg *msg = opt->value;
528
529 if (!arg)
530 return -1;
531 if (msg->buf.len)
532 strbuf_addstr(&(msg->buf), "\n\n");
533 strbuf_addstr(&(msg->buf), arg);
534 msg->given = 1;
535 return 0;
536}
537
Michael Schubert4f0accd2011-05-10 01:36:36 +0200538static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
539{
540 if (name[0] == '-')
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200541 return -1;
Michael Schubert4f0accd2011-05-10 01:36:36 +0200542
543 strbuf_reset(sb);
544 strbuf_addf(sb, "refs/tags/%s", name);
545
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200546 return check_refname_format(sb->buf, 0);
Michael Schubert4f0accd2011-05-10 01:36:36 +0200547}
548
Ramsay Jones0975a502012-02-13 18:24:41 +0000549static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
Tom Grennanae7706b2012-02-08 15:03:43 -0800550 const char *arg, int unset)
551{
552 unsigned char sha1[20];
553
554 if (unset) {
555 sha1_array_clear(&points_at);
556 return 0;
557 }
558 if (!arg)
559 return error(_("switch 'points-at' requires an object"));
560 if (get_sha1(arg, sha1))
561 return error(_("malformed object name '%s'"), arg);
562 sha1_array_append(&points_at, sha1);
563 return 0;
564}
565
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700566static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
567{
568 int *sort = opt->value;
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700569
Jacob Kellerb1507942014-07-16 14:48:02 -0700570 return parse_sort_string(NULL, arg, sort);
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700571}
572
Carlos Rica62e09ce2007-07-20 01:42:28 +0200573int cmd_tag(int argc, const char **argv, const char *prefix)
574{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500575 struct strbuf buf = STRBUF_INIT;
Michael Schubert4f0accd2011-05-10 01:36:36 +0200576 struct strbuf ref = STRBUF_INIT;
Carlos Rica62e09ce2007-07-20 01:42:28 +0200577 unsigned char object[20], prev[20];
Carlos Rica62e09ce2007-07-20 01:42:28 +0200578 const char *object_ref, *tag;
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200579 struct create_tag_options opt;
580 char *cleanup_arg = NULL;
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700581 int annotate = 0, force = 0, lines = -1;
Jacob Kellerb1507942014-07-16 14:48:02 -0700582 int cmdmode = 0;
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700583 const char *msgfile = NULL, *keyid = NULL;
Junio C Hamanobd46c9a2007-11-22 23:16:51 -0800584 struct msg_arg msg = { 0, STRBUF_INIT };
Jake Goulding32c35cf2009-01-26 09:13:25 -0500585 struct commit_list *with_commit = NULL;
Ronnie Sahlberge5074bf2014-04-16 15:30:41 -0700586 struct ref_transaction *transaction;
587 struct strbuf err = STRBUF_INIT;
Carlos Rica39686582007-11-09 14:42:56 +0100588 struct option options[] = {
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700589 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700590 { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
591 N_("print <n> lines of each tag message"),
Carlos Rica39686582007-11-09 14:42:56 +0100592 PARSE_OPT_OPTARG, NULL, 1 },
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700593 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
594 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
Carlos Rica39686582007-11-09 14:42:56 +0100595
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700596 OPT_GROUP(N_("Tag creation options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200597 OPT_BOOL('a', "annotate", &annotate,
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700598 N_("annotated tag, needs a message")),
599 OPT_CALLBACK('m', "message", &msg, N_("message"),
600 N_("tag message"), parse_msg_arg),
601 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200602 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700603 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
604 N_("how to strip spaces and #comments from message")),
Junio C Hamanoe703d712014-03-23 15:58:12 -0700605 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700606 N_("use another key to sign the tag")),
607 OPT__FORCE(&force, N_("replace the tag if exists")),
608 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700609 {
Jacob Kellerb1507942014-07-16 14:48:02 -0700610 OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700611 PARSE_OPT_NONEG, parse_opt_sort
612 },
Jake Goulding32c35cf2009-01-26 09:13:25 -0500613
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700614 OPT_GROUP(N_("Tag listing options")),
Jake Goulding32c35cf2009-01-26 09:13:25 -0500615 {
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700616 OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"),
617 N_("print only tags that contain the commit"),
Jake Goulding32c35cf2009-01-26 09:13:25 -0500618 PARSE_OPT_LASTARG_DEFAULT,
619 parse_opt_with_commit, (intptr_t)"HEAD",
620 },
Tom Grennanae7706b2012-02-08 15:03:43 -0800621 {
Junio C Hamanob0bc1362014-03-07 12:07:35 -0800622 OPTION_CALLBACK, 0, "with", &with_commit, N_("commit"),
623 N_("print only tags that contain the commit"),
624 PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
625 parse_opt_with_commit, (intptr_t)"HEAD",
626 },
627 {
Nguyễn Thái Ngọc Duyc88bba12012-08-20 19:32:47 +0700628 OPTION_CALLBACK, 0, "points-at", NULL, N_("object"),
629 N_("print only tags of the object"), 0, parse_opt_points_at
Tom Grennanae7706b2012-02-08 15:03:43 -0800630 },
Carlos Rica39686582007-11-09 14:42:56 +0100631 OPT_END()
632 };
633
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100634 git_config(git_tag_config, NULL);
Carlos Rica39686582007-11-09 14:42:56 +0100635
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200636 memset(&opt, 0, sizeof(opt));
637
Stephen Boyd37782922009-05-23 11:53:12 -0700638 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
Carlos Rica39686582007-11-09 14:42:56 +0100639
Linus Torvaldsbe15f502007-12-10 20:08:06 -0800640 if (keyid) {
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200641 opt.sign = 1;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700642 set_signing_key(keyid);
Linus Torvaldsbe15f502007-12-10 20:08:06 -0800643 }
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200644 if (opt.sign)
Junio C Hamanoc1a41b92007-11-25 15:21:42 -0800645 annotate = 1;
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700646 if (argc == 0 && !cmdmode)
647 cmdmode = 'l';
Junio C Hamanoc1a41b92007-11-25 15:21:42 -0800648
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700649 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
Samuel Tardieu6fa83422008-11-05 00:20:31 +0100650 usage_with_options(git_tag_usage, options);
651
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +0700652 finalize_colopts(&colopts, -1);
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700653 if (cmdmode == 'l' && lines != -1) {
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +0700654 if (explicitly_enable_column(colopts))
655 die(_("--column and -n are incompatible"));
656 colopts = 0;
657 }
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700658 if (cmdmode == 'l') {
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +0700659 int ret;
660 if (column_active(colopts)) {
661 struct column_options copts;
662 memset(&copts, 0, sizeof(copts));
663 copts.padding = 2;
664 run_column_filter(colopts, &copts);
665 }
Jacob Kellerb1507942014-07-16 14:48:02 -0700666 if (lines != -1 && tag_sort)
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700667 die(_("--sort and -n are incompatible"));
Jacob Kellerb1507942014-07-16 14:48:02 -0700668 ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
Nguyễn Thái Ngọc Duyd96e3c12012-04-13 17:54:41 +0700669 if (column_active(colopts))
670 stop_column_filter();
671 return ret;
672 }
Samuel Tardieu6fa83422008-11-05 00:20:31 +0100673 if (lines != -1)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000674 die(_("-n option is only allowed with -l."));
Jake Goulding32c35cf2009-01-26 09:13:25 -0500675 if (with_commit)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000676 die(_("--contains option is only allowed with -l."));
Tom Grennanae7706b2012-02-08 15:03:43 -0800677 if (points_at.nr)
678 die(_("--points-at option is only allowed with -l."));
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700679 if (cmdmode == 'd')
Carlos Rica39686582007-11-09 14:42:56 +0100680 return for_each_tag_name(argv, delete_tag);
Junio C Hamanoe6b722d2013-07-30 12:31:27 -0700681 if (cmdmode == 'v')
Carlos Rica39686582007-11-09 14:42:56 +0100682 return for_each_tag_name(argv, verify_tag);
683
Junio C Hamanobd46c9a2007-11-22 23:16:51 -0800684 if (msg.given || msgfile) {
685 if (msg.given && msgfile)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000686 die(_("only one -F or -m option is allowed."));
Carlos Rica39686582007-11-09 14:42:56 +0100687 annotate = 1;
Junio C Hamanobd46c9a2007-11-22 23:16:51 -0800688 if (msg.given)
689 strbuf_addbuf(&buf, &(msg.buf));
Carlos Rica39686582007-11-09 14:42:56 +0100690 else {
691 if (!strcmp(msgfile, "-")) {
Pierre Habouzit387e7e12007-09-27 15:25:55 +0200692 if (strbuf_read(&buf, 0, 1024) < 0)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000693 die_errno(_("cannot read '%s'"), msgfile);
Pierre Habouzit387e7e12007-09-27 15:25:55 +0200694 } else {
Carlos Rica39686582007-11-09 14:42:56 +0100695 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000696 die_errno(_("could not open or read '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200697 msgfile);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200698 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200699 }
Carlos Rica62e09ce2007-07-20 01:42:28 +0200700 }
701
Carlos Rica39686582007-11-09 14:42:56 +0100702 tag = argv[0];
Carlos Rica62e09ce2007-07-20 01:42:28 +0200703
Carlos Rica39686582007-11-09 14:42:56 +0100704 object_ref = argc == 2 ? argv[1] : "HEAD";
705 if (argc > 2)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000706 die(_("too many params"));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200707
708 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000709 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200710
Michael Schubert4f0accd2011-05-10 01:36:36 +0200711 if (strbuf_check_tag_ref(&ref, tag))
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000712 die(_("'%s' is not a valid tag name."), tag);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200713
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700714 if (read_ref(ref.buf, prev))
Carlos Rica62e09ce2007-07-20 01:42:28 +0200715 hashclr(prev);
716 else if (!force)
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000717 die(_("tag '%s' already exists"), tag);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200718
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200719 opt.message_given = msg.given || msgfile;
720
721 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
722 opt.cleanup_mode = CLEANUP_ALL;
723 else if (!strcmp(cleanup_arg, "verbatim"))
724 opt.cleanup_mode = CLEANUP_NONE;
725 else if (!strcmp(cleanup_arg, "whitespace"))
726 opt.cleanup_mode = CLEANUP_SPACE;
727 else
728 die(_("Invalid cleanup mode %s"), cleanup_arg);
729
Carlos Rica62e09ce2007-07-20 01:42:28 +0200730 if (annotate)
Kirill A. Shutemovd3e05982011-12-07 05:01:45 +0200731 create_tag(object, tag, &buf, &opt, prev, object);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200732
Ronnie Sahlberge5074bf2014-04-16 15:30:41 -0700733 transaction = ref_transaction_begin(&err);
734 if (!transaction ||
735 ref_transaction_update(transaction, ref.buf, object, prev,
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700736 0, 1, NULL, &err) ||
737 ref_transaction_commit(transaction, &err))
Ronnie Sahlberge5074bf2014-04-16 15:30:41 -0700738 die("%s", err.buf);
739 ref_transaction_free(transaction);
Phil Hord3ae851e2013-03-12 19:13:41 -0400740 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
Ævar Arnfjörð Bjarmasond08ebf92011-02-22 23:42:09 +0000741 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
Carlos Rica62e09ce2007-07-20 01:42:28 +0200742
Ronnie Sahlberge5074bf2014-04-16 15:30:41 -0700743 strbuf_release(&err);
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200744 strbuf_release(&buf);
Michael Schubert4f0accd2011-05-10 01:36:36 +0200745 strbuf_release(&ref);
Carlos Rica62e09ce2007-07-20 01:42:28 +0200746 return 0;
747}