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