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