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