ref-filter: add support for %(contents:lines=X)
[git] / builtin / tag.c
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"
10 #include "builtin.h"
11 #include "refs.h"
12 #include "tag.h"
13 #include "run-command.h"
14 #include "parse-options.h"
15 #include "diff.h"
16 #include "revision.h"
17 #include "gpg-interface.h"
18 #include "sha1-array.h"
19 #include "column.h"
20
21 static const char * const git_tag_usage[] = {
22         N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
23         N_("git tag -d <tagname>..."),
24         N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
25                 "\n\t\t[<pattern>...]"),
26         N_("git tag -v <tagname>..."),
27         NULL
28 };
29
30 #define STRCMP_SORT     0       /* must be zero */
31 #define VERCMP_SORT     1
32 #define SORT_MASK       0x7fff
33 #define REVERSE_SORT    0x8000
34
35 static int tag_sort;
36
37 struct tag_filter {
38         const char **patterns;
39         int lines;
40         int sort;
41         struct string_list tags;
42         struct commit_list *with_commit;
43 };
44
45 static struct sha1_array points_at;
46 static unsigned int colopts;
47
48 static int match_pattern(const char **patterns, const char *ref)
49 {
50         /* no pattern means match everything */
51         if (!*patterns)
52                 return 1;
53         for (; *patterns; patterns++)
54                 if (!wildmatch(*patterns, ref, 0, NULL))
55                         return 1;
56         return 0;
57 }
58
59 /*
60  * This is currently duplicated in ref-filter.c, and will eventually be
61  * removed as we port tag.c to use the ref-filter APIs.
62  */
63 static const unsigned char *match_points_at(const char *refname,
64                                             const unsigned char *sha1)
65 {
66         const unsigned char *tagged_sha1 = NULL;
67         struct object *obj;
68
69         if (sha1_array_lookup(&points_at, sha1) >= 0)
70                 return sha1;
71         obj = parse_object(sha1);
72         if (!obj)
73                 die(_("malformed object at '%s'"), refname);
74         if (obj->type == OBJ_TAG)
75                 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
76         if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
77                 return tagged_sha1;
78         return NULL;
79 }
80
81 static int in_commit_list(const struct commit_list *want, struct commit *c)
82 {
83         for (; want; want = want->next)
84                 if (!hashcmp(want->item->object.sha1, c->object.sha1))
85                         return 1;
86         return 0;
87 }
88
89 /*
90  * The entire code segment for supporting the --contains option has been
91  * copied over to ref-filter.{c,h}. This will be deleted evetually when
92  * we port tag.c to use ref-filter APIs.
93  */
94 enum contains_result {
95         CONTAINS_UNKNOWN = -1,
96         CONTAINS_NO = 0,
97         CONTAINS_YES = 1
98 };
99
100 /*
101  * Test whether the candidate or one of its parents is contained in the list.
102  * Do not recurse to find out, though, but return -1 if inconclusive.
103  */
104 static enum contains_result contains_test(struct commit *candidate,
105                             const struct commit_list *want)
106 {
107         /* was it previously marked as containing a want commit? */
108         if (candidate->object.flags & TMP_MARK)
109                 return 1;
110         /* or marked as not possibly containing a want commit? */
111         if (candidate->object.flags & UNINTERESTING)
112                 return 0;
113         /* or are we it? */
114         if (in_commit_list(want, candidate)) {
115                 candidate->object.flags |= TMP_MARK;
116                 return 1;
117         }
118
119         if (parse_commit(candidate) < 0)
120                 return 0;
121
122         return -1;
123 }
124
125 /*
126  * Mimicking the real stack, this stack lives on the heap, avoiding stack
127  * overflows.
128  *
129  * At each recursion step, the stack items points to the commits whose
130  * ancestors are to be inspected.
131  */
132 struct stack {
133         int nr, alloc;
134         struct stack_entry {
135                 struct commit *commit;
136                 struct commit_list *parents;
137         } *stack;
138 };
139
140 static void push_to_stack(struct commit *candidate, struct stack *stack)
141 {
142         int index = stack->nr++;
143         ALLOC_GROW(stack->stack, stack->nr, stack->alloc);
144         stack->stack[index].commit = candidate;
145         stack->stack[index].parents = candidate->parents;
146 }
147
148 static enum contains_result contains(struct commit *candidate,
149                 const struct commit_list *want)
150 {
151         struct stack stack = { 0, 0, NULL };
152         int result = contains_test(candidate, want);
153
154         if (result != CONTAINS_UNKNOWN)
155                 return result;
156
157         push_to_stack(candidate, &stack);
158         while (stack.nr) {
159                 struct stack_entry *entry = &stack.stack[stack.nr - 1];
160                 struct commit *commit = entry->commit;
161                 struct commit_list *parents = entry->parents;
162
163                 if (!parents) {
164                         commit->object.flags |= UNINTERESTING;
165                         stack.nr--;
166                 }
167                 /*
168                  * If we just popped the stack, parents->item has been marked,
169                  * therefore contains_test will return a meaningful 0 or 1.
170                  */
171                 else switch (contains_test(parents->item, want)) {
172                 case CONTAINS_YES:
173                         commit->object.flags |= TMP_MARK;
174                         stack.nr--;
175                         break;
176                 case CONTAINS_NO:
177                         entry->parents = parents->next;
178                         break;
179                 case CONTAINS_UNKNOWN:
180                         push_to_stack(parents->item, &stack);
181                         break;
182                 }
183         }
184         free(stack.stack);
185         return contains_test(candidate, want);
186 }
187
188 /*
189  * Currently modified and used in ref-filter as append_lines(), will
190  * eventually be removed as we port tag.c to use ref-filter APIs.
191  */
192 static void show_tag_lines(const struct object_id *oid, int lines)
193 {
194         int i;
195         unsigned long size;
196         enum object_type type;
197         char *buf, *sp, *eol;
198         size_t len;
199
200         buf = read_sha1_file(oid->hash, &type, &size);
201         if (!buf)
202                 die_errno("unable to read object %s", oid_to_hex(oid));
203         if (type != OBJ_COMMIT && type != OBJ_TAG)
204                 goto free_return;
205         if (!size)
206                 die("an empty %s object %s?",
207                     typename(type), oid_to_hex(oid));
208
209         /* skip header */
210         sp = strstr(buf, "\n\n");
211         if (!sp)
212                 goto free_return;
213
214         /* only take up to "lines" lines, and strip the signature from a tag */
215         if (type == OBJ_TAG)
216                 size = parse_signature(buf, size);
217         for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
218                 if (i)
219                         printf("\n    ");
220                 eol = memchr(sp, '\n', size - (sp - buf));
221                 len = eol ? eol - sp : size - (sp - buf);
222                 fwrite(sp, len, 1, stdout);
223                 if (!eol)
224                         break;
225                 sp = eol + 1;
226         }
227 free_return:
228         free(buf);
229 }
230
231 static int show_reference(const char *refname, const struct object_id *oid,
232                           int flag, void *cb_data)
233 {
234         struct tag_filter *filter = cb_data;
235
236         if (match_pattern(filter->patterns, refname)) {
237                 if (filter->with_commit) {
238                         struct commit *commit;
239
240                         commit = lookup_commit_reference_gently(oid->hash, 1);
241                         if (!commit)
242                                 return 0;
243                         if (!contains(commit, filter->with_commit))
244                                 return 0;
245                 }
246
247                 if (points_at.nr && !match_points_at(refname, oid->hash))
248                         return 0;
249
250                 if (!filter->lines) {
251                         if (filter->sort)
252                                 string_list_append(&filter->tags, refname);
253                         else
254                                 printf("%s\n", refname);
255                         return 0;
256                 }
257                 printf("%-15s ", refname);
258                 show_tag_lines(oid, filter->lines);
259                 putchar('\n');
260         }
261
262         return 0;
263 }
264
265 static int sort_by_version(const void *a_, const void *b_)
266 {
267         const struct string_list_item *a = a_;
268         const struct string_list_item *b = b_;
269         return versioncmp(a->string, b->string);
270 }
271
272 static int list_tags(const char **patterns, int lines,
273                      struct commit_list *with_commit, int sort)
274 {
275         struct tag_filter filter;
276
277         filter.patterns = patterns;
278         filter.lines = lines;
279         filter.sort = sort;
280         filter.with_commit = with_commit;
281         memset(&filter.tags, 0, sizeof(filter.tags));
282         filter.tags.strdup_strings = 1;
283
284         for_each_tag_ref(show_reference, (void *)&filter);
285         if (sort) {
286                 int i;
287                 if ((sort & SORT_MASK) == VERCMP_SORT)
288                         qsort(filter.tags.items, filter.tags.nr,
289                               sizeof(struct string_list_item), sort_by_version);
290                 if (sort & REVERSE_SORT)
291                         for (i = filter.tags.nr - 1; i >= 0; i--)
292                                 printf("%s\n", filter.tags.items[i].string);
293                 else
294                         for (i = 0; i < filter.tags.nr; i++)
295                                 printf("%s\n", filter.tags.items[i].string);
296                 string_list_clear(&filter.tags, 0);
297         }
298         return 0;
299 }
300
301 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
302                                 const unsigned char *sha1);
303
304 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
305 {
306         const char **p;
307         char ref[PATH_MAX];
308         int had_error = 0;
309         unsigned char sha1[20];
310
311         for (p = argv; *p; p++) {
312                 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
313                                         >= sizeof(ref)) {
314                         error(_("tag name too long: %.*s..."), 50, *p);
315                         had_error = 1;
316                         continue;
317                 }
318                 if (read_ref(ref, sha1)) {
319                         error(_("tag '%s' not found."), *p);
320                         had_error = 1;
321                         continue;
322                 }
323                 if (fn(*p, ref, sha1))
324                         had_error = 1;
325         }
326         return had_error;
327 }
328
329 static int delete_tag(const char *name, const char *ref,
330                                 const unsigned char *sha1)
331 {
332         if (delete_ref(ref, sha1, 0))
333                 return 1;
334         printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
335         return 0;
336 }
337
338 static int verify_tag(const char *name, const char *ref,
339                                 const unsigned char *sha1)
340 {
341         const char *argv_verify_tag[] = {"verify-tag",
342                                         "-v", "SHA1_HEX", NULL};
343         argv_verify_tag[2] = sha1_to_hex(sha1);
344
345         if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
346                 return error(_("could not verify the tag '%s'"), name);
347         return 0;
348 }
349
350 static int do_sign(struct strbuf *buffer)
351 {
352         return sign_buffer(buffer, buffer, get_signing_key());
353 }
354
355 static const char tag_template[] =
356         N_("\nWrite a message for tag:\n  %s\n"
357         "Lines starting with '%c' will be ignored.\n");
358
359 static const char tag_template_nocleanup[] =
360         N_("\nWrite a message for tag:\n  %s\n"
361         "Lines starting with '%c' will be kept; you may remove them"
362         " yourself if you want to.\n");
363
364 /*
365  * Parse a sort string, and return 0 if parsed successfully. Will return
366  * non-zero when the sort string does not parse into a known type. If var is
367  * given, the error message becomes a warning and includes information about
368  * the configuration value.
369  */
370 static int parse_sort_string(const char *var, const char *arg, int *sort)
371 {
372         int type = 0, flags = 0;
373
374         if (skip_prefix(arg, "-", &arg))
375                 flags |= REVERSE_SORT;
376
377         if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
378                 type = VERCMP_SORT;
379         else
380                 type = STRCMP_SORT;
381
382         if (strcmp(arg, "refname")) {
383                 if (!var)
384                         return error(_("unsupported sort specification '%s'"), arg);
385                 else {
386                         warning(_("unsupported sort specification '%s' in variable '%s'"),
387                                 var, arg);
388                         return -1;
389                 }
390         }
391
392         *sort = (type | flags);
393
394         return 0;
395 }
396
397 static int git_tag_config(const char *var, const char *value, void *cb)
398 {
399         int status;
400
401         if (!strcmp(var, "tag.sort")) {
402                 if (!value)
403                         return config_error_nonbool(var);
404                 parse_sort_string(var, value, &tag_sort);
405                 return 0;
406         }
407
408         status = git_gpg_config(var, value, cb);
409         if (status)
410                 return status;
411         if (starts_with(var, "column."))
412                 return git_column_config(var, value, "tag", &colopts);
413         return git_default_config(var, value, cb);
414 }
415
416 static void write_tag_body(int fd, const unsigned char *sha1)
417 {
418         unsigned long size;
419         enum object_type type;
420         char *buf, *sp;
421
422         buf = read_sha1_file(sha1, &type, &size);
423         if (!buf)
424                 return;
425         /* skip header */
426         sp = strstr(buf, "\n\n");
427
428         if (!sp || !size || type != OBJ_TAG) {
429                 free(buf);
430                 return;
431         }
432         sp += 2; /* skip the 2 LFs */
433         write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
434
435         free(buf);
436 }
437
438 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
439 {
440         if (sign && do_sign(buf) < 0)
441                 return error(_("unable to sign the tag"));
442         if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
443                 return error(_("unable to write tag file"));
444         return 0;
445 }
446
447 struct create_tag_options {
448         unsigned int message_given:1;
449         unsigned int sign;
450         enum {
451                 CLEANUP_NONE,
452                 CLEANUP_SPACE,
453                 CLEANUP_ALL
454         } cleanup_mode;
455 };
456
457 static void create_tag(const unsigned char *object, const char *tag,
458                        struct strbuf *buf, struct create_tag_options *opt,
459                        unsigned char *prev, unsigned char *result)
460 {
461         enum object_type type;
462         char header_buf[1024];
463         int header_len;
464         char *path = NULL;
465
466         type = sha1_object_info(object, NULL);
467         if (type <= OBJ_NONE)
468             die(_("bad object type."));
469
470         header_len = snprintf(header_buf, sizeof(header_buf),
471                           "object %s\n"
472                           "type %s\n"
473                           "tag %s\n"
474                           "tagger %s\n\n",
475                           sha1_to_hex(object),
476                           typename(type),
477                           tag,
478                           git_committer_info(IDENT_STRICT));
479
480         if (header_len > sizeof(header_buf) - 1)
481                 die(_("tag header too big."));
482
483         if (!opt->message_given) {
484                 int fd;
485
486                 /* write the template message before editing: */
487                 path = git_pathdup("TAG_EDITMSG");
488                 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
489                 if (fd < 0)
490                         die_errno(_("could not create file '%s'"), path);
491
492                 if (!is_null_sha1(prev)) {
493                         write_tag_body(fd, prev);
494                 } else {
495                         struct strbuf buf = STRBUF_INIT;
496                         strbuf_addch(&buf, '\n');
497                         if (opt->cleanup_mode == CLEANUP_ALL)
498                                 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
499                         else
500                                 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
501                         write_or_die(fd, buf.buf, buf.len);
502                         strbuf_release(&buf);
503                 }
504                 close(fd);
505
506                 if (launch_editor(path, buf, NULL)) {
507                         fprintf(stderr,
508                         _("Please supply the message using either -m or -F option.\n"));
509                         exit(1);
510                 }
511         }
512
513         if (opt->cleanup_mode != CLEANUP_NONE)
514                 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
515
516         if (!opt->message_given && !buf->len)
517                 die(_("no tag message?"));
518
519         strbuf_insert(buf, 0, header_buf, header_len);
520
521         if (build_tag_object(buf, opt->sign, result) < 0) {
522                 if (path)
523                         fprintf(stderr, _("The tag message has been left in %s\n"),
524                                 path);
525                 exit(128);
526         }
527         if (path) {
528                 unlink_or_warn(path);
529                 free(path);
530         }
531 }
532
533 struct msg_arg {
534         int given;
535         struct strbuf buf;
536 };
537
538 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
539 {
540         struct msg_arg *msg = opt->value;
541
542         if (!arg)
543                 return -1;
544         if (msg->buf.len)
545                 strbuf_addstr(&(msg->buf), "\n\n");
546         strbuf_addstr(&(msg->buf), arg);
547         msg->given = 1;
548         return 0;
549 }
550
551 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
552 {
553         if (name[0] == '-')
554                 return -1;
555
556         strbuf_reset(sb);
557         strbuf_addf(sb, "refs/tags/%s", name);
558
559         return check_refname_format(sb->buf, 0);
560 }
561
562 static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
563 {
564         int *sort = opt->value;
565
566         return parse_sort_string(NULL, arg, sort);
567 }
568
569 int cmd_tag(int argc, const char **argv, const char *prefix)
570 {
571         struct strbuf buf = STRBUF_INIT;
572         struct strbuf ref = STRBUF_INIT;
573         unsigned char object[20], prev[20];
574         const char *object_ref, *tag;
575         struct create_tag_options opt;
576         char *cleanup_arg = NULL;
577         int annotate = 0, force = 0, lines = -1;
578         int create_reflog = 0;
579         int cmdmode = 0;
580         const char *msgfile = NULL, *keyid = NULL;
581         struct msg_arg msg = { 0, STRBUF_INIT };
582         struct commit_list *with_commit = NULL;
583         struct ref_transaction *transaction;
584         struct strbuf err = STRBUF_INIT;
585         struct option options[] = {
586                 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
587                 { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
588                                 N_("print <n> lines of each tag message"),
589                                 PARSE_OPT_OPTARG, NULL, 1 },
590                 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
591                 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
592
593                 OPT_GROUP(N_("Tag creation options")),
594                 OPT_BOOL('a', "annotate", &annotate,
595                                         N_("annotated tag, needs a message")),
596                 OPT_CALLBACK('m', "message", &msg, N_("message"),
597                              N_("tag message"), parse_msg_arg),
598                 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
599                 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
600                 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
601                         N_("how to strip spaces and #comments from message")),
602                 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
603                                         N_("use another key to sign the tag")),
604                 OPT__FORCE(&force, N_("replace the tag if exists")),
605                 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create_reflog")),
606
607                 OPT_GROUP(N_("Tag listing options")),
608                 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
609                 OPT_CONTAINS(&with_commit, N_("print only tags that contain the commit")),
610                 OPT_WITH(&with_commit, N_("print only tags that contain the commit")),
611                 {
612                         OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
613                         PARSE_OPT_NONEG, parse_opt_sort
614                 },
615                 {
616                         OPTION_CALLBACK, 0, "points-at", &points_at, N_("object"),
617                         N_("print only tags of the object"), 0, parse_opt_object_name
618                 },
619                 OPT_END()
620         };
621
622         git_config(git_tag_config, NULL);
623
624         memset(&opt, 0, sizeof(opt));
625
626         argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
627
628         if (keyid) {
629                 opt.sign = 1;
630                 set_signing_key(keyid);
631         }
632         if (opt.sign)
633                 annotate = 1;
634         if (argc == 0 && !cmdmode)
635                 cmdmode = 'l';
636
637         if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
638                 usage_with_options(git_tag_usage, options);
639
640         finalize_colopts(&colopts, -1);
641         if (cmdmode == 'l' && lines != -1) {
642                 if (explicitly_enable_column(colopts))
643                         die(_("--column and -n are incompatible"));
644                 colopts = 0;
645         }
646         if (cmdmode == 'l') {
647                 int ret;
648                 if (column_active(colopts)) {
649                         struct column_options copts;
650                         memset(&copts, 0, sizeof(copts));
651                         copts.padding = 2;
652                         run_column_filter(colopts, &copts);
653                 }
654                 if (lines != -1 && tag_sort)
655                         die(_("--sort and -n are incompatible"));
656                 ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
657                 if (column_active(colopts))
658                         stop_column_filter();
659                 return ret;
660         }
661         if (lines != -1)
662                 die(_("-n option is only allowed with -l."));
663         if (with_commit)
664                 die(_("--contains option is only allowed with -l."));
665         if (points_at.nr)
666                 die(_("--points-at option is only allowed with -l."));
667         if (cmdmode == 'd')
668                 return for_each_tag_name(argv, delete_tag);
669         if (cmdmode == 'v')
670                 return for_each_tag_name(argv, verify_tag);
671
672         if (msg.given || msgfile) {
673                 if (msg.given && msgfile)
674                         die(_("only one -F or -m option is allowed."));
675                 annotate = 1;
676                 if (msg.given)
677                         strbuf_addbuf(&buf, &(msg.buf));
678                 else {
679                         if (!strcmp(msgfile, "-")) {
680                                 if (strbuf_read(&buf, 0, 1024) < 0)
681                                         die_errno(_("cannot read '%s'"), msgfile);
682                         } else {
683                                 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
684                                         die_errno(_("could not open or read '%s'"),
685                                                 msgfile);
686                         }
687                 }
688         }
689
690         tag = argv[0];
691
692         object_ref = argc == 2 ? argv[1] : "HEAD";
693         if (argc > 2)
694                 die(_("too many params"));
695
696         if (get_sha1(object_ref, object))
697                 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
698
699         if (strbuf_check_tag_ref(&ref, tag))
700                 die(_("'%s' is not a valid tag name."), tag);
701
702         if (read_ref(ref.buf, prev))
703                 hashclr(prev);
704         else if (!force)
705                 die(_("tag '%s' already exists"), tag);
706
707         opt.message_given = msg.given || msgfile;
708
709         if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
710                 opt.cleanup_mode = CLEANUP_ALL;
711         else if (!strcmp(cleanup_arg, "verbatim"))
712                 opt.cleanup_mode = CLEANUP_NONE;
713         else if (!strcmp(cleanup_arg, "whitespace"))
714                 opt.cleanup_mode = CLEANUP_SPACE;
715         else
716                 die(_("Invalid cleanup mode %s"), cleanup_arg);
717
718         if (annotate)
719                 create_tag(object, tag, &buf, &opt, prev, object);
720
721         transaction = ref_transaction_begin(&err);
722         if (!transaction ||
723             ref_transaction_update(transaction, ref.buf, object, prev,
724                                    create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
725                                    NULL, &err) ||
726             ref_transaction_commit(transaction, &err))
727                 die("%s", err.buf);
728         ref_transaction_free(transaction);
729         if (force && !is_null_sha1(prev) && hashcmp(prev, object))
730                 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
731
732         strbuf_release(&err);
733         strbuf_release(&buf);
734         strbuf_release(&ref);
735         return 0;
736 }