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