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