]>
Commit | Line | Data |
---|---|---|
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 | |
21 | static 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 |
35 | static int tag_sort; |
36 | ||
62e09ce9 | 37 | struct 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 | 45 | static struct sha1_array points_at; |
d96e3c15 | 46 | static unsigned int colopts; |
ae7706b9 | 47 | |
588d0e83 JK |
48 | static 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 | ||
ae7706b9 TG |
59 | static 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 | ||
ffc4b801 JK |
77 | static 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 | ||
cbc60b67 JJL |
85 | enum contains_result { |
86 | CONTAINS_UNKNOWN = -1, | |
87 | CONTAINS_NO = 0, | |
95acfc24 | 88 | CONTAINS_YES = 1 |
cbc60b67 JJL |
89 | }; |
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 | */ | |
95 | static enum contains_result contains_test(struct commit *candidate, | |
c6d72c49 | 96 | const struct commit_list *want) |
ffc4b801 | 97 | { |
ffc4b801 JK |
98 | /* 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? */ | |
cbc60b67 JJL |
105 | if (in_commit_list(want, candidate)) { |
106 | candidate->object.flags |= TMP_MARK; | |
ffc4b801 | 107 | return 1; |
cbc60b67 | 108 | } |
ffc4b801 JK |
109 | |
110 | if (parse_commit(candidate) < 0) | |
111 | return 0; | |
112 | ||
cbc60b67 JJL |
113 | return -1; |
114 | } | |
115 | ||
116 | /* | |
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 | */ | |
123 | struct stack { | |
124 | int nr, alloc; | |
125 | struct stack_entry { | |
126 | struct commit *commit; | |
127 | struct commit_list *parents; | |
128 | } *stack; | |
129 | }; | |
130 | ||
131 | static void push_to_stack(struct commit *candidate, struct stack *stack) | |
132 | { | |
133 | 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; | |
ffc4b801 JK |
137 | } |
138 | ||
cbc60b67 JJL |
139 | static enum contains_result contains(struct commit *candidate, |
140 | const struct commit_list *want) | |
ffc4b801 | 141 | { |
cbc60b67 JJL |
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); | |
ffc4b801 JK |
177 | } |
178 | ||
ca516999 JK |
179 | static 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); | |
fb630e04 JK |
188 | if (!buf) |
189 | die_errno("unable to read object %s", sha1_to_hex(sha1)); | |
31fd8d72 JH |
190 | 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)); | |
ca516999 JK |
195 | |
196 | /* skip header */ | |
197 | sp = strstr(buf, "\n\n"); | |
31fd8d72 JH |
198 | 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); | |
ca516999 JK |
204 | 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 | } | |
31fd8d72 | 214 | free_return: |
ca516999 JK |
215 | free(buf); |
216 | } | |
217 | ||
62e09ce9 CR |
218 | static 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 | ||
588d0e83 | 223 | if (match_pattern(filter->patterns, refname)) { |
32c35cfb JG |
224 | if (filter->with_commit) { |
225 | struct commit *commit; | |
226 | ||
227 | commit = lookup_commit_reference_gently(sha1, 1); | |
228 | if (!commit) | |
229 | return 0; | |
ffc4b801 | 230 | if (!contains(commit, filter->with_commit)) |
32c35cfb JG |
231 | return 0; |
232 | } | |
233 | ||
ae7706b9 TG |
234 | if (points_at.nr && !match_points_at(refname, sha1)) |
235 | return 0; | |
236 | ||
62e09ce9 | 237 | if (!filter->lines) { |
9ef176b5 NTND |
238 | if (filter->sort) |
239 | string_list_append(&filter->tags, refname); | |
240 | else | |
241 | printf("%s\n", refname); | |
62e09ce9 CR |
242 | return 0; |
243 | } | |
244 | printf("%-15s ", refname); | |
ca516999 | 245 | show_tag_lines(sha1, filter->lines); |
62e09ce9 | 246 | putchar('\n'); |
62e09ce9 CR |
247 | } |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
9ef176b5 NTND |
252 | static 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 | ||
588d0e83 | 259 | static int list_tags(const char **patterns, int lines, |
9ef176b5 | 260 | struct commit_list *with_commit, int sort) |
62e09ce9 CR |
261 | { |
262 | struct tag_filter filter; | |
2b2a5be3 MH |
263 | struct each_ref_fn_sha1_adapter wrapped_show_reference = |
264 | {show_reference, (void *)&filter}; | |
62e09ce9 | 265 | |
588d0e83 | 266 | filter.patterns = patterns; |
62e09ce9 | 267 | filter.lines = lines; |
9ef176b5 | 268 | filter.sort = sort; |
32c35cfb | 269 | filter.with_commit = with_commit; |
9ef176b5 NTND |
270 | memset(&filter.tags, 0, sizeof(filter.tags)); |
271 | filter.tags.strdup_strings = 1; | |
62e09ce9 | 272 | |
2b2a5be3 | 273 | for_each_tag_ref(each_ref_fn_adapter, &wrapped_show_reference); |
9ef176b5 NTND |
274 | if (sort) { |
275 | int i; | |
276 | if ((sort & SORT_MASK) == VERCMP_SORT) | |
277 | qsort(filter.tags.items, filter.tags.nr, | |
278 | sizeof(struct string_list_item), sort_by_version); | |
279 | if (sort & REVERSE_SORT) | |
280 | for (i = filter.tags.nr - 1; i >= 0; i--) | |
281 | printf("%s\n", filter.tags.items[i].string); | |
282 | else | |
283 | for (i = 0; i < filter.tags.nr; i++) | |
284 | printf("%s\n", filter.tags.items[i].string); | |
285 | string_list_clear(&filter.tags, 0); | |
286 | } | |
62e09ce9 CR |
287 | return 0; |
288 | } | |
289 | ||
e317cfaf | 290 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
62e09ce9 CR |
291 | const unsigned char *sha1); |
292 | ||
e317cfaf | 293 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn) |
62e09ce9 CR |
294 | { |
295 | const char **p; | |
296 | char ref[PATH_MAX]; | |
297 | int had_error = 0; | |
298 | unsigned char sha1[20]; | |
299 | ||
300 | for (p = argv; *p; p++) { | |
301 | if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p) | |
302 | >= sizeof(ref)) { | |
d08ebf99 | 303 | error(_("tag name too long: %.*s..."), 50, *p); |
62e09ce9 CR |
304 | had_error = 1; |
305 | continue; | |
306 | } | |
c6893323 | 307 | if (read_ref(ref, sha1)) { |
d08ebf99 | 308 | error(_("tag '%s' not found."), *p); |
62e09ce9 CR |
309 | had_error = 1; |
310 | continue; | |
311 | } | |
312 | if (fn(*p, ref, sha1)) | |
313 | had_error = 1; | |
314 | } | |
315 | return had_error; | |
316 | } | |
317 | ||
318 | static int delete_tag(const char *name, const char *ref, | |
319 | const unsigned char *sha1) | |
320 | { | |
eca35a25 | 321 | if (delete_ref(ref, sha1, 0)) |
62e09ce9 | 322 | return 1; |
d08ebf99 | 323 | printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
62e09ce9 CR |
324 | return 0; |
325 | } | |
326 | ||
327 | static int verify_tag(const char *name, const char *ref, | |
328 | const unsigned char *sha1) | |
329 | { | |
a6ccbbdb | 330 | const char *argv_verify_tag[] = {"verify-tag", |
62e09ce9 CR |
331 | "-v", "SHA1_HEX", NULL}; |
332 | argv_verify_tag[2] = sha1_to_hex(sha1); | |
333 | ||
a6ccbbdb | 334 | if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD)) |
d08ebf99 | 335 | return error(_("could not verify the tag '%s'"), name); |
62e09ce9 CR |
336 | return 0; |
337 | } | |
338 | ||
fd17f5b5 | 339 | static int do_sign(struct strbuf *buffer) |
62e09ce9 | 340 | { |
2f47eae2 | 341 | return sign_buffer(buffer, buffer, get_signing_key()); |
62e09ce9 CR |
342 | } |
343 | ||
344 | static const char tag_template[] = | |
d78f340e | 345 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f | 346 | "Lines starting with '%c' will be ignored.\n"); |
d3e05983 KS |
347 | |
348 | static const char tag_template_nocleanup[] = | |
d78f340e | 349 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f JH |
350 | "Lines starting with '%c' will be kept; you may remove them" |
351 | " yourself if you want to.\n"); | |
62e09ce9 | 352 | |
b150794d JK |
353 | /* |
354 | * Parse a sort string, and return 0 if parsed successfully. Will return | |
355 | * non-zero when the sort string does not parse into a known type. If var is | |
356 | * given, the error message becomes a warning and includes information about | |
357 | * the configuration value. | |
358 | */ | |
359 | static int parse_sort_string(const char *var, const char *arg, int *sort) | |
360 | { | |
361 | int type = 0, flags = 0; | |
362 | ||
363 | if (skip_prefix(arg, "-", &arg)) | |
364 | flags |= REVERSE_SORT; | |
365 | ||
366 | if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg)) | |
367 | type = VERCMP_SORT; | |
368 | else | |
369 | type = STRCMP_SORT; | |
370 | ||
371 | if (strcmp(arg, "refname")) { | |
372 | if (!var) | |
373 | return error(_("unsupported sort specification '%s'"), arg); | |
374 | else { | |
375 | warning(_("unsupported sort specification '%s' in variable '%s'"), | |
376 | var, arg); | |
377 | return -1; | |
378 | } | |
379 | } | |
380 | ||
381 | *sort = (type | flags); | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
ef90d6d4 | 386 | static int git_tag_config(const char *var, const char *value, void *cb) |
62e09ce9 | 387 | { |
b150794d JK |
388 | int status; |
389 | ||
390 | if (!strcmp(var, "tag.sort")) { | |
391 | if (!value) | |
392 | return config_error_nonbool(var); | |
393 | parse_sort_string(var, value, &tag_sort); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | status = git_gpg_config(var, value, cb); | |
2f47eae2 JH |
398 | if (status) |
399 | return status; | |
59556548 | 400 | if (starts_with(var, "column.")) |
d96e3c15 | 401 | return git_column_config(var, value, "tag", &colopts); |
ef90d6d4 | 402 | return git_default_config(var, value, cb); |
62e09ce9 CR |
403 | } |
404 | ||
bab8118a MH |
405 | static void write_tag_body(int fd, const unsigned char *sha1) |
406 | { | |
407 | unsigned long size; | |
408 | enum object_type type; | |
e10dfb62 | 409 | char *buf, *sp; |
bab8118a MH |
410 | |
411 | buf = read_sha1_file(sha1, &type, &size); | |
412 | if (!buf) | |
413 | return; | |
414 | /* skip header */ | |
415 | sp = strstr(buf, "\n\n"); | |
416 | ||
417 | if (!sp || !size || type != OBJ_TAG) { | |
418 | free(buf); | |
419 | return; | |
420 | } | |
421 | sp += 2; /* skip the 2 LFs */ | |
e10dfb62 | 422 | write_or_die(fd, sp, parse_signature(sp, buf + size - sp)); |
bab8118a MH |
423 | |
424 | free(buf); | |
425 | } | |
426 | ||
3927bbe9 JK |
427 | static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result) |
428 | { | |
429 | if (sign && do_sign(buf) < 0) | |
d08ebf99 | 430 | return error(_("unable to sign the tag")); |
3927bbe9 | 431 | if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0) |
d08ebf99 | 432 | return error(_("unable to write tag file")); |
3927bbe9 JK |
433 | return 0; |
434 | } | |
435 | ||
d3e05983 KS |
436 | struct create_tag_options { |
437 | unsigned int message_given:1; | |
438 | unsigned int sign; | |
439 | enum { | |
440 | CLEANUP_NONE, | |
441 | CLEANUP_SPACE, | |
442 | CLEANUP_ALL | |
443 | } cleanup_mode; | |
444 | }; | |
445 | ||
62e09ce9 | 446 | static void create_tag(const unsigned char *object, const char *tag, |
d3e05983 | 447 | struct strbuf *buf, struct create_tag_options *opt, |
bd46c9a9 | 448 | unsigned char *prev, unsigned char *result) |
62e09ce9 CR |
449 | { |
450 | enum object_type type; | |
fd17f5b5 PH |
451 | char header_buf[1024]; |
452 | int header_len; | |
3927bbe9 | 453 | char *path = NULL; |
62e09ce9 CR |
454 | |
455 | type = sha1_object_info(object, NULL); | |
e317cfaf | 456 | if (type <= OBJ_NONE) |
d08ebf99 | 457 | die(_("bad object type.")); |
62e09ce9 CR |
458 | |
459 | header_len = snprintf(header_buf, sizeof(header_buf), | |
460 | "object %s\n" | |
461 | "type %s\n" | |
462 | "tag %s\n" | |
463 | "tagger %s\n\n", | |
464 | sha1_to_hex(object), | |
465 | typename(type), | |
466 | tag, | |
f9bc573f | 467 | git_committer_info(IDENT_STRICT)); |
62e09ce9 | 468 | |
e317cfaf | 469 | if (header_len > sizeof(header_buf) - 1) |
d08ebf99 | 470 | die(_("tag header too big.")); |
62e09ce9 | 471 | |
d3e05983 | 472 | if (!opt->message_given) { |
62e09ce9 CR |
473 | int fd; |
474 | ||
475 | /* write the template message before editing: */ | |
a4f34cbb | 476 | path = git_pathdup("TAG_EDITMSG"); |
62e09ce9 CR |
477 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
478 | if (fd < 0) | |
d08ebf99 | 479 | die_errno(_("could not create file '%s'"), path); |
bab8118a | 480 | |
eff80a9f | 481 | if (!is_null_sha1(prev)) { |
bab8118a | 482 | write_tag_body(fd, prev); |
eff80a9f JH |
483 | } else { |
484 | struct strbuf buf = STRBUF_INIT; | |
485 | strbuf_addch(&buf, '\n'); | |
486 | if (opt->cleanup_mode == CLEANUP_ALL) | |
d78f340e | 487 | strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); |
eff80a9f | 488 | else |
d78f340e | 489 | strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); |
eff80a9f JH |
490 | write_or_die(fd, buf.buf, buf.len); |
491 | strbuf_release(&buf); | |
492 | } | |
62e09ce9 CR |
493 | close(fd); |
494 | ||
7198203a SB |
495 | if (launch_editor(path, buf, NULL)) { |
496 | fprintf(stderr, | |
d08ebf99 | 497 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
498 | exit(1); |
499 | } | |
62e09ce9 | 500 | } |
62e09ce9 | 501 | |
d3e05983 KS |
502 | if (opt->cleanup_mode != CLEANUP_NONE) |
503 | stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); | |
62e09ce9 | 504 | |
d3e05983 | 505 | if (!opt->message_given && !buf->len) |
d08ebf99 | 506 | die(_("no tag message?")); |
62e09ce9 | 507 | |
fd17f5b5 | 508 | strbuf_insert(buf, 0, header_buf, header_len); |
62e09ce9 | 509 | |
d3e05983 | 510 | if (build_tag_object(buf, opt->sign, result) < 0) { |
3927bbe9 | 511 | if (path) |
d08ebf99 | 512 | fprintf(stderr, _("The tag message has been left in %s\n"), |
3927bbe9 JK |
513 | path); |
514 | exit(128); | |
515 | } | |
516 | if (path) { | |
691f1a28 | 517 | unlink_or_warn(path); |
3927bbe9 JK |
518 | free(path); |
519 | } | |
62e09ce9 CR |
520 | } |
521 | ||
bd46c9a9 JH |
522 | struct msg_arg { |
523 | int given; | |
524 | struct strbuf buf; | |
525 | }; | |
526 | ||
527 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) | |
528 | { | |
529 | struct msg_arg *msg = opt->value; | |
530 | ||
531 | if (!arg) | |
532 | return -1; | |
533 | if (msg->buf.len) | |
534 | strbuf_addstr(&(msg->buf), "\n\n"); | |
535 | strbuf_addstr(&(msg->buf), arg); | |
536 | msg->given = 1; | |
537 | return 0; | |
538 | } | |
539 | ||
4f0accd6 MS |
540 | static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) |
541 | { | |
542 | if (name[0] == '-') | |
8d9c5010 | 543 | return -1; |
4f0accd6 MS |
544 | |
545 | strbuf_reset(sb); | |
546 | strbuf_addf(sb, "refs/tags/%s", name); | |
547 | ||
8d9c5010 | 548 | return check_refname_format(sb->buf, 0); |
4f0accd6 MS |
549 | } |
550 | ||
0975a502 | 551 | static int parse_opt_points_at(const struct option *opt __attribute__((unused)), |
ae7706b9 TG |
552 | const char *arg, int unset) |
553 | { | |
554 | unsigned char sha1[20]; | |
555 | ||
556 | if (unset) { | |
557 | sha1_array_clear(&points_at); | |
558 | return 0; | |
559 | } | |
560 | if (!arg) | |
561 | return error(_("switch 'points-at' requires an object")); | |
562 | if (get_sha1(arg, sha1)) | |
563 | return error(_("malformed object name '%s'"), arg); | |
564 | sha1_array_append(&points_at, sha1); | |
565 | return 0; | |
566 | } | |
567 | ||
9ef176b5 NTND |
568 | static int parse_opt_sort(const struct option *opt, const char *arg, int unset) |
569 | { | |
570 | int *sort = opt->value; | |
9ef176b5 | 571 | |
b150794d | 572 | return parse_sort_string(NULL, arg, sort); |
9ef176b5 NTND |
573 | } |
574 | ||
62e09ce9 CR |
575 | int cmd_tag(int argc, const char **argv, const char *prefix) |
576 | { | |
f285a2d7 | 577 | struct strbuf buf = STRBUF_INIT; |
4f0accd6 | 578 | struct strbuf ref = STRBUF_INIT; |
62e09ce9 | 579 | unsigned char object[20], prev[20]; |
62e09ce9 | 580 | const char *object_ref, *tag; |
d3e05983 KS |
581 | struct create_tag_options opt; |
582 | char *cleanup_arg = NULL; | |
e6b722db | 583 | int annotate = 0, force = 0, lines = -1; |
b150794d | 584 | int cmdmode = 0; |
dbd0f5c7 | 585 | const char *msgfile = NULL, *keyid = NULL; |
bd46c9a9 | 586 | struct msg_arg msg = { 0, STRBUF_INIT }; |
32c35cfb | 587 | struct commit_list *with_commit = NULL; |
e5074bfe RS |
588 | struct ref_transaction *transaction; |
589 | struct strbuf err = STRBUF_INIT; | |
39686585 | 590 | struct option options[] = { |
e6b722db | 591 | OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'), |
c88bba18 NTND |
592 | { OPTION_INTEGER, 'n', NULL, &lines, N_("n"), |
593 | N_("print <n> lines of each tag message"), | |
39686585 | 594 | PARSE_OPT_OPTARG, NULL, 1 }, |
e6b722db JH |
595 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), |
596 | OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), | |
39686585 | 597 | |
c88bba18 | 598 | OPT_GROUP(N_("Tag creation options")), |
d5d09d47 | 599 | OPT_BOOL('a', "annotate", &annotate, |
c88bba18 NTND |
600 | N_("annotated tag, needs a message")), |
601 | OPT_CALLBACK('m', "message", &msg, N_("message"), | |
602 | N_("tag message"), parse_msg_arg), | |
603 | OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), | |
d5d09d47 | 604 | OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), |
c88bba18 NTND |
605 | OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"), |
606 | N_("how to strip spaces and #comments from message")), | |
e703d711 | 607 | OPT_STRING('u', "local-user", &keyid, N_("key-id"), |
c88bba18 NTND |
608 | N_("use another key to sign the tag")), |
609 | OPT__FORCE(&force, N_("replace the tag if exists")), | |
dd059c6c JK |
610 | |
611 | OPT_GROUP(N_("Tag listing options")), | |
c88bba18 | 612 | OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), |
9ef176b5 | 613 | { |
b150794d | 614 | OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"), |
9ef176b5 NTND |
615 | PARSE_OPT_NONEG, parse_opt_sort |
616 | }, | |
32c35cfb | 617 | { |
c88bba18 NTND |
618 | OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"), |
619 | N_("print only tags that contain the commit"), | |
32c35cfb JG |
620 | PARSE_OPT_LASTARG_DEFAULT, |
621 | parse_opt_with_commit, (intptr_t)"HEAD", | |
622 | }, | |
b0bc1365 JH |
623 | { |
624 | OPTION_CALLBACK, 0, "with", &with_commit, N_("commit"), | |
625 | N_("print only tags that contain the commit"), | |
626 | PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT, | |
627 | parse_opt_with_commit, (intptr_t)"HEAD", | |
628 | }, | |
ae7706b9 | 629 | { |
c88bba18 NTND |
630 | OPTION_CALLBACK, 0, "points-at", NULL, N_("object"), |
631 | N_("print only tags of the object"), 0, parse_opt_points_at | |
ae7706b9 | 632 | }, |
39686585 CR |
633 | OPT_END() |
634 | }; | |
635 | ||
ef90d6d4 | 636 | git_config(git_tag_config, NULL); |
62e09ce9 | 637 | |
d3e05983 KS |
638 | memset(&opt, 0, sizeof(opt)); |
639 | ||
37782920 | 640 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
62e09ce9 | 641 | |
be15f505 | 642 | if (keyid) { |
d3e05983 | 643 | opt.sign = 1; |
2f47eae2 | 644 | set_signing_key(keyid); |
be15f505 | 645 | } |
d3e05983 | 646 | if (opt.sign) |
c1a41b9d | 647 | annotate = 1; |
e6b722db JH |
648 | if (argc == 0 && !cmdmode) |
649 | cmdmode = 'l'; | |
c1a41b9d | 650 | |
e6b722db | 651 | if ((annotate || msg.given || msgfile || force) && (cmdmode != 0)) |
6fa8342b ST |
652 | usage_with_options(git_tag_usage, options); |
653 | ||
d96e3c15 | 654 | finalize_colopts(&colopts, -1); |
e6b722db | 655 | if (cmdmode == 'l' && lines != -1) { |
d96e3c15 NTND |
656 | if (explicitly_enable_column(colopts)) |
657 | die(_("--column and -n are incompatible")); | |
658 | colopts = 0; | |
659 | } | |
e6b722db | 660 | if (cmdmode == 'l') { |
d96e3c15 NTND |
661 | int ret; |
662 | if (column_active(colopts)) { | |
663 | struct column_options copts; | |
664 | memset(&copts, 0, sizeof(copts)); | |
665 | copts.padding = 2; | |
666 | run_column_filter(colopts, &copts); | |
667 | } | |
b150794d | 668 | if (lines != -1 && tag_sort) |
9ef176b5 | 669 | die(_("--sort and -n are incompatible")); |
b150794d | 670 | ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort); |
d96e3c15 NTND |
671 | if (column_active(colopts)) |
672 | stop_column_filter(); | |
673 | return ret; | |
674 | } | |
6fa8342b | 675 | if (lines != -1) |
d08ebf99 | 676 | die(_("-n option is only allowed with -l.")); |
32c35cfb | 677 | if (with_commit) |
d08ebf99 | 678 | die(_("--contains option is only allowed with -l.")); |
ae7706b9 TG |
679 | if (points_at.nr) |
680 | die(_("--points-at option is only allowed with -l.")); | |
e6b722db | 681 | if (cmdmode == 'd') |
39686585 | 682 | return for_each_tag_name(argv, delete_tag); |
e6b722db | 683 | if (cmdmode == 'v') |
39686585 CR |
684 | return for_each_tag_name(argv, verify_tag); |
685 | ||
bd46c9a9 JH |
686 | if (msg.given || msgfile) { |
687 | if (msg.given && msgfile) | |
d08ebf99 | 688 | die(_("only one -F or -m option is allowed.")); |
39686585 | 689 | annotate = 1; |
bd46c9a9 JH |
690 | if (msg.given) |
691 | strbuf_addbuf(&buf, &(msg.buf)); | |
39686585 CR |
692 | else { |
693 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 694 | if (strbuf_read(&buf, 0, 1024) < 0) |
d08ebf99 | 695 | die_errno(_("cannot read '%s'"), msgfile); |
387e7e19 | 696 | } else { |
39686585 | 697 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
d08ebf99 | 698 | die_errno(_("could not open or read '%s'"), |
d824cbba | 699 | msgfile); |
62e09ce9 | 700 | } |
62e09ce9 | 701 | } |
62e09ce9 CR |
702 | } |
703 | ||
39686585 | 704 | tag = argv[0]; |
62e09ce9 | 705 | |
39686585 CR |
706 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
707 | if (argc > 2) | |
d08ebf99 | 708 | die(_("too many params")); |
62e09ce9 CR |
709 | |
710 | if (get_sha1(object_ref, object)) | |
d08ebf99 | 711 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
62e09ce9 | 712 | |
4f0accd6 | 713 | if (strbuf_check_tag_ref(&ref, tag)) |
d08ebf99 | 714 | die(_("'%s' is not a valid tag name."), tag); |
62e09ce9 | 715 | |
c6893323 | 716 | if (read_ref(ref.buf, prev)) |
62e09ce9 CR |
717 | hashclr(prev); |
718 | else if (!force) | |
d08ebf99 | 719 | die(_("tag '%s' already exists"), tag); |
62e09ce9 | 720 | |
d3e05983 KS |
721 | opt.message_given = msg.given || msgfile; |
722 | ||
723 | if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) | |
724 | opt.cleanup_mode = CLEANUP_ALL; | |
725 | else if (!strcmp(cleanup_arg, "verbatim")) | |
726 | opt.cleanup_mode = CLEANUP_NONE; | |
727 | else if (!strcmp(cleanup_arg, "whitespace")) | |
728 | opt.cleanup_mode = CLEANUP_SPACE; | |
729 | else | |
730 | die(_("Invalid cleanup mode %s"), cleanup_arg); | |
731 | ||
62e09ce9 | 732 | if (annotate) |
d3e05983 | 733 | create_tag(object, tag, &buf, &opt, prev, object); |
62e09ce9 | 734 | |
e5074bfe RS |
735 | transaction = ref_transaction_begin(&err); |
736 | if (!transaction || | |
737 | ref_transaction_update(transaction, ref.buf, object, prev, | |
1d147bdf | 738 | 0, NULL, &err) || |
db7516ab | 739 | ref_transaction_commit(transaction, &err)) |
e5074bfe RS |
740 | die("%s", err.buf); |
741 | ref_transaction_free(transaction); | |
3ae851e6 | 742 | if (force && !is_null_sha1(prev) && hashcmp(prev, object)) |
d08ebf99 | 743 | printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV)); |
62e09ce9 | 744 | |
e5074bfe | 745 | strbuf_release(&err); |
fd17f5b5 | 746 | strbuf_release(&buf); |
4f0accd6 | 747 | strbuf_release(&ref); |
62e09ce9 CR |
748 | return 0; |
749 | } |