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