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