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