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