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