]>
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 | ||
bc5c5ec0 | 9 | #include "builtin.h" |
6c6ddf92 | 10 | #include "advice.h" |
b2141fc1 | 11 | #include "config.h" |
4e120823 | 12 | #include "editor.h" |
7ee24e18 | 13 | #include "environment.h" |
f394e093 | 14 | #include "gettext.h" |
41771fa4 | 15 | #include "hex.h" |
62e09ce9 | 16 | #include "refs.h" |
dabab1d6 | 17 | #include "object-name.h" |
a034e910 | 18 | #include "object-store-ll.h" |
c339932b | 19 | #include "path.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 | |
ef90d6d4 | 191 | static int git_tag_config(const char *var, const char *value, void *cb) |
62e09ce9 | 192 | { |
1c6b565f TM |
193 | if (!strcmp(var, "tag.gpgsign")) { |
194 | config_sign_tag = git_config_bool(var, value); | |
195 | return 0; | |
196 | } | |
197 | ||
b150794d JK |
198 | if (!strcmp(var, "tag.sort")) { |
199 | if (!value) | |
200 | return config_error_nonbool(var); | |
98e7ab6d | 201 | string_list_append(cb, value); |
b150794d JK |
202 | return 0; |
203 | } | |
204 | ||
61c2fe0c LA |
205 | if (!strcmp(var, "tag.forcesignannotated")) { |
206 | force_sign_annotate = git_config_bool(var, value); | |
207 | return 0; | |
208 | } | |
209 | ||
59556548 | 210 | if (starts_with(var, "column.")) |
d96e3c15 | 211 | return git_column_config(var, value, "tag", &colopts); |
b521fd12 | 212 | return git_color_default_config(var, value, cb); |
62e09ce9 CR |
213 | } |
214 | ||
7422ab50 | 215 | static void write_tag_body(int fd, const struct object_id *oid) |
bab8118a MH |
216 | { |
217 | unsigned long size; | |
218 | enum object_type type; | |
482c1191 | 219 | char *buf, *sp, *orig; |
220 | struct strbuf payload = STRBUF_INIT; | |
221 | struct strbuf signature = STRBUF_INIT; | |
bab8118a | 222 | |
bc726bd0 | 223 | orig = buf = repo_read_object_file(the_repository, oid, &type, &size); |
bab8118a MH |
224 | if (!buf) |
225 | return; | |
482c1191 | 226 | if (parse_signature(buf, size, &payload, &signature)) { |
227 | buf = payload.buf; | |
228 | size = payload.len; | |
229 | } | |
bab8118a MH |
230 | /* skip header */ |
231 | sp = strstr(buf, "\n\n"); | |
232 | ||
233 | if (!sp || !size || type != OBJ_TAG) { | |
234 | free(buf); | |
235 | return; | |
236 | } | |
237 | sp += 2; /* skip the 2 LFs */ | |
482c1191 | 238 | write_or_die(fd, sp, buf + size - sp); |
bab8118a | 239 | |
482c1191 | 240 | free(orig); |
241 | strbuf_release(&payload); | |
242 | strbuf_release(&signature); | |
bab8118a MH |
243 | } |
244 | ||
7422ab50 | 245 | static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result) |
3927bbe9 JK |
246 | { |
247 | if (sign && do_sign(buf) < 0) | |
d08ebf99 | 248 | return error(_("unable to sign the tag")); |
c80d226a | 249 | if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0) |
d08ebf99 | 250 | return error(_("unable to write tag file")); |
3927bbe9 JK |
251 | return 0; |
252 | } | |
253 | ||
d3e05983 KS |
254 | struct create_tag_options { |
255 | unsigned int message_given:1; | |
9eed6e40 | 256 | unsigned int use_editor:1; |
d3e05983 KS |
257 | unsigned int sign; |
258 | enum { | |
259 | CLEANUP_NONE, | |
260 | CLEANUP_SPACE, | |
261 | CLEANUP_ALL | |
262 | } cleanup_mode; | |
263 | }; | |
264 | ||
eea9c1e7 | 265 | static const char message_advice_nested_tag[] = |
a54b2ab3 | 266 | N_("You have created a nested tag. The object referred to by your new tag is\n" |
eea9c1e7 DL |
267 | "already a tag. If you meant to tag the object that it points to, use:\n" |
268 | "\n" | |
269 | "\tgit tag -f %s %s^{}"); | |
270 | ||
271 | static void create_tag(const struct object_id *object, const char *object_ref, | |
272 | const char *tag, | |
d3e05983 | 273 | struct strbuf *buf, struct create_tag_options *opt, |
08c12ec1 | 274 | struct object_id *prev, struct object_id *result, char *path) |
62e09ce9 CR |
275 | { |
276 | enum object_type type; | |
b0ceab98 | 277 | struct strbuf header = STRBUF_INIT; |
62e09ce9 | 278 | |
0df8e965 | 279 | type = oid_object_info(the_repository, object, NULL); |
e317cfaf | 280 | if (type <= OBJ_NONE) |
01dc801a | 281 | die(_("bad object type.")); |
62e09ce9 | 282 | |
f665d63a HW |
283 | if (type == OBJ_TAG) |
284 | advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag), | |
285 | tag, object_ref); | |
62e09ce9 | 286 | |
b0ceab98 JK |
287 | strbuf_addf(&header, |
288 | "object %s\n" | |
289 | "type %s\n" | |
290 | "tag %s\n" | |
291 | "tagger %s\n\n", | |
7422ab50 | 292 | oid_to_hex(object), |
debca9d2 | 293 | type_name(type), |
b0ceab98 JK |
294 | tag, |
295 | git_committer_info(IDENT_STRICT)); | |
62e09ce9 | 296 | |
9eed6e40 | 297 | if (!opt->message_given || opt->use_editor) { |
62e09ce9 CR |
298 | int fd; |
299 | ||
300 | /* write the template message before editing: */ | |
66e905b7 | 301 | fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
bab8118a | 302 | |
9eed6e40 NMC |
303 | if (opt->message_given) { |
304 | write_or_die(fd, buf->buf, buf->len); | |
305 | strbuf_reset(buf); | |
306 | } else if (!is_null_oid(prev)) { | |
bab8118a | 307 | write_tag_body(fd, prev); |
eff80a9f JH |
308 | } else { |
309 | struct strbuf buf = STRBUF_INIT; | |
310 | strbuf_addch(&buf, '\n'); | |
311 | if (opt->cleanup_mode == CLEANUP_ALL) | |
d78f340e | 312 | strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); |
eff80a9f | 313 | else |
d78f340e | 314 | strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); |
eff80a9f JH |
315 | write_or_die(fd, buf.buf, buf.len); |
316 | strbuf_release(&buf); | |
317 | } | |
62e09ce9 CR |
318 | close(fd); |
319 | ||
7198203a SB |
320 | if (launch_editor(path, buf, NULL)) { |
321 | fprintf(stderr, | |
d08ebf99 | 322 | _("Please supply the message using either -m or -F option.\n")); |
7198203a SB |
323 | exit(1); |
324 | } | |
62e09ce9 | 325 | } |
62e09ce9 | 326 | |
d3e05983 | 327 | if (opt->cleanup_mode != CLEANUP_NONE) |
63af4a84 | 328 | strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); |
62e09ce9 | 329 | |
d3e05983 | 330 | if (!opt->message_given && !buf->len) |
d08ebf99 | 331 | die(_("no tag message?")); |
62e09ce9 | 332 | |
b0ceab98 JK |
333 | strbuf_insert(buf, 0, header.buf, header.len); |
334 | strbuf_release(&header); | |
62e09ce9 | 335 | |
d3e05983 | 336 | if (build_tag_object(buf, opt->sign, result) < 0) { |
3927bbe9 | 337 | if (path) |
d08ebf99 | 338 | fprintf(stderr, _("The tag message has been left in %s\n"), |
3927bbe9 JK |
339 | path); |
340 | exit(128); | |
341 | } | |
62e09ce9 CR |
342 | } |
343 | ||
7422ab50 | 344 | static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb) |
df8512ed CW |
345 | { |
346 | enum object_type type; | |
347 | struct commit *c; | |
348 | char *buf; | |
349 | unsigned long size; | |
350 | int subject_len = 0; | |
351 | const char *subject_start; | |
352 | ||
353 | char *rla = getenv("GIT_REFLOG_ACTION"); | |
354 | if (rla) { | |
355 | strbuf_addstr(sb, rla); | |
356 | } else { | |
c3027be9 | 357 | strbuf_addstr(sb, "tag: tagging "); |
30e677e0 | 358 | strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV); |
df8512ed CW |
359 | } |
360 | ||
361 | strbuf_addstr(sb, " ("); | |
0df8e965 | 362 | type = oid_object_info(the_repository, oid, NULL); |
df8512ed CW |
363 | switch (type) { |
364 | default: | |
c3027be9 | 365 | strbuf_addstr(sb, "object of unknown type"); |
df8512ed CW |
366 | break; |
367 | case OBJ_COMMIT: | |
bc726bd0 | 368 | if ((buf = repo_read_object_file(the_repository, oid, &type, &size))) { |
df8512ed CW |
369 | subject_len = find_commit_subject(buf, &subject_start); |
370 | strbuf_insert(sb, sb->len, subject_start, subject_len); | |
371 | } else { | |
c3027be9 | 372 | strbuf_addstr(sb, "commit object"); |
df8512ed CW |
373 | } |
374 | free(buf); | |
375 | ||
afe8a907 | 376 | if ((c = lookup_commit_reference(the_repository, oid))) |
df8512ed CW |
377 | strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT))); |
378 | break; | |
379 | case OBJ_TREE: | |
c3027be9 | 380 | strbuf_addstr(sb, "tree object"); |
df8512ed CW |
381 | break; |
382 | case OBJ_BLOB: | |
c3027be9 | 383 | strbuf_addstr(sb, "blob object"); |
df8512ed CW |
384 | break; |
385 | case OBJ_TAG: | |
c3027be9 | 386 | strbuf_addstr(sb, "other tag object"); |
df8512ed CW |
387 | break; |
388 | } | |
389 | strbuf_addch(sb, ')'); | |
390 | } | |
391 | ||
bd46c9a9 JH |
392 | struct msg_arg { |
393 | int given; | |
394 | struct strbuf buf; | |
395 | }; | |
396 | ||
397 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) | |
398 | { | |
399 | struct msg_arg *msg = opt->value; | |
400 | ||
517fe807 JK |
401 | BUG_ON_OPT_NEG(unset); |
402 | ||
bd46c9a9 JH |
403 | if (!arg) |
404 | return -1; | |
405 | if (msg->buf.len) | |
406 | strbuf_addstr(&(msg->buf), "\n\n"); | |
407 | strbuf_addstr(&(msg->buf), arg); | |
408 | msg->given = 1; | |
409 | return 0; | |
410 | } | |
411 | ||
4f0accd6 MS |
412 | static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) |
413 | { | |
414 | if (name[0] == '-') | |
8d9c5010 | 415 | return -1; |
4f0accd6 MS |
416 | |
417 | strbuf_reset(sb); | |
418 | strbuf_addf(sb, "refs/tags/%s", name); | |
419 | ||
8d9c5010 | 420 | return check_refname_format(sb->buf, 0); |
4f0accd6 MS |
421 | } |
422 | ||
62e09ce9 CR |
423 | int cmd_tag(int argc, const char **argv, const char *prefix) |
424 | { | |
f285a2d7 | 425 | struct strbuf buf = STRBUF_INIT; |
4f0accd6 | 426 | struct strbuf ref = STRBUF_INIT; |
df8512ed | 427 | struct strbuf reflog_msg = STRBUF_INIT; |
7422ab50 | 428 | struct object_id object, prev; |
62e09ce9 | 429 | const char *object_ref, *tag; |
d3e05983 KS |
430 | struct create_tag_options opt; |
431 | char *cleanup_arg = NULL; | |
144c76fa | 432 | int create_reflog = 0; |
ac4cc866 | 433 | int annotate = 0, force = 0; |
61c2fe0c | 434 | int cmdmode = 0, create_tag_object = 0; |
7ce4088a JK |
435 | char *msgfile = NULL; |
436 | const char *keyid = NULL; | |
37766b61 | 437 | struct msg_arg msg = { .buf = STRBUF_INIT }; |
e5074bfe RS |
438 | struct ref_transaction *transaction; |
439 | struct strbuf err = STRBUF_INIT; | |
ac4cc866 | 440 | struct ref_filter filter; |
98e7ab6d JH |
441 | struct ref_sorting *sorting; |
442 | struct string_list sorting_options = STRING_LIST_INIT_DUP; | |
4a68e36d | 443 | struct ref_format format = REF_FORMAT_INIT; |
3bb16a8b | 444 | int icase = 0; |
9eed6e40 | 445 | int edit_flag = 0; |
39686585 | 446 | struct option options[] = { |
e6b722db | 447 | OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'), |
ac4cc866 | 448 | { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"), |
c88bba18 | 449 | N_("print <n> lines of each tag message"), |
39686585 | 450 | PARSE_OPT_OPTARG, NULL, 1 }, |
e6b722db JH |
451 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), |
452 | OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), | |
39686585 | 453 | |
c88bba18 | 454 | OPT_GROUP(N_("Tag creation options")), |
d5d09d47 | 455 | OPT_BOOL('a', "annotate", &annotate, |
c88bba18 | 456 | N_("annotated tag, needs a message")), |
203c8533 DL |
457 | OPT_CALLBACK_F('m', "message", &msg, N_("message"), |
458 | N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg), | |
c88bba18 | 459 | OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), |
9eed6e40 | 460 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")), |
d5d09d47 | 461 | OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), |
ca04dc96 | 462 | OPT_CLEANUP(&cleanup_arg), |
e703d711 | 463 | OPT_STRING('u', "local-user", &keyid, N_("key-id"), |
c88bba18 | 464 | N_("use another key to sign the tag")), |
1224781d | 465 | OPT__FORCE(&force, N_("replace the tag if exists"), 0), |
98c32bd8 | 466 | OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")), |
dd059c6c JK |
467 | |
468 | OPT_GROUP(N_("Tag listing options")), | |
c88bba18 | 469 | OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")), |
ac4cc866 | 470 | OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 471 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")), |
ac4cc866 | 472 | OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")), |
ac3f5a34 | 473 | OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")), |
5242860f KN |
474 | OPT_MERGED(&filter, N_("print only tags that are merged")), |
475 | OPT_NO_MERGED(&filter, N_("print only tags that are not merged")), | |
aabfdc95 ØW |
476 | OPT_BOOL(0, "omit-empty", &omit_empty, |
477 | N_("do not output a newline after empty formatted refs")), | |
98e7ab6d | 478 | OPT_REF_SORT(&sorting_options), |
32c35cfb | 479 | { |
ac4cc866 | 480 | OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"), |
1e0c3b68 ÆAB |
481 | N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT, |
482 | parse_opt_object_name, (intptr_t) "HEAD" | |
ae7706b9 | 483 | }, |
4a68e36d JK |
484 | OPT_STRING( 0 , "format", &format.format, N_("format"), |
485 | N_("format to use for the output")), | |
0c88bf50 | 486 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
3bb16a8b | 487 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
39686585 CR |
488 | OPT_END() |
489 | }; | |
37766b61 | 490 | int ret = 0; |
408c5c5c | 491 | const char *only_in_list = NULL; |
08c12ec1 | 492 | char *path = NULL; |
39686585 | 493 | |
56b43607 KN |
494 | setup_ref_filter_porcelain_msg(); |
495 | ||
98e7ab6d | 496 | git_config(git_tag_config, &sorting_options); |
62e09ce9 | 497 | |
d3e05983 | 498 | memset(&opt, 0, sizeof(opt)); |
ac4cc866 KN |
499 | memset(&filter, 0, sizeof(filter)); |
500 | filter.lines = -1; | |
1c6b565f | 501 | opt.sign = -1; |
d3e05983 | 502 | |
37782920 | 503 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
62e09ce9 | 504 | |
6a338149 ÆAB |
505 | if (!cmdmode) { |
506 | if (argc == 0) | |
507 | cmdmode = 'l'; | |
ac3f5a34 | 508 | else if (filter.with_commit || filter.no_commit || |
21bf9339 AL |
509 | filter.reachable_from || filter.unreachable_from || |
510 | filter.points_at.nr || filter.lines != -1) | |
6a338149 ÆAB |
511 | cmdmode = 'l'; |
512 | } | |
c1a41b9d | 513 | |
de121ffe | 514 | if (cmdmode == 'l') |
ff1e7248 | 515 | setup_auto_pager("tag", 1); |
de121ffe | 516 | |
1c6b565f TM |
517 | if (opt.sign == -1) |
518 | opt.sign = cmdmode ? 0 : config_sign_tag > 0; | |
519 | ||
520 | if (keyid) { | |
521 | opt.sign = 1; | |
522 | set_signing_key(keyid); | |
523 | } | |
524 | create_tag_object = (opt.sign || annotate || msg.given || msgfile); | |
525 | ||
61c2fe0c | 526 | if ((create_tag_object || force) && (cmdmode != 0)) |
6fa8342b ST |
527 | usage_with_options(git_tag_usage, options); |
528 | ||
d96e3c15 | 529 | finalize_colopts(&colopts, -1); |
ac4cc866 | 530 | if (cmdmode == 'l' && filter.lines != -1) { |
d96e3c15 | 531 | if (explicitly_enable_column(colopts)) |
12909b6b | 532 | die(_("options '%s' and '%s' cannot be used together"), "--column", "-n"); |
d96e3c15 NTND |
533 | colopts = 0; |
534 | } | |
98e7ab6d | 535 | sorting = ref_sorting_options(&sorting_options); |
7c269a7b | 536 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
3bb16a8b | 537 | filter.ignore_case = icase; |
e6b722db | 538 | if (cmdmode == 'l') { |
d96e3c15 NTND |
539 | if (column_active(colopts)) { |
540 | struct column_options copts; | |
541 | memset(&copts, 0, sizeof(copts)); | |
542 | copts.padding = 2; | |
543 | run_column_filter(colopts, &copts); | |
544 | } | |
ac4cc866 | 545 | filter.name_patterns = argv; |
4a68e36d | 546 | ret = list_tags(&filter, sorting, &format); |
d96e3c15 NTND |
547 | if (column_active(colopts)) |
548 | stop_column_filter(); | |
37766b61 | 549 | goto cleanup; |
d96e3c15 | 550 | } |
ac4cc866 | 551 | if (filter.lines != -1) |
408c5c5c JNA |
552 | only_in_list = "-n"; |
553 | else if (filter.with_commit) | |
554 | only_in_list = "--contains"; | |
555 | else if (filter.no_commit) | |
556 | only_in_list = "--no-contains"; | |
557 | else if (filter.points_at.nr) | |
558 | only_in_list = "--points-at"; | |
559 | else if (filter.reachable_from) | |
560 | only_in_list = "--merged"; | |
561 | else if (filter.unreachable_from) | |
562 | only_in_list = "--no-merged"; | |
563 | if (only_in_list) | |
564 | die(_("the '%s' option is only allowed in list mode"), only_in_list); | |
37766b61 ÆAB |
565 | if (cmdmode == 'd') { |
566 | ret = delete_tags(argv); | |
567 | goto cleanup; | |
568 | } | |
07d347cf | 569 | if (cmdmode == 'v') { |
4a68e36d | 570 | if (format.format && verify_ref_format(&format)) |
2eda0102 | 571 | usage_with_options(git_tag_usage, options); |
37766b61 ÆAB |
572 | ret = for_each_tag_name(argv, verify_tag, &format); |
573 | goto cleanup; | |
07d347cf | 574 | } |
39686585 | 575 | |
bd46c9a9 JH |
576 | if (msg.given || msgfile) { |
577 | if (msg.given && msgfile) | |
12909b6b | 578 | die(_("options '%s' and '%s' cannot be used together"), "-F", "-m"); |
bd46c9a9 JH |
579 | if (msg.given) |
580 | strbuf_addbuf(&buf, &(msg.buf)); | |
39686585 CR |
581 | else { |
582 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 583 | if (strbuf_read(&buf, 0, 1024) < 0) |
d08ebf99 | 584 | die_errno(_("cannot read '%s'"), msgfile); |
387e7e19 | 585 | } else { |
39686585 | 586 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
d08ebf99 | 587 | die_errno(_("could not open or read '%s'"), |
d824cbba | 588 | msgfile); |
62e09ce9 | 589 | } |
62e09ce9 | 590 | } |
62e09ce9 CR |
591 | } |
592 | ||
39686585 | 593 | tag = argv[0]; |
62e09ce9 | 594 | |
39686585 CR |
595 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
596 | if (argc > 2) | |
b8657347 | 597 | die(_("too many arguments")); |
62e09ce9 | 598 | |
d850b7a5 | 599 | if (repo_get_oid(the_repository, object_ref, &object)) |
d08ebf99 | 600 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
62e09ce9 | 601 | |
4f0accd6 | 602 | if (strbuf_check_tag_ref(&ref, tag)) |
d08ebf99 | 603 | die(_("'%s' is not a valid tag name."), tag); |
62e09ce9 | 604 | |
34c290a6 | 605 | if (read_ref(ref.buf, &prev)) |
7422ab50 | 606 | oidclr(&prev); |
62e09ce9 | 607 | else if (!force) |
d08ebf99 | 608 | die(_("tag '%s' already exists"), tag); |
62e09ce9 | 609 | |
d3e05983 | 610 | opt.message_given = msg.given || msgfile; |
9eed6e40 | 611 | opt.use_editor = edit_flag; |
d3e05983 KS |
612 | |
613 | if (!cleanup_arg || !strcmp(cleanup_arg, "strip")) | |
614 | opt.cleanup_mode = CLEANUP_ALL; | |
615 | else if (!strcmp(cleanup_arg, "verbatim")) | |
616 | opt.cleanup_mode = CLEANUP_NONE; | |
617 | else if (!strcmp(cleanup_arg, "whitespace")) | |
618 | opt.cleanup_mode = CLEANUP_SPACE; | |
619 | else | |
620 | die(_("Invalid cleanup mode %s"), cleanup_arg); | |
621 | ||
7422ab50 | 622 | create_reflog_msg(&object, &reflog_msg); |
df8512ed | 623 | |
61c2fe0c LA |
624 | if (create_tag_object) { |
625 | if (force_sign_annotate && !annotate) | |
626 | opt.sign = 1; | |
08c12ec1 KH |
627 | path = git_pathdup("TAG_EDITMSG"); |
628 | create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object, | |
629 | path); | |
61c2fe0c | 630 | } |
62e09ce9 | 631 | |
e5074bfe RS |
632 | transaction = ref_transaction_begin(&err); |
633 | if (!transaction || | |
89f3bbdd | 634 | ref_transaction_update(transaction, ref.buf, &object, &prev, |
144c76fa | 635 | create_reflog ? REF_FORCE_CREATE_REFLOG : 0, |
df8512ed | 636 | reflog_msg.buf, &err) || |
08c12ec1 KH |
637 | ref_transaction_commit(transaction, &err)) { |
638 | if (path) | |
639 | fprintf(stderr, | |
640 | _("The tag message has been left in %s\n"), | |
641 | path); | |
e5074bfe | 642 | die("%s", err.buf); |
08c12ec1 KH |
643 | } |
644 | if (path) { | |
645 | unlink_or_warn(path); | |
646 | free(path); | |
647 | } | |
e5074bfe | 648 | ref_transaction_free(transaction); |
9001dc2a | 649 | if (force && !is_null_oid(&prev) && !oideq(&prev, &object)) |
aab9583f | 650 | printf(_("Updated tag '%s' (was %s)\n"), tag, |
d850b7a5 | 651 | repo_find_unique_abbrev(the_repository, &prev, DEFAULT_ABBREV)); |
62e09ce9 | 652 | |
37766b61 | 653 | cleanup: |
e5fb0286 | 654 | ref_sorting_release(sorting); |
37766b61 ÆAB |
655 | strbuf_release(&buf); |
656 | strbuf_release(&ref); | |
657 | strbuf_release(&reflog_msg); | |
658 | strbuf_release(&msg.buf); | |
659 | strbuf_release(&err); | |
7ce4088a | 660 | free(msgfile); |
37766b61 | 661 | return ret; |
62e09ce9 | 662 | } |