tag: change default of `pager.tag` to "on"
[git/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
9#include "cache.h"
b2141fc1 10#include "config.h"
62e09ce9
CR
11#include "builtin.h"
12#include "refs.h"
13#include "tag.h"
14#include "run-command.h"
39686585 15#include "parse-options.h"
ffc4b801
JK
16#include "diff.h"
17#include "revision.h"
2f47eae2 18#include "gpg-interface.h"
ae7706b9 19#include "sha1-array.h"
d96e3c15 20#include "column.h"
ac4cc866 21#include "ref-filter.h"
39686585
CR
22
23static const char * const git_tag_usage[] = {
9c9b4f2f 24 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
c88bba18 25 N_("git tag -d <tagname>..."),
ac3f5a34 26 N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]"
5242860f 27 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
07d347cf 28 N_("git tag -v [--format=<format>] <tagname>..."),
39686585
CR
29 NULL
30};
62e09ce9 31
d96e3c15 32static unsigned int colopts;
61c2fe0c 33static int force_sign_annotate;
ae7706b9 34
df094741 35static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
ca516999 36{
ac4cc866 37 struct ref_array array;
df094741 38 char *to_free = NULL;
ca516999 39 int i;
62e09ce9 40
ac4cc866 41 memset(&array, 0, sizeof(array));
32c35cfb 42
ac4cc866
KN
43 if (filter->lines == -1)
44 filter->lines = 0;
ae7706b9 45
df094741
KN
46 if (!format) {
47 if (filter->lines) {
48 to_free = xstrfmt("%s %%(contents:lines=%d)",
17938f17 49 "%(align:15)%(refname:lstrip=2)%(end)",
df094741
KN
50 filter->lines);
51 format = to_free;
52 } else
17938f17 53 format = "%(refname:lstrip=2)";
62e09ce9
CR
54 }
55
b7cc53e9 56 verify_ref_format(format);
008ed7df 57 filter->with_commit_tag_algo = 1;
b7cc53e9
KN
58 filter_refs(&array, filter, FILTER_REFS_TAGS);
59 ref_array_sort(sorting, &array);
62e09ce9 60
b7cc53e9
KN
61 for (i = 0; i < array.nr; i++)
62 show_ref_array_item(array.items[i], format, 0);
63 ref_array_clear(&array);
64 free(to_free);
9ef176b5 65
62e09ce9
CR
66 return 0;
67}
68
e317cfaf 69typedef int (*each_tag_name_fn)(const char *name, const char *ref,
7422ab50 70 const struct object_id *oid, const void *cb_data);
62e09ce9 71
07d347cf
LP
72static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
73 const void *cb_data)
62e09ce9
CR
74{
75 const char **p;
7f897b6f 76 struct strbuf ref = STRBUF_INIT;
62e09ce9 77 int had_error = 0;
7422ab50 78 struct object_id oid;
62e09ce9
CR
79
80 for (p = argv; *p; p++) {
7f897b6f
JK
81 strbuf_reset(&ref);
82 strbuf_addf(&ref, "refs/tags/%s", *p);
7422ab50 83 if (read_ref(ref.buf, oid.hash)) {
d08ebf99 84 error(_("tag '%s' not found."), *p);
62e09ce9
CR
85 had_error = 1;
86 continue;
87 }
7422ab50 88 if (fn(*p, ref.buf, &oid, cb_data))
62e09ce9
CR
89 had_error = 1;
90 }
7f897b6f 91 strbuf_release(&ref);
62e09ce9
CR
92 return had_error;
93}
94
95static int delete_tag(const char *name, const char *ref,
7422ab50 96 const struct object_id *oid, const void *cb_data)
62e09ce9 97{
7422ab50 98 if (delete_ref(NULL, ref, oid->hash, 0))
62e09ce9 99 return 1;
7422ab50 100 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
62e09ce9
CR
101 return 0;
102}
103
104static int verify_tag(const char *name, const char *ref,
7422ab50 105 const struct object_id *oid, const void *cb_data)
62e09ce9 106{
07d347cf
LP
107 int flags;
108 const char *fmt_pretty = cb_data;
109 flags = GPG_VERIFY_VERBOSE;
110
111 if (fmt_pretty)
112 flags = GPG_VERIFY_OMIT_STATUS;
113
7422ab50 114 if (gpg_verify_tag(oid->hash, name, flags))
07d347cf
LP
115 return -1;
116
117 if (fmt_pretty)
7422ab50 118 pretty_print_ref(name, oid->hash, fmt_pretty);
07d347cf
LP
119
120 return 0;
62e09ce9
CR
121}
122
fd17f5b5 123static int do_sign(struct strbuf *buffer)
62e09ce9 124{
2f47eae2 125 return sign_buffer(buffer, buffer, get_signing_key());
62e09ce9
CR
126}
127
128static const char tag_template[] =
d78f340e 129 N_("\nWrite a message for tag:\n %s\n"
eff80a9f 130 "Lines starting with '%c' will be ignored.\n");
d3e05983
KS
131
132static const char tag_template_nocleanup[] =
d78f340e 133 N_("\nWrite a message for tag:\n %s\n"
eff80a9f
JH
134 "Lines starting with '%c' will be kept; you may remove them"
135 " yourself if you want to.\n");
62e09ce9 136
b7cc53e9
KN
137/* Parse arg given and add it the ref_sorting array */
138static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
b150794d 139{
b7cc53e9
KN
140 struct ref_sorting *s;
141 int len;
b150794d 142
b7cc53e9
KN
143 s = xcalloc(1, sizeof(*s));
144 s->next = *sorting_tail;
145 *sorting_tail = s;
b150794d 146
b7cc53e9
KN
147 if (*arg == '-') {
148 s->reverse = 1;
149 arg++;
b150794d 150 }
b7cc53e9
KN
151 if (skip_prefix(arg, "version:", &arg) ||
152 skip_prefix(arg, "v:", &arg))
153 s->version = 1;
b150794d 154
b7cc53e9
KN
155 len = strlen(arg);
156 s->atom = parse_ref_filter_atom(arg, arg+len);
b150794d
JK
157
158 return 0;
159}
160
ef90d6d4 161static int git_tag_config(const char *var, const char *value, void *cb)
62e09ce9 162{
b150794d 163 int status;
b7cc53e9 164 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
b150794d
JK
165
166 if (!strcmp(var, "tag.sort")) {
167 if (!value)
168 return config_error_nonbool(var);
b7cc53e9 169 parse_sorting_string(value, sorting_tail);
b150794d
JK
170 return 0;
171 }
172
173 status = git_gpg_config(var, value, cb);
2f47eae2
JH
174 if (status)
175 return status;
61c2fe0c
LA
176 if (!strcmp(var, "tag.forcesignannotated")) {
177 force_sign_annotate = git_config_bool(var, value);
178 return 0;
179 }
180
59556548 181 if (starts_with(var, "column."))
d96e3c15 182 return git_column_config(var, value, "tag", &colopts);
ef90d6d4 183 return git_default_config(var, value, cb);
62e09ce9
CR
184}
185
7422ab50 186static void write_tag_body(int fd, const struct object_id *oid)
bab8118a
MH
187{
188 unsigned long size;
189 enum object_type type;
e10dfb62 190 char *buf, *sp;
bab8118a 191
7422ab50 192 buf = read_sha1_file(oid->hash, &type, &size);
bab8118a
MH
193 if (!buf)
194 return;
195 /* skip header */
196 sp = strstr(buf, "\n\n");
197
198 if (!sp || !size || type != OBJ_TAG) {
199 free(buf);
200 return;
201 }
202 sp += 2; /* skip the 2 LFs */
e10dfb62 203 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
bab8118a
MH
204
205 free(buf);
206}
207
7422ab50 208static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
3927bbe9
JK
209{
210 if (sign && do_sign(buf) < 0)
d08ebf99 211 return error(_("unable to sign the tag"));
7422ab50 212 if (write_sha1_file(buf->buf, buf->len, tag_type, result->hash) < 0)
d08ebf99 213 return error(_("unable to write tag file"));
3927bbe9
JK
214 return 0;
215}
216
d3e05983
KS
217struct create_tag_options {
218 unsigned int message_given:1;
219 unsigned int sign;
220 enum {
221 CLEANUP_NONE,
222 CLEANUP_SPACE,
223 CLEANUP_ALL
224 } cleanup_mode;
225};
226
7422ab50 227static void create_tag(const struct object_id *object, const char *tag,
d3e05983 228 struct strbuf *buf, struct create_tag_options *opt,
7422ab50 229 struct object_id *prev, struct object_id *result)
62e09ce9
CR
230{
231 enum object_type type;
b0ceab98 232 struct strbuf header = STRBUF_INIT;
3927bbe9 233 char *path = NULL;
62e09ce9 234
7422ab50 235 type = sha1_object_info(object->hash, NULL);
e317cfaf 236 if (type <= OBJ_NONE)
d08ebf99 237 die(_("bad object type."));
62e09ce9 238
b0ceab98
JK
239 strbuf_addf(&header,
240 "object %s\n"
241 "type %s\n"
242 "tag %s\n"
243 "tagger %s\n\n",
7422ab50 244 oid_to_hex(object),
b0ceab98
JK
245 typename(type),
246 tag,
247 git_committer_info(IDENT_STRICT));
62e09ce9 248
d3e05983 249 if (!opt->message_given) {
62e09ce9
CR
250 int fd;
251
252 /* write the template message before editing: */
a4f34cbb 253 path = git_pathdup("TAG_EDITMSG");
62e09ce9
CR
254 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
255 if (fd < 0)
d08ebf99 256 die_errno(_("could not create file '%s'"), path);
bab8118a 257
7422ab50 258 if (!is_null_oid(prev)) {
bab8118a 259 write_tag_body(fd, prev);
eff80a9f
JH
260 } else {
261 struct strbuf buf = STRBUF_INIT;
262 strbuf_addch(&buf, '\n');
263 if (opt->cleanup_mode == CLEANUP_ALL)
d78f340e 264 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
eff80a9f 265 else
d78f340e 266 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
eff80a9f
JH
267 write_or_die(fd, buf.buf, buf.len);
268 strbuf_release(&buf);
269 }
62e09ce9
CR
270 close(fd);
271
7198203a
SB
272 if (launch_editor(path, buf, NULL)) {
273 fprintf(stderr,
d08ebf99 274 _("Please supply the message using either -m or -F option.\n"));
7198203a
SB
275 exit(1);
276 }
62e09ce9 277 }
62e09ce9 278
d3e05983 279 if (opt->cleanup_mode != CLEANUP_NONE)
63af4a84 280 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
62e09ce9 281
d3e05983 282 if (!opt->message_given && !buf->len)
d08ebf99 283 die(_("no tag message?"));
62e09ce9 284
b0ceab98
JK
285 strbuf_insert(buf, 0, header.buf, header.len);
286 strbuf_release(&header);
62e09ce9 287
d3e05983 288 if (build_tag_object(buf, opt->sign, result) < 0) {
3927bbe9 289 if (path)
d08ebf99 290 fprintf(stderr, _("The tag message has been left in %s\n"),
3927bbe9
JK
291 path);
292 exit(128);
293 }
294 if (path) {
691f1a28 295 unlink_or_warn(path);
3927bbe9
JK
296 free(path);
297 }
62e09ce9
CR
298}
299
7422ab50 300static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
df8512ed
CW
301{
302 enum object_type type;
303 struct commit *c;
304 char *buf;
305 unsigned long size;
306 int subject_len = 0;
307 const char *subject_start;
308
309 char *rla = getenv("GIT_REFLOG_ACTION");
310 if (rla) {
311 strbuf_addstr(sb, rla);
312 } else {
c3027be9 313 strbuf_addstr(sb, "tag: tagging ");
7422ab50 314 strbuf_add_unique_abbrev(sb, oid->hash, DEFAULT_ABBREV);
df8512ed
CW
315 }
316
317 strbuf_addstr(sb, " (");
7422ab50 318 type = sha1_object_info(oid->hash, NULL);
df8512ed
CW
319 switch (type) {
320 default:
c3027be9 321 strbuf_addstr(sb, "object of unknown type");
df8512ed
CW
322 break;
323 case OBJ_COMMIT:
7422ab50 324 if ((buf = read_sha1_file(oid->hash, &type, &size)) != NULL) {
df8512ed
CW
325 subject_len = find_commit_subject(buf, &subject_start);
326 strbuf_insert(sb, sb->len, subject_start, subject_len);
327 } else {
c3027be9 328 strbuf_addstr(sb, "commit object");
df8512ed
CW
329 }
330 free(buf);
331
bc83266a 332 if ((c = lookup_commit_reference(oid)) != NULL)
df8512ed
CW
333 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
334 break;
335 case OBJ_TREE:
c3027be9 336 strbuf_addstr(sb, "tree object");
df8512ed
CW
337 break;
338 case OBJ_BLOB:
c3027be9 339 strbuf_addstr(sb, "blob object");
df8512ed
CW
340 break;
341 case OBJ_TAG:
c3027be9 342 strbuf_addstr(sb, "other tag object");
df8512ed
CW
343 break;
344 }
345 strbuf_addch(sb, ')');
346}
347
bd46c9a9
JH
348struct msg_arg {
349 int given;
350 struct strbuf buf;
351};
352
353static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
354{
355 struct msg_arg *msg = opt->value;
356
357 if (!arg)
358 return -1;
359 if (msg->buf.len)
360 strbuf_addstr(&(msg->buf), "\n\n");
361 strbuf_addstr(&(msg->buf), arg);
362 msg->given = 1;
363 return 0;
364}
365
4f0accd6
MS
366static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
367{
368 if (name[0] == '-')
8d9c5010 369 return -1;
4f0accd6
MS
370
371 strbuf_reset(sb);
372 strbuf_addf(sb, "refs/tags/%s", name);
373
8d9c5010 374 return check_refname_format(sb->buf, 0);
4f0accd6
MS
375}
376
62e09ce9
CR
377int cmd_tag(int argc, const char **argv, const char *prefix)
378{
f285a2d7 379 struct strbuf buf = STRBUF_INIT;
4f0accd6 380 struct strbuf ref = STRBUF_INIT;
df8512ed 381 struct strbuf reflog_msg = STRBUF_INIT;
7422ab50 382 struct object_id object, prev;
62e09ce9 383 const char *object_ref, *tag;
d3e05983
KS
384 struct create_tag_options opt;
385 char *cleanup_arg = NULL;
144c76fa 386 int create_reflog = 0;
ac4cc866 387 int annotate = 0, force = 0;
61c2fe0c 388 int cmdmode = 0, create_tag_object = 0;
dbd0f5c7 389 const char *msgfile = NULL, *keyid = NULL;
bd46c9a9 390 struct msg_arg msg = { 0, STRBUF_INIT };
e5074bfe
RS
391 struct ref_transaction *transaction;
392 struct strbuf err = STRBUF_INIT;
ac4cc866 393 struct ref_filter filter;
b7cc53e9 394 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
df094741 395 const char *format = NULL;
3bb16a8b 396 int icase = 0;
39686585 397 struct option options[] = {
e6b722db 398 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
ac4cc866 399 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
c88bba18 400 N_("print <n> lines of each tag message"),
39686585 401 PARSE_OPT_OPTARG, NULL, 1 },
e6b722db
JH
402 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
403 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
39686585 404
c88bba18 405 OPT_GROUP(N_("Tag creation options")),
d5d09d47 406 OPT_BOOL('a', "annotate", &annotate,
c88bba18
NTND
407 N_("annotated tag, needs a message")),
408 OPT_CALLBACK('m', "message", &msg, N_("message"),
409 N_("tag message"), parse_msg_arg),
410 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
d5d09d47 411 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
c88bba18
NTND
412 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
413 N_("how to strip spaces and #comments from message")),
e703d711 414 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
c88bba18
NTND
415 N_("use another key to sign the tag")),
416 OPT__FORCE(&force, N_("replace the tag if exists")),
98c32bd8 417 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
dd059c6c
JK
418
419 OPT_GROUP(N_("Tag listing options")),
c88bba18 420 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
ac4cc866 421 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
ac3f5a34 422 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
ac4cc866 423 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
ac3f5a34 424 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
5242860f
KN
425 OPT_MERGED(&filter, N_("print only tags that are merged")),
426 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
b7cc53e9
KN
427 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
428 N_("field name to sort on"), &parse_opt_ref_sorting),
32c35cfb 429 {
ac4cc866 430 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
1e0c3b68
ÆAB
431 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
432 parse_opt_object_name, (intptr_t) "HEAD"
ae7706b9 433 },
df094741 434 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
3bb16a8b 435 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
39686585
CR
436 OPT_END()
437 };
438
56b43607
KN
439 setup_ref_filter_porcelain_msg();
440
b7cc53e9 441 git_config(git_tag_config, sorting_tail);
62e09ce9 442
d3e05983 443 memset(&opt, 0, sizeof(opt));
ac4cc866
KN
444 memset(&filter, 0, sizeof(filter));
445 filter.lines = -1;
d3e05983 446
37782920 447 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
62e09ce9 448
be15f505 449 if (keyid) {
d3e05983 450 opt.sign = 1;
2f47eae2 451 set_signing_key(keyid);
be15f505 452 }
61c2fe0c
LA
453 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
454
6a338149
ÆAB
455 if (!cmdmode) {
456 if (argc == 0)
457 cmdmode = 'l';
ac3f5a34 458 else if (filter.with_commit || filter.no_commit ||
6a338149
ÆAB
459 filter.points_at.nr || filter.merge_commit ||
460 filter.lines != -1)
461 cmdmode = 'l';
462 }
c1a41b9d 463
de121ffe 464 if (cmdmode == 'l')
ff1e7248 465 setup_auto_pager("tag", 1);
de121ffe 466
61c2fe0c 467 if ((create_tag_object || force) && (cmdmode != 0))
6fa8342b
ST
468 usage_with_options(git_tag_usage, options);
469
d96e3c15 470 finalize_colopts(&colopts, -1);
ac4cc866 471 if (cmdmode == 'l' && filter.lines != -1) {
d96e3c15
NTND
472 if (explicitly_enable_column(colopts))
473 die(_("--column and -n are incompatible"));
474 colopts = 0;
475 }
b7cc53e9
KN
476 if (!sorting)
477 sorting = ref_default_sorting();
3bb16a8b
NTND
478 sorting->ignore_case = icase;
479 filter.ignore_case = icase;
e6b722db 480 if (cmdmode == 'l') {
d96e3c15
NTND
481 int ret;
482 if (column_active(colopts)) {
483 struct column_options copts;
484 memset(&copts, 0, sizeof(copts));
485 copts.padding = 2;
486 run_column_filter(colopts, &copts);
487 }
ac4cc866 488 filter.name_patterns = argv;
df094741 489 ret = list_tags(&filter, sorting, format);
d96e3c15
NTND
490 if (column_active(colopts))
491 stop_column_filter();
492 return ret;
493 }
ac4cc866 494 if (filter.lines != -1)
6a338149 495 die(_("-n option is only allowed in list mode"));
ac4cc866 496 if (filter.with_commit)
6a338149 497 die(_("--contains option is only allowed in list mode"));
ac3f5a34
ÆAB
498 if (filter.no_commit)
499 die(_("--no-contains option is only allowed in list mode"));
ac4cc866 500 if (filter.points_at.nr)
6a338149 501 die(_("--points-at option is only allowed in list mode"));
5242860f 502 if (filter.merge_commit)
6a338149 503 die(_("--merged and --no-merged options are only allowed in list mode"));
e6b722db 504 if (cmdmode == 'd')
07d347cf
LP
505 return for_each_tag_name(argv, delete_tag, NULL);
506 if (cmdmode == 'v') {
507 if (format)
508 verify_ref_format(format);
509 return for_each_tag_name(argv, verify_tag, format);
510 }
39686585 511
bd46c9a9
JH
512 if (msg.given || msgfile) {
513 if (msg.given && msgfile)
d08ebf99 514 die(_("only one -F or -m option is allowed."));
bd46c9a9
JH
515 if (msg.given)
516 strbuf_addbuf(&buf, &(msg.buf));
39686585
CR
517 else {
518 if (!strcmp(msgfile, "-")) {
387e7e19 519 if (strbuf_read(&buf, 0, 1024) < 0)
d08ebf99 520 die_errno(_("cannot read '%s'"), msgfile);
387e7e19 521 } else {
39686585 522 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
d08ebf99 523 die_errno(_("could not open or read '%s'"),
d824cbba 524 msgfile);
62e09ce9 525 }
62e09ce9 526 }
62e09ce9
CR
527 }
528
39686585 529 tag = argv[0];
62e09ce9 530
39686585
CR
531 object_ref = argc == 2 ? argv[1] : "HEAD";
532 if (argc > 2)
d08ebf99 533 die(_("too many params"));
62e09ce9 534
7422ab50 535 if (get_oid(object_ref, &object))
d08ebf99 536 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
62e09ce9 537
4f0accd6 538 if (strbuf_check_tag_ref(&ref, tag))
d08ebf99 539 die(_("'%s' is not a valid tag name."), tag);
62e09ce9 540
7422ab50 541 if (read_ref(ref.buf, prev.hash))
542 oidclr(&prev);
62e09ce9 543 else if (!force)
d08ebf99 544 die(_("tag '%s' already exists"), tag);
62e09ce9 545
d3e05983
KS
546 opt.message_given = msg.given || msgfile;
547
548 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
549 opt.cleanup_mode = CLEANUP_ALL;
550 else if (!strcmp(cleanup_arg, "verbatim"))
551 opt.cleanup_mode = CLEANUP_NONE;
552 else if (!strcmp(cleanup_arg, "whitespace"))
553 opt.cleanup_mode = CLEANUP_SPACE;
554 else
555 die(_("Invalid cleanup mode %s"), cleanup_arg);
556
7422ab50 557 create_reflog_msg(&object, &reflog_msg);
df8512ed 558
61c2fe0c
LA
559 if (create_tag_object) {
560 if (force_sign_annotate && !annotate)
561 opt.sign = 1;
7422ab50 562 create_tag(&object, tag, &buf, &opt, &prev, &object);
61c2fe0c 563 }
62e09ce9 564
e5074bfe
RS
565 transaction = ref_transaction_begin(&err);
566 if (!transaction ||
7422ab50 567 ref_transaction_update(transaction, ref.buf, object.hash, prev.hash,
144c76fa 568 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
df8512ed 569 reflog_msg.buf, &err) ||
db7516ab 570 ref_transaction_commit(transaction, &err))
e5074bfe
RS
571 die("%s", err.buf);
572 ref_transaction_free(transaction);
7422ab50 573 if (force && !is_null_oid(&prev) && oidcmp(&prev, &object))
574 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev.hash, DEFAULT_ABBREV));
62e09ce9 575
e5074bfe 576 strbuf_release(&err);
fd17f5b5 577 strbuf_release(&buf);
4f0accd6 578 strbuf_release(&ref);
df8512ed 579 strbuf_release(&reflog_msg);
62e09ce9
CR
580 return 0;
581}