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