builtin / tag.con commit Merge branch 'jc/pull-signed-tag' (eb8aa3d)
   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
  19static const char * const git_tag_usage[] = {
  20        "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
  21        "git tag -d <tagname>...",
  22        "git tag -l [-n[<num>]] [<pattern>...]",
  23        "git tag -v <tagname>...",
  24        NULL
  25};
  26
  27struct tag_filter {
  28        const char **patterns;
  29        int lines;
  30        struct commit_list *with_commit;
  31};
  32
  33static int match_pattern(const char **patterns, const char *ref)
  34{
  35        /* no pattern means match everything */
  36        if (!*patterns)
  37                return 1;
  38        for (; *patterns; patterns++)
  39                if (!fnmatch(*patterns, ref, 0))
  40                        return 1;
  41        return 0;
  42}
  43
  44static int in_commit_list(const struct commit_list *want, struct commit *c)
  45{
  46        for (; want; want = want->next)
  47                if (!hashcmp(want->item->object.sha1, c->object.sha1))
  48                        return 1;
  49        return 0;
  50}
  51
  52static int contains_recurse(struct commit *candidate,
  53                            const struct commit_list *want)
  54{
  55        struct commit_list *p;
  56
  57        /* was it previously marked as containing a want commit? */
  58        if (candidate->object.flags & TMP_MARK)
  59                return 1;
  60        /* or marked as not possibly containing a want commit? */
  61        if (candidate->object.flags & UNINTERESTING)
  62                return 0;
  63        /* or are we it? */
  64        if (in_commit_list(want, candidate))
  65                return 1;
  66
  67        if (parse_commit(candidate) < 0)
  68                return 0;
  69
  70        /* Otherwise recurse and mark ourselves for future traversals. */
  71        for (p = candidate->parents; p; p = p->next) {
  72                if (contains_recurse(p->item, want)) {
  73                        candidate->object.flags |= TMP_MARK;
  74                        return 1;
  75                }
  76        }
  77        candidate->object.flags |= UNINTERESTING;
  78        return 0;
  79}
  80
  81static int contains(struct commit *candidate, const struct commit_list *want)
  82{
  83        return contains_recurse(candidate, want);
  84}
  85
  86static int show_reference(const char *refname, const unsigned char *sha1,
  87                          int flag, void *cb_data)
  88{
  89        struct tag_filter *filter = cb_data;
  90
  91        if (match_pattern(filter->patterns, refname)) {
  92                int i;
  93                unsigned long size;
  94                enum object_type type;
  95                char *buf, *sp, *eol;
  96                size_t len;
  97
  98                if (filter->with_commit) {
  99                        struct commit *commit;
 100
 101                        commit = lookup_commit_reference_gently(sha1, 1);
 102                        if (!commit)
 103                                return 0;
 104                        if (!contains(commit, filter->with_commit))
 105                                return 0;
 106                }
 107
 108                if (!filter->lines) {
 109                        printf("%s\n", refname);
 110                        return 0;
 111                }
 112                printf("%-15s ", refname);
 113
 114                buf = read_sha1_file(sha1, &type, &size);
 115                if (!buf || !size)
 116                        return 0;
 117
 118                /* skip header */
 119                sp = strstr(buf, "\n\n");
 120                if (!sp) {
 121                        free(buf);
 122                        return 0;
 123                }
 124                /* only take up to "lines" lines, and strip the signature */
 125                size = parse_signature(buf, size);
 126                for (i = 0, sp += 2;
 127                                i < filter->lines && sp < buf + size;
 128                                i++) {
 129                        if (i)
 130                                printf("\n    ");
 131                        eol = memchr(sp, '\n', size - (sp - buf));
 132                        len = eol ? eol - sp : size - (sp - buf);
 133                        fwrite(sp, len, 1, stdout);
 134                        if (!eol)
 135                                break;
 136                        sp = eol + 1;
 137                }
 138                putchar('\n');
 139                free(buf);
 140        }
 141
 142        return 0;
 143}
 144
 145static int list_tags(const char **patterns, int lines,
 146                        struct commit_list *with_commit)
 147{
 148        struct tag_filter filter;
 149
 150        filter.patterns = patterns;
 151        filter.lines = lines;
 152        filter.with_commit = with_commit;
 153
 154        for_each_tag_ref(show_reference, (void *) &filter);
 155
 156        return 0;
 157}
 158
 159typedef int (*each_tag_name_fn)(const char *name, const char *ref,
 160                                const unsigned char *sha1);
 161
 162static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
 163{
 164        const char **p;
 165        char ref[PATH_MAX];
 166        int had_error = 0;
 167        unsigned char sha1[20];
 168
 169        for (p = argv; *p; p++) {
 170                if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
 171                                        >= sizeof(ref)) {
 172                        error(_("tag name too long: %.*s..."), 50, *p);
 173                        had_error = 1;
 174                        continue;
 175                }
 176                if (!resolve_ref(ref, sha1, 1, NULL)) {
 177                        error(_("tag '%s' not found."), *p);
 178                        had_error = 1;
 179                        continue;
 180                }
 181                if (fn(*p, ref, sha1))
 182                        had_error = 1;
 183        }
 184        return had_error;
 185}
 186
 187static int delete_tag(const char *name, const char *ref,
 188                                const unsigned char *sha1)
 189{
 190        if (delete_ref(ref, sha1, 0))
 191                return 1;
 192        printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
 193        return 0;
 194}
 195
 196static int verify_tag(const char *name, const char *ref,
 197                                const unsigned char *sha1)
 198{
 199        const char *argv_verify_tag[] = {"verify-tag",
 200                                        "-v", "SHA1_HEX", NULL};
 201        argv_verify_tag[2] = sha1_to_hex(sha1);
 202
 203        if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
 204                return error(_("could not verify the tag '%s'"), name);
 205        return 0;
 206}
 207
 208static int do_sign(struct strbuf *buffer)
 209{
 210        return sign_buffer(buffer, buffer, get_signing_key());
 211}
 212
 213static const char tag_template[] =
 214        N_("\n"
 215        "#\n"
 216        "# Write a tag message\n"
 217        "#\n");
 218
 219static int git_tag_config(const char *var, const char *value, void *cb)
 220{
 221        int status = git_gpg_config(var, value, cb);
 222        if (status)
 223                return status;
 224        return git_default_config(var, value, cb);
 225}
 226
 227static void write_tag_body(int fd, const unsigned char *sha1)
 228{
 229        unsigned long size;
 230        enum object_type type;
 231        char *buf, *sp;
 232
 233        buf = read_sha1_file(sha1, &type, &size);
 234        if (!buf)
 235                return;
 236        /* skip header */
 237        sp = strstr(buf, "\n\n");
 238
 239        if (!sp || !size || type != OBJ_TAG) {
 240                free(buf);
 241                return;
 242        }
 243        sp += 2; /* skip the 2 LFs */
 244        write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
 245
 246        free(buf);
 247}
 248
 249static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
 250{
 251        if (sign && do_sign(buf) < 0)
 252                return error(_("unable to sign the tag"));
 253        if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
 254                return error(_("unable to write tag file"));
 255        return 0;
 256}
 257
 258static void create_tag(const unsigned char *object, const char *tag,
 259                       struct strbuf *buf, int message, int sign,
 260                       unsigned char *prev, unsigned char *result)
 261{
 262        enum object_type type;
 263        char header_buf[1024];
 264        int header_len;
 265        char *path = NULL;
 266
 267        type = sha1_object_info(object, NULL);
 268        if (type <= OBJ_NONE)
 269            die(_("bad object type."));
 270
 271        header_len = snprintf(header_buf, sizeof(header_buf),
 272                          "object %s\n"
 273                          "type %s\n"
 274                          "tag %s\n"
 275                          "tagger %s\n\n",
 276                          sha1_to_hex(object),
 277                          typename(type),
 278                          tag,
 279                          git_committer_info(IDENT_ERROR_ON_NO_NAME));
 280
 281        if (header_len > sizeof(header_buf) - 1)
 282                die(_("tag header too big."));
 283
 284        if (!message) {
 285                int fd;
 286
 287                /* write the template message before editing: */
 288                path = git_pathdup("TAG_EDITMSG");
 289                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 290                if (fd < 0)
 291                        die_errno(_("could not create file '%s'"), path);
 292
 293                if (!is_null_sha1(prev))
 294                        write_tag_body(fd, prev);
 295                else
 296                        write_or_die(fd, _(tag_template), strlen(_(tag_template)));
 297                close(fd);
 298
 299                if (launch_editor(path, buf, NULL)) {
 300                        fprintf(stderr,
 301                        _("Please supply the message using either -m or -F option.\n"));
 302                        exit(1);
 303                }
 304        }
 305
 306        stripspace(buf, 1);
 307
 308        if (!message && !buf->len)
 309                die(_("no tag message?"));
 310
 311        strbuf_insert(buf, 0, header_buf, header_len);
 312
 313        if (build_tag_object(buf, sign, result) < 0) {
 314                if (path)
 315                        fprintf(stderr, _("The tag message has been left in %s\n"),
 316                                path);
 317                exit(128);
 318        }
 319        if (path) {
 320                unlink_or_warn(path);
 321                free(path);
 322        }
 323}
 324
 325struct msg_arg {
 326        int given;
 327        struct strbuf buf;
 328};
 329
 330static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 331{
 332        struct msg_arg *msg = opt->value;
 333
 334        if (!arg)
 335                return -1;
 336        if (msg->buf.len)
 337                strbuf_addstr(&(msg->buf), "\n\n");
 338        strbuf_addstr(&(msg->buf), arg);
 339        msg->given = 1;
 340        return 0;
 341}
 342
 343static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
 344{
 345        if (name[0] == '-')
 346                return -1;
 347
 348        strbuf_reset(sb);
 349        strbuf_addf(sb, "refs/tags/%s", name);
 350
 351        return check_refname_format(sb->buf, 0);
 352}
 353
 354int cmd_tag(int argc, const char **argv, const char *prefix)
 355{
 356        struct strbuf buf = STRBUF_INIT;
 357        struct strbuf ref = STRBUF_INIT;
 358        unsigned char object[20], prev[20];
 359        const char *object_ref, *tag;
 360        struct ref_lock *lock;
 361
 362        int annotate = 0, sign = 0, force = 0, lines = -1,
 363                list = 0, delete = 0, verify = 0;
 364        const char *msgfile = NULL, *keyid = NULL;
 365        struct msg_arg msg = { 0, STRBUF_INIT };
 366        struct commit_list *with_commit = NULL;
 367        struct option options[] = {
 368                OPT_BOOLEAN('l', "list", &list, "list tag names"),
 369                { OPTION_INTEGER, 'n', NULL, &lines, "n",
 370                                "print <n> lines of each tag message",
 371                                PARSE_OPT_OPTARG, NULL, 1 },
 372                OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
 373                OPT_BOOLEAN('v', "verify", &verify, "verify tags"),
 374
 375                OPT_GROUP("Tag creation options"),
 376                OPT_BOOLEAN('a', "annotate", &annotate,
 377                                        "annotated tag, needs a message"),
 378                OPT_CALLBACK('m', "message", &msg, "message",
 379                             "tag message", parse_msg_arg),
 380                OPT_FILENAME('F', "file", &msgfile, "read message from file"),
 381                OPT_BOOLEAN('s', "sign", &sign, "annotated and GPG-signed tag"),
 382                OPT_STRING('u', "local-user", &keyid, "key-id",
 383                                        "use another key to sign the tag"),
 384                OPT__FORCE(&force, "replace the tag if exists"),
 385
 386                OPT_GROUP("Tag listing options"),
 387                {
 388                        OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
 389                        "print only tags that contain the commit",
 390                        PARSE_OPT_LASTARG_DEFAULT,
 391                        parse_opt_with_commit, (intptr_t)"HEAD",
 392                },
 393                OPT_END()
 394        };
 395
 396        git_config(git_tag_config, NULL);
 397
 398        argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
 399
 400        if (keyid) {
 401                sign = 1;
 402                set_signing_key(keyid);
 403        }
 404        if (sign)
 405                annotate = 1;
 406        if (argc == 0 && !(delete || verify))
 407                list = 1;
 408
 409        if ((annotate || msg.given || msgfile || force) &&
 410            (list || delete || verify))
 411                usage_with_options(git_tag_usage, options);
 412
 413        if (list + delete + verify > 1)
 414                usage_with_options(git_tag_usage, options);
 415        if (list)
 416                return list_tags(argv, lines == -1 ? 0 : lines,
 417                                 with_commit);
 418        if (lines != -1)
 419                die(_("-n option is only allowed with -l."));
 420        if (with_commit)
 421                die(_("--contains option is only allowed with -l."));
 422        if (delete)
 423                return for_each_tag_name(argv, delete_tag);
 424        if (verify)
 425                return for_each_tag_name(argv, verify_tag);
 426
 427        if (msg.given || msgfile) {
 428                if (msg.given && msgfile)
 429                        die(_("only one -F or -m option is allowed."));
 430                annotate = 1;
 431                if (msg.given)
 432                        strbuf_addbuf(&buf, &(msg.buf));
 433                else {
 434                        if (!strcmp(msgfile, "-")) {
 435                                if (strbuf_read(&buf, 0, 1024) < 0)
 436                                        die_errno(_("cannot read '%s'"), msgfile);
 437                        } else {
 438                                if (strbuf_read_file(&buf, msgfile, 1024) < 0)
 439                                        die_errno(_("could not open or read '%s'"),
 440                                                msgfile);
 441                        }
 442                }
 443        }
 444
 445        tag = argv[0];
 446
 447        object_ref = argc == 2 ? argv[1] : "HEAD";
 448        if (argc > 2)
 449                die(_("too many params"));
 450
 451        if (get_sha1(object_ref, object))
 452                die(_("Failed to resolve '%s' as a valid ref."), object_ref);
 453
 454        if (strbuf_check_tag_ref(&ref, tag))
 455                die(_("'%s' is not a valid tag name."), tag);
 456
 457        if (!resolve_ref(ref.buf, prev, 1, NULL))
 458                hashclr(prev);
 459        else if (!force)
 460                die(_("tag '%s' already exists"), tag);
 461
 462        if (annotate)
 463                create_tag(object, tag, &buf, msg.given || msgfile,
 464                           sign, prev, object);
 465
 466        lock = lock_any_ref_for_update(ref.buf, prev, 0);
 467        if (!lock)
 468                die(_("%s: cannot lock the ref"), ref.buf);
 469        if (write_ref_sha1(lock, object, NULL) < 0)
 470                die(_("%s: cannot update the ref"), ref.buf);
 471        if (force && hashcmp(prev, object))
 472                printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
 473
 474        strbuf_release(&buf);
 475        strbuf_release(&ref);
 476        return 0;
 477}