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