]>
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" | |
b2141fc1 | 10 | #include "config.h" |
62e09ce9 CR |
11 | #include "builtin.h" |
12 | #include "refs.h" | |
cbd53a21 | 13 | #include "object-store.h" |
62e09ce9 CR |
14 | #include "tag.h" |
15 | #include "run-command.h" | |
39686585 | 16 | #include "parse-options.h" |
ffc4b801 JK |
17 | #include "diff.h" |
18 | #include "revision.h" | |
2f47eae2 | 19 | #include "gpg-interface.h" |
fe299ec5 | 20 | #include "oid-array.h" |
d96e3c15 | 21 | #include "column.h" |
ac4cc866 | 22 | #include "ref-filter.h" |
39686585 CR |
23 | |
24 | static const char * const git_tag_usage[] = { | |
01dc801a | 25 | N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n" |
5d70198e | 26 | " <tagname> [<head>]"), |
c88bba18 | 27 | N_("git tag -d <tagname>..."), |
01dc801a | 28 | N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]\n" |
5d70198e | 29 | " [--format=<format>] [--merged <commit>] [--no-merged <commit>] [<pattern>...]"), |
07d347cf | 30 | N_("git tag -v [--format=<format>] <tagname>..."), |
39686585 CR |
31 | NULL |
32 | }; | |
62e09ce9 | 33 | |
d96e3c15 | 34 | static unsigned int colopts; |
61c2fe0c | 35 | static int force_sign_annotate; |
1c6b565f | 36 | static int config_sign_tag = -1; /* unspecified */ |
ae7706b9 | 37 | |
4a68e36d JK |
38 | static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, |
39 | struct ref_format *format) | |
ca516999 | 40 | { |
ac4cc866 | 41 | struct ref_array array; |
844c3f0b ZH |
42 | struct strbuf output = STRBUF_INIT; |
43 | struct strbuf err = STRBUF_INIT; | |
df094741 | 44 | char *to_free = NULL; |
ca516999 | 45 | int i; |
62e09ce9 | 46 | |
ac4cc866 | 47 | memset(&array, 0, sizeof(array)); |
32c35cfb | 48 | |
ac4cc866 KN |
49 | if (filter->lines == -1) |
50 | filter->lines = 0; | |
ae7706b9 | 51 | |
4a68e36d | 52 | if (!format->format) { |
df094741 KN |
53 | if (filter->lines) { |
54 | to_free = xstrfmt("%s %%(contents:lines=%d)", | |
17938f17 | 55 | "%(align:15)%(refname:lstrip=2)%(end)", |
df094741 | 56 | filter->lines); |
4a68e36d | 57 | format->format = to_free; |
df094741 | 58 | } else |
4a68e36d | 59 | format->format = "%(refname:lstrip=2)"; |
62e09ce9 CR |
60 | } |
61 | ||
2eda0102 JK |
62 | if (verify_ref_format(format)) |
63 | die(_("unable to parse format string")); | |
008ed7df | 64 | filter->with_commit_tag_algo = 1; |
b7cc53e9 KN |
65 | filter_refs(&array, filter, FILTER_REFS_TAGS); |
66 | ref_array_sort(sorting, &array); | |
62e09ce9 | 67 | |
22f69a85 | 68 | for (i = 0; i < array.nr; i++) { |
844c3f0b ZH |
69 | strbuf_reset(&output); |
70 | strbuf_reset(&err); | |
22f69a85 ZH |
71 | if (format_ref_array_item(array.items[i], format, &output, &err)) |
72 | die("%s", err.buf); | |
73 | fwrite(output.buf, 1, output.len, stdout); | |
74 | putchar('\n'); | |
22f69a85 | 75 | } |
844c3f0b ZH |
76 | |
77 | strbuf_release(&err); | |
78 | strbuf_release(&output); | |
b7cc53e9 KN |
79 | ref_array_clear(&array); |
80 | free(to_free); | |
9ef176b5 | 81 | |
62e09ce9 CR |
82 | return 0; |
83 | } | |
84 | ||
e317cfaf | 85 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
81989077 | 86 | const struct object_id *oid, void *cb_data); |
62e09ce9 | 87 | |
07d347cf | 88 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn, |
81989077 | 89 | void *cb_data) |
62e09ce9 CR |
90 | { |
91 | const char **p; | |
7f897b6f | 92 | struct strbuf ref = STRBUF_INIT; |
62e09ce9 | 93 | int had_error = 0; |
7422ab50 | 94 | struct object_id oid; |
62e09ce9 CR |
95 | |
96 | for (p = argv; *p; p++) { | |
7f897b6f JK |
97 | strbuf_reset(&ref); |
98 | strbuf_addf(&ref, "refs/tags/%s", *p); | |
34c290a6 | 99 | if (read_ref(ref.buf, &oid)) { |
d08ebf99 | 100 | error(_("tag '%s' not found."), *p); |
62e09ce9 CR |
101 | had_error = 1; |
102 | continue; | |
103 | } | |
7422ab50 | 104 | if (fn(*p, ref.buf, &oid, cb_data)) |
62e09ce9 CR |
105 | had_error = 1; |
106 | } | |
7f897b6f | 107 | strbuf_release(&ref); |
62e09ce9 CR |
108 | return had_error; |
109 | } | |
110 | ||
81989077 PH |
111 | static int collect_tags(const char *name, const char *ref, |
112 | const struct object_id *oid, void *cb_data) | |
62e09ce9 | 113 | { |
81989077 PH |
114 | struct string_list *ref_list = cb_data; |
115 | ||
116 | string_list_append(ref_list, ref); | |
117 | ref_list->items[ref_list->nr - 1].util = oiddup(oid); | |
62e09ce9 CR |
118 | return 0; |
119 | } | |
120 | ||
81989077 PH |
121 | static int delete_tags(const char **argv) |
122 | { | |
123 | int result; | |
124 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; | |
125 | struct string_list_item *item; | |
126 | ||
127 | result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete); | |
128 | if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF)) | |
129 | result = 1; | |
130 | ||
131 | for_each_string_list_item(item, &refs_to_delete) { | |
132 | const char *name = item->string; | |
133 | struct object_id *oid = item->util; | |
134 | if (!ref_exists(name)) | |
135 | printf(_("Deleted tag '%s' (was %s)\n"), | |
136 | item->string + 10, | |
137 | find_unique_abbrev(oid, DEFAULT_ABBREV)); | |
138 | ||
139 | free(oid); | |
140 | } | |
141 | string_list_clear(&refs_to_delete, 0); | |
142 | return result; | |
143 | } | |
144 | ||
62e09ce9 | 145 | static int verify_tag(const char *name, const char *ref, |
81989077 | 146 | const struct object_id *oid, void *cb_data) |
62e09ce9 | 147 | { |
07d347cf | 148 | int flags; |
e85fcb35 | 149 | struct ref_format *format = cb_data; |
07d347cf LP |
150 | flags = GPG_VERIFY_VERBOSE; |
151 | ||
4a68e36d | 152 | if (format->format) |
07d347cf LP |
153 | flags = GPG_VERIFY_OMIT_STATUS; |
154 | ||
84571760 | 155 | if (gpg_verify_tag(oid, name, flags)) |
07d347cf LP |
156 | return -1; |
157 | ||
4a68e36d | 158 | if (format->format) |
53df97a2 | 159 | pretty_print_ref(name, oid, format); |
07d347cf LP |
160 | |
161 | return 0; | |
62e09ce9 CR |
162 | } |
163 | ||
fd17f5b5 | 164 | static int do_sign(struct strbuf *buffer) |
62e09ce9 | 165 | { |
2f47eae2 | 166 | return sign_buffer(buffer, buffer, get_signing_key()); |
62e09ce9 CR |
167 | } |
168 | ||
169 | static const char tag_template[] = | |
d78f340e | 170 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f | 171 | "Lines starting with '%c' will be ignored.\n"); |
d3e05983 KS |
172 | |
173 | static const char tag_template_nocleanup[] = | |
d78f340e | 174 | N_("\nWrite a message for tag:\n %s\n" |
eff80a9f JH |
175 | "Lines starting with '%c' will be kept; you may remove them" |
176 | " yourself if you want to.\n"); | |
62e09ce9 | 177 | |
ef90d6d4 | 178 | static int git_tag_config(const char *var, const char *value, void *cb) |
62e09ce9 | 179 | { |
b150794d | 180 | int status; |
b7cc53e9 | 181 | struct ref_sorting **sorting_tail = (struct ref_sorting **)cb; |
b150794d | 182 | |
1c6b565f TM |
183 | if (!strcmp(var, "tag.gpgsign")) { |
184 | config_sign_tag = git_config_bool(var, value); | |
185 | return 0; | |
186 | } | |
187 | ||
b150794d JK |
188 | if (!strcmp(var, "tag.sort")) { |
189 | if (!value) | |
190 | return config_error_nonbool(var); | |
18a25650 | 191 | parse_ref_sorting(sorting_tail, value); |
b150794d JK |
192 | return 0; |
193 | } | |
194 | ||
195 | status = git_gpg_config(var, value, cb); | |
2f47eae2 JH |
196 | if (status) |
197 | return status; | |
61c2fe0c LA |
198 | if (!strcmp(var, "tag.forcesignannotated")) { |
199 | force_sign_annotate = git_config_bool(var, value); | |
200 | return 0; | |
201 | } | |
202 | ||
59556548 | 203 | if (starts_with(var, "column.")) |
d96e3c15 | 204 | return git_column_config(var, value, "tag", &colopts); |
b521fd12 | 205 | return git_color_default_config(var, value, cb); |
62e09ce9 CR |
206 | } |
207 | ||
7422ab50 | 208 | static void write_tag_body(int fd, const struct object_id *oid) |
bab8118a MH |
209 | { |
210 | unsigned long size; | |
211 | enum object_type type; | |
482c1191 | 212 | char *buf, *sp, *orig; |
213 | struct strbuf payload = STRBUF_INIT; | |
214 | struct strbuf signature = STRBUF_INIT; | |
bab8118a | 215 | |
482c1191 | 216 | orig = buf = read_object_file(oid, &type, &size); |
bab8118a MH |
217 | if (!buf) |
218 | return; | |
482c1191 | 219 | if (parse_signature(buf, size, &payload, &signature)) { |
220 | buf = payload.buf; | |
221 | size = payload.len; | |
222 | } | |
bab8118a MH |
223 | /* skip header */ |
224 | sp = strstr(buf, "\n\n"); | |
225 | ||
226 | if (!sp || !size || type != OBJ_TAG) { | |
227 | free(buf); | |
228 | return; | |
229 | } | |
230 | sp += 2; /* skip the 2 LFs */ | |
482c1191 | 231 | write_or_die(fd, sp, buf + size - sp); |
bab8118a | 232 | |
482c1191 | 233 | free(orig); |
234 | strbuf_release(&payload); | |
235 | strbuf_release(&signature); | |
bab8118a MH |
236 | } |
237 | ||
7422ab50 | 238 | static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result) |
3927bbe9 JK |
239 | { |
240 | if (sign && do_sign(buf) < 0) | |
d08ebf99 | 241 | return error(_("unable to sign the tag")); |
a09c985e | 242 | if (write_object_file(buf->buf, buf->len, tag_type, result) < 0) |
d08ebf99 | 243 | return error(_("unable to write tag file")); |
3927bbe9 JK |
244 | return 0; |
245 | } | |
246 | ||
d3e05983 KS |
247 | struct create_tag_options { |
248 | unsigned int message_given:1; | |
9eed6e40 | 249 | unsigned int use_editor:1; |
d3e05983 KS |
250 | unsigned int sign; |
251 | enum { | |
252 | CLEANUP_NONE, | |
253 | CLEANUP_SPACE, | |
254 | CLEANUP_ALL | |
255 | } cleanup_mode; | |
256 | }; | |
257 | ||
eea9c1e7 | 258 | static const char message_advice_nested_tag[] = |
a54b2ab3 | 259 | N_("You have created a nested tag. The object referred to by your new tag is\n" |
eea9c1e7 DL |
260 | "already a tag. If you meant to tag the object that it points to, use:\n" |
261 | "\n" | |
262 | "\tgit tag -f %s %s^{}"); | |
263 | ||
264 | static void create_tag(const struct object_id *object, const char *object_ref, | |
265 | const char *tag, | |
d3e05983 | 266 | struct strbuf *buf, struct create_tag_options *opt, |
7422ab50 | 267 | struct object_id *prev, struct object_id *result) |
62e09ce9 CR |
268 | { |
269 | enum object_type type; | |
b0ceab98 | 270 | struct strbuf header = STRBUF_INIT; |
3927bbe9 | 271 | char *path = NULL; |
62e09ce9 | 272 | |
0df8e965 | 273 | type = oid_object_info(the_repository, object, NULL); |
e317cfaf | 274 | if (type <= OBJ_NONE) |
01dc801a | 275 | die(_("bad object type.")); |
62e09ce9 | 276 | |
f665d63a HW |
277 | if (type == OBJ_TAG) |
278 | advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag), | |
279 | tag, object_ref); | |
62e09ce9 | 280 | |
b0ceab98 JK |
281 | strbuf_addf(&header, |
282 | "object %s\n" | |
283 | "type %s\n" | |
284 | "tag %s\n" | |
285 | "tagger %s\n\n", | |
7422ab50 | 286 | oid_to_hex(object), |
debca9d2 | 287 | type_name(type), |
b0ceab98 JK |
288 | tag, |
289 | git_committer_info(IDENT_STRICT)); | |
62e09ce9 | 290 | |
9eed6e40 | 291 | if (!opt->message_given || opt->use_editor) { |
62e09ce9 CR |
292 | int fd; |
293 | ||
294 | /* write the template message before editing: */ | |
a4f34cbb | 295 | path = git_pathdup("TAG_EDITMSG"); |
66e905b7 | 296 | fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
bab8118a | 297 | |
9eed6e40 NMC |
298 | if (opt->message_given) { |
299 | write_or_die(fd, buf->buf, buf->len); | |
300 | strbuf_reset(buf); | |
301 | } else if (!is_null_oid(prev)) { | |
bab8118a | 302 | write_tag_body(fd, prev); |
eff80a9f JH |
303 | } else { |
304 | struct strbuf buf = STRBUF_INIT; | |
305 | strbuf_addch(&buf, '\n'); | |
306 | if (opt->cleanup_mode == CLEANUP_ALL) | |
d78f340e | 307 | strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); |
eff80a9f | 308 | else |
d78f340e | 309 | strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); |
eff80a9f JH |
310 | write_or_die(fd, buf.buf, buf.len); |
311 | strbuf_release(&buf); | |
312 | } | |
62e09ce9 CR |
313 | close(fd); |
314 | ||
7198203a SB |
315 | if (launch_editor(path, buf, NULL)) { |
316 | fprintf(stderr, | |
d08ebf99 | 317 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
318 | exit(1); |
319 | } | |
62e09ce9 | 320 | } |
62e09ce9 | 321 | |
d3e05983 | 322 | if (opt->cleanup_mode != CLEANUP_NONE) |
63af4a84 | 323 | strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); |
62e09ce9 | 324 | |
d3e05983 | 325 | if (!opt->message_given && !buf->len) |
d08ebf99 | 326 | die(_("no tag message?")); |
62e09ce9 | 327 | |
b0ceab98 JK |
328 | strbuf_insert(buf, 0, header.buf, header.len); |
329 | strbuf_release(&header); | |
62e09ce9 | 330 | |
d3e05983 | 331 | if (build_tag_object(buf, opt->sign, result) < 0) { |
3927bbe9 | 332 | if (path) |
d08ebf99 | 333 | fprintf(stderr, _("The tag message has been left in %s\n"), |
3927bbe9 JK |
334 | path); |
335 | exit(128); | |
336 | } | |
337 | if (path) { | |
691f1a28 | 338 | unlink_or_warn(path); |
3927bbe9 JK |
339 | free(path); |
340 | } | |
62e09ce9 CR |
341 | } |
342 | ||
7422ab50 | 343 | static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb) |
df8512ed CW |
344 | { |
345 | enum object_type type; | |
346 | struct commit *c; | |
347 | char *buf; | |
348 | unsigned long size; | |
349 | int subject_len = 0; | |
350 | const char *subject_start; | |
351 | ||
352 | char *rla = getenv("GIT_REFLOG_ACTION"); | |
353 | if (rla) { | |
354 | strbuf_addstr(sb, rla); | |
355 | } else { | |
c3027be9 | 356 | strbuf_addstr(sb, "tag: tagging "); |
30e677e0 | 357 | strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV); |
df8512ed CW |
358 | } |
359 | ||
360 | strbuf_addstr(sb, " ("); | |
0df8e965 | 361 | type = oid_object_info(the_repository, oid, NULL); |
df8512ed CW |
362 | switch (type) { |
363 | default: | |
c3027be9 | 364 | strbuf_addstr(sb, "object of unknown type"); |
df8512ed CW |
365 | break; |
366 | case OBJ_COMMIT: | |
afe8a907 | 367 | if ((buf = read_object_file(oid, &type, &size))) { |
df8512ed CW |
368 | subject_len = find_commit_subject(buf, &subject_start); |
369 | strbuf_insert(sb, sb->len, subject_start, subject_len); | |
370 | } else { | |
c3027be9 | 371 | strbuf_addstr(sb, "commit object"); |
df8512ed CW |
372 | } |
373 | free(buf); | |
374 | ||
afe8a907 | 375 | if ((c = lookup_commit_reference(the_repository, oid))) |
df8512ed CW |
376 | strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT))); |
377 | break; | |
378 | case OBJ_TREE: | |
c3027be9 | 379 | strbuf_addstr(sb, "tree object"); |
df8512ed CW |
380 | break; |
381 | case OBJ_BLOB: | |
c3027be9 | 382 | strbuf_addstr(sb, "blob object"); |
df8512ed CW |
383 | break; |
384 | case OBJ_TAG: | |
c3027be9 | 385 | strbuf_addstr(sb, "other tag object"); |
df8512ed CW |
386 | break; |
387 | } | |
388 | strbuf_addch(sb, ')'); | |
389 | } | |
390 | ||
bd46c9a9 JH |
391 | struct msg_arg { |
392 | int given; | |
393 | struct strbuf buf; | |
394 | }; | |
395 | ||
396 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) | |
397 | { | |
398 | struct msg_arg *msg = opt->value; | |
399 | ||
517fe807 JK |
400 | BUG_ON_OPT_NEG(unset); |
401 | ||
bd46c9a9 JH |
402 | if (!arg) |
403 | return -1; | |
404 | if (msg->buf.len) | |
405 | strbuf_addstr(&(msg->buf), "\n\n"); | |
406 | strbuf_addstr(&(msg->buf), arg); | |
407 | msg->given = 1; | |
408 | return 0; | |
409 | } | |
410 | ||
4f0accd6 MS |
411 | static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) |
412 | { | |
413 | if (name[0] == '-') | |
8d9c5010 | 414 | return -1; |
4f0accd6 MS |
415 | |
416 | strbuf_reset(sb); | |
417 | strbuf_addf(sb, "refs/tags/%s", name); | |
418 | ||
8d9c5010 | 419 | return check_refname_format(sb->buf, 0); |
4f0accd6 MS |
420 | } |
421 | ||
62e09ce9 CR |
422 | int cmd_tag(int argc, const char **argv, const char *prefix) |
423 | { | |
f285a2d7 | 424 | struct strbuf buf = STRBUF_INIT; |
4f0accd6 | 425 | struct strbuf ref = STRBUF_INIT; |
df8512ed | 426 | struct strbuf reflog_msg = STRBUF_INIT; |
7422ab50 | 427 | struct object_id object, prev; |
62e09ce9 | 428 | const char *object_ref, *tag; |
d3e05983 KS |
429 | struct create_tag_options opt; |
430 | char *cleanup_arg = NULL; | |
144c76fa | 431 | int create_reflog = 0; |
ac4cc866 | 432 | int annotate = 0, force = 0; |
61c2fe0c | 433 | int cmdmode = 0, create_tag_object = 0; |
dbd0f5c7 | 434 | const char *msgfile = NULL, *keyid = NULL; |
37766b61 | 435 | struct msg_arg msg = { .buf = STRBUF_INIT }; |
e5074bfe RS |
436 | struct ref_transaction *transaction; |
437 | struct strbuf err = STRBUF_INIT; | |
ac4cc866 | 438 | struct ref_filter filter; |
b7cc53e9 | 439 | static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; |
4a68e36d | 440 | struct ref_format format = REF_FORMAT_INIT; |
3bb16a8b | 441 | int icase = 0; |
9eed6e40 | 442 | int edit_flag = 0; |
39686585 | 443 | struct option options[] = { |
e6b722db | 444 | OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'), |
ac4cc866 | 445 | { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"), |
c88bba18 | 446 | N_("print <n> lines of each tag message"), |
39686585 | 447 | PARSE_OPT_OPTARG, NULL, 1 }, |
e6b722db JH |
448 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), |
449 | OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), | |
39686585 | 450 | |
c88bba18 | 451 | OPT_GROUP(N_("Tag creation options")), |
d5d09d47 | 452 | OPT_BOOL('a', "annotate", &annotate, |
c88bba18 | 453 | N_("annotated tag, needs a message")), |
203c8533 DL |
454 | OPT_CALLBACK_F('m', "message", &msg, N_("message"), |
455 | N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg), | |
c88bba18 | 456 | OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), |
9eed6e40 | 457 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")), |
d5d09d47 | 458 | OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), |
ca04dc96 | 459 | OPT_CLEANUP(&cleanup_arg), |
e703d711 | 460 | OPT_STRING('u', "local-user", &keyid, N_("key-id"), |
c88bba18 | 461 | N_("use another key to sign the tag")), |
1224781d | 462 | OPT__FORCE(&force, N_("replace the tag if exists"), 0), |
98c32bd8 | 463 | OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")), |
dd059c6c JK |
464 | |
465 | OPT_GROUP(N_("Tag listing options")), | |
c88bba18 | 466 | OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), |
ac4cc866 | 467 | OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 468 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")), |
ac4cc866 | 469 | OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 470 | OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")), |
5242860f KN |
471 | OPT_MERGED(&filter, N_("print only tags that are merged")), |
472 | OPT_NO_MERGED(&filter, N_("print only tags that are not merged")), | |
95be717c | 473 | OPT_REF_SORT(sorting_tail), |
32c35cfb | 474 | { |
ac4cc866 | 475 | OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"), |
1e0c3b68 ÆAB |
476 | N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT, |
477 | parse_opt_object_name, (intptr_t) "HEAD" | |
ae7706b9 | 478 | }, |
4a68e36d JK |
479 | OPT_STRING( 0 , "format", &format.format, N_("format"), |
480 | N_("format to use for the output")), | |
0c88bf50 | 481 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
3bb16a8b | 482 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
39686585 CR |
483 | OPT_END() |
484 | }; | |
37766b61 | 485 | int ret = 0; |
39686585 | 486 | |
56b43607 KN |
487 | setup_ref_filter_porcelain_msg(); |
488 | ||
b7cc53e9 | 489 | git_config(git_tag_config, sorting_tail); |
62e09ce9 | 490 | |
d3e05983 | 491 | memset(&opt, 0, sizeof(opt)); |
ac4cc866 KN |
492 | memset(&filter, 0, sizeof(filter)); |
493 | filter.lines = -1; | |
1c6b565f | 494 | opt.sign = -1; |
d3e05983 | 495 | |
37782920 | 496 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
62e09ce9 | 497 | |
6a338149 ÆAB |
498 | if (!cmdmode) { |
499 | if (argc == 0) | |
500 | cmdmode = 'l'; | |
ac3f5a34 | 501 | else if (filter.with_commit || filter.no_commit || |
21bf9339 AL |
502 | filter.reachable_from || filter.unreachable_from || |
503 | filter.points_at.nr || filter.lines != -1) | |
6a338149 ÆAB |
504 | cmdmode = 'l'; |
505 | } | |
c1a41b9d | 506 | |
de121ffe | 507 | if (cmdmode == 'l') |
ff1e7248 | 508 | setup_auto_pager("tag", 1); |
de121ffe | 509 | |
1c6b565f TM |
510 | if (opt.sign == -1) |
511 | opt.sign = cmdmode ? 0 : config_sign_tag > 0; | |
512 | ||
513 | if (keyid) { | |
514 | opt.sign = 1; | |
515 | set_signing_key(keyid); | |
516 | } | |
517 | create_tag_object = (opt.sign || annotate || msg.given || msgfile); | |
518 | ||
61c2fe0c | 519 | if ((create_tag_object || force) && (cmdmode != 0)) |
6fa8342b ST |
520 | usage_with_options(git_tag_usage, options); |
521 | ||
d96e3c15 | 522 | finalize_colopts(&colopts, -1); |
ac4cc866 | 523 | if (cmdmode == 'l' && filter.lines != -1) { |
d96e3c15 NTND |
524 | if (explicitly_enable_column(colopts)) |
525 | die(_("--column and -n are incompatible")); | |
526 | colopts = 0; | |
527 | } | |
b7cc53e9 KN |
528 | if (!sorting) |
529 | sorting = ref_default_sorting(); | |
7c269a7b | 530 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
3bb16a8b | 531 | filter.ignore_case = icase; |
e6b722db | 532 | if (cmdmode == 'l') { |
d96e3c15 NTND |
533 | if (column_active(colopts)) { |
534 | struct column_options copts; | |
535 | memset(&copts, 0, sizeof(copts)); | |
536 | copts.padding = 2; | |
537 | run_column_filter(colopts, &copts); | |
538 | } | |
ac4cc866 | 539 | filter.name_patterns = argv; |
4a68e36d | 540 | ret = list_tags(&filter, sorting, &format); |
d96e3c15 NTND |
541 | if (column_active(colopts)) |
542 | stop_column_filter(); | |
37766b61 | 543 | goto cleanup; |
d96e3c15 | 544 | } |
ac4cc866 | 545 | if (filter.lines != -1) |
6a338149 | 546 | die(_("-n option is only allowed in list mode")); |
ac4cc866 | 547 | if (filter.with_commit) |
6a338149 | 548 | die(_("--contains option is only allowed in list mode")); |
ac3f5a34 ÆAB |
549 | if (filter.no_commit) |
550 | die(_("--no-contains option is only allowed in list mode")); | |
ac4cc866 | 551 | if (filter.points_at.nr) |
6a338149 | 552 | die(_("--points-at option is only allowed in list mode")); |
21bf9339 | 553 | if (filter.reachable_from || filter.unreachable_from) |
6a338149 | 554 | die(_("--merged and --no-merged options are only allowed in list mode")); |
37766b61 ÆAB |
555 | if (cmdmode == 'd') { |
556 | ret = delete_tags(argv); | |
557 | goto cleanup; | |
558 | } | |
07d347cf | 559 | if (cmdmode == 'v') { |
4a68e36d | 560 | if (format.format && verify_ref_format(&format)) |
2eda0102 | 561 | usage_with_options(git_tag_usage, options); |
37766b61 ÆAB |
562 | ret = for_each_tag_name(argv, verify_tag, &format); |
563 | goto cleanup; | |
07d347cf | 564 | } |
39686585 | 565 | |
bd46c9a9 JH |
566 | if (msg.given || msgfile) { |
567 | if (msg.given && msgfile) | |
d08ebf99 | 568 | die(_("only one -F or -m option is allowed.")); |
bd46c9a9 JH |
569 | if (msg.given) |
570 | strbuf_addbuf(&buf, &(msg.buf)); | |
39686585 CR |
571 | else { |
572 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 573 | if (strbuf_read(&buf, 0, 1024) < 0) |
d08ebf99 | 574 | die_errno(_("cannot read '%s'"), msgfile); |
387e7e19 | 575 | } else { |
39686585 | 576 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
d08ebf99 | 577 | die_errno(_("could not open or read '%s'"), |
d824cbba | 578 | msgfile); |
62e09ce9 | 579 | } |
62e09ce9 | 580 | } |
62e09ce9 CR |
581 | } |
582 | ||
39686585 | 583 | tag = argv[0]; |
62e09ce9 | 584 | |
39686585 CR |
585 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
586 | if (argc > 2) | |
b8657347 | 587 | die(_("too many arguments")); |
62e09ce9 | 588 | |
7422ab50 | 589 | if (get_oid(object_ref, &object)) |
d08ebf99 | 590 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
62e09ce9 | 591 | |
4f0accd6 | 592 | if (strbuf_check_tag_ref(&ref, tag)) |
d08ebf99 | 593 | die(_("'%s' is not a valid tag name."), tag); |
62e09ce9 | 594 | |
34c290a6 | 595 | if (read_ref(ref.buf, &prev)) |
7422ab50 | 596 | oidclr(&prev); |
62e09ce9 | 597 | else if (!force) |
d08ebf99 | 598 | die(_("tag '%s' already exists"), tag); |
62e09ce9 | 599 | |
d3e05983 | 600 | opt.message_given = msg.given || msgfile; |
9eed6e40 | 601 | opt.use_editor = edit_flag; |
d3e05983 KS |
602 | |
603 | if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) | |
604 | opt.cleanup_mode = CLEANUP_ALL; | |
605 | else if (!strcmp(cleanup_arg, "verbatim")) | |
606 | opt.cleanup_mode = CLEANUP_NONE; | |
607 | else if (!strcmp(cleanup_arg, "whitespace")) | |
608 | opt.cleanup_mode = CLEANUP_SPACE; | |
609 | else | |
610 | die(_("Invalid cleanup mode %s"), cleanup_arg); | |
611 | ||
7422ab50 | 612 | create_reflog_msg(&object, &reflog_msg); |
df8512ed | 613 | |
61c2fe0c LA |
614 | if (create_tag_object) { |
615 | if (force_sign_annotate && !annotate) | |
616 | opt.sign = 1; | |
eea9c1e7 | 617 | create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object); |
61c2fe0c | 618 | } |
62e09ce9 | 619 | |
e5074bfe RS |
620 | transaction = ref_transaction_begin(&err); |
621 | if (!transaction || | |
89f3bbdd | 622 | ref_transaction_update(transaction, ref.buf, &object, &prev, |
144c76fa | 623 | create_reflog ? REF_FORCE_CREATE_REFLOG : 0, |
df8512ed | 624 | reflog_msg.buf, &err) || |
db7516ab | 625 | ref_transaction_commit(transaction, &err)) |
e5074bfe RS |
626 | die("%s", err.buf); |
627 | ref_transaction_free(transaction); | |
9001dc2a | 628 | if (force && !is_null_oid(&prev) && !oideq(&prev, &object)) |
aab9583f | 629 | printf(_("Updated tag '%s' (was %s)\n"), tag, |
630 | find_unique_abbrev(&prev, DEFAULT_ABBREV)); | |
62e09ce9 | 631 | |
37766b61 | 632 | cleanup: |
e5fb0286 | 633 | ref_sorting_release(sorting); |
37766b61 ÆAB |
634 | strbuf_release(&buf); |
635 | strbuf_release(&ref); | |
636 | strbuf_release(&reflog_msg); | |
637 | strbuf_release(&msg.buf); | |
638 | strbuf_release(&err); | |
639 | return ret; | |
62e09ce9 | 640 | } |