builtin-tag.con commit rev-parse: fix meaning of rev~ vs rev~0. (621ff67)
   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
  16static const char * const git_tag_usage[] = {
  17        "git-tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
  18        "git-tag -d <tagname>...",
  19        "git-tag -l [-n [<num>]] [<pattern>]",
  20        "git-tag -v <tagname>...",
  21        NULL
  22};
  23
  24static char signingkey[1000];
  25
  26void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
  27{
  28        const char *editor, *terminal;
  29
  30        editor = getenv("GIT_EDITOR");
  31        if (!editor && editor_program)
  32                editor = editor_program;
  33        if (!editor)
  34                editor = getenv("VISUAL");
  35        if (!editor)
  36                editor = getenv("EDITOR");
  37
  38        terminal = getenv("TERM");
  39        if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
  40                fprintf(stderr,
  41                "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
  42                "Please supply the message using either -m or -F option.\n");
  43                exit(1);
  44        }
  45
  46        if (!editor)
  47                editor = "vi";
  48
  49        if (strcmp(editor, ":")) {
  50                size_t len = strlen(editor);
  51                int i = 0;
  52                const char *args[6];
  53                struct strbuf arg0;
  54
  55                strbuf_init(&arg0, 0);
  56                if (strcspn(editor, "$ \t'") != len) {
  57                        /* there are specials */
  58                        strbuf_addf(&arg0, "%s \"$@\"", editor);
  59                        args[i++] = "sh";
  60                        args[i++] = "-c";
  61                        args[i++] = arg0.buf;
  62                }
  63                args[i++] = editor;
  64                args[i++] = path;
  65                args[i] = NULL;
  66
  67                if (run_command_v_opt_cd_env(args, 0, NULL, env))
  68                        die("There was a problem with the editor %s.", editor);
  69                strbuf_release(&arg0);
  70        }
  71
  72        if (!buffer)
  73                return;
  74        if (strbuf_read_file(buffer, path, 0) < 0)
  75                die("could not read message file '%s': %s",
  76                    path, strerror(errno));
  77}
  78
  79struct tag_filter {
  80        const char *pattern;
  81        int lines;
  82};
  83
  84#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
  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 (!fnmatch(filter->pattern, refname, 0)) {
  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->lines) {
  99                        printf("%s\n", refname);
 100                        return 0;
 101                }
 102                printf("%-15s ", refname);
 103
 104                buf = read_sha1_file(sha1, &type, &size);
 105                if (!buf || !size)
 106                        return 0;
 107
 108                /* skip header */
 109                sp = strstr(buf, "\n\n");
 110                if (!sp) {
 111                        free(buf);
 112                        return 0;
 113                }
 114                /* only take up to "lines" lines, and strip the signature */
 115                for (i = 0, sp += 2;
 116                                i < filter->lines && sp < buf + size &&
 117                                prefixcmp(sp, PGP_SIGNATURE "\n");
 118                                i++) {
 119                        if (i)
 120                                printf("\n    ");
 121                        eol = memchr(sp, '\n', size - (sp - buf));
 122                        len = eol ? eol - sp : size - (sp - buf);
 123                        fwrite(sp, len, 1, stdout);
 124                        if (!eol)
 125                                break;
 126                        sp = eol + 1;
 127                }
 128                putchar('\n');
 129                free(buf);
 130        }
 131
 132        return 0;
 133}
 134
 135static int list_tags(const char *pattern, int lines)
 136{
 137        struct tag_filter filter;
 138
 139        if (pattern == NULL)
 140                pattern = "*";
 141
 142        filter.pattern = pattern;
 143        filter.lines = lines;
 144
 145        for_each_tag_ref(show_reference, (void *) &filter);
 146
 147        return 0;
 148}
 149
 150typedef int (*each_tag_name_fn)(const char *name, const char *ref,
 151                                const unsigned char *sha1);
 152
 153static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
 154{
 155        const char **p;
 156        char ref[PATH_MAX];
 157        int had_error = 0;
 158        unsigned char sha1[20];
 159
 160        for (p = argv; *p; p++) {
 161                if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
 162                                        >= sizeof(ref)) {
 163                        error("tag name too long: %.*s...", 50, *p);
 164                        had_error = 1;
 165                        continue;
 166                }
 167                if (!resolve_ref(ref, sha1, 1, NULL)) {
 168                        error("tag '%s' not found.", *p);
 169                        had_error = 1;
 170                        continue;
 171                }
 172                if (fn(*p, ref, sha1))
 173                        had_error = 1;
 174        }
 175        return had_error;
 176}
 177
 178static int delete_tag(const char *name, const char *ref,
 179                                const unsigned char *sha1)
 180{
 181        if (delete_ref(ref, sha1))
 182                return 1;
 183        printf("Deleted tag '%s'\n", name);
 184        return 0;
 185}
 186
 187static int verify_tag(const char *name, const char *ref,
 188                                const unsigned char *sha1)
 189{
 190        const char *argv_verify_tag[] = {"git-verify-tag",
 191                                        "-v", "SHA1_HEX", NULL};
 192        argv_verify_tag[2] = sha1_to_hex(sha1);
 193
 194        if (run_command_v_opt(argv_verify_tag, 0))
 195                return error("could not verify the tag '%s'", name);
 196        return 0;
 197}
 198
 199static int do_sign(struct strbuf *buffer)
 200{
 201        struct child_process gpg;
 202        const char *args[4];
 203        char *bracket;
 204        int len;
 205
 206        if (!*signingkey) {
 207                if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
 208                                sizeof(signingkey)) > sizeof(signingkey) - 1)
 209                        return error("committer info too long.");
 210                bracket = strchr(signingkey, '>');
 211                if (bracket)
 212                        bracket[1] = '\0';
 213        }
 214
 215        /* When the username signingkey is bad, program could be terminated
 216         * because gpg exits without reading and then write gets SIGPIPE. */
 217        signal(SIGPIPE, SIG_IGN);
 218
 219        memset(&gpg, 0, sizeof(gpg));
 220        gpg.argv = args;
 221        gpg.in = -1;
 222        gpg.out = -1;
 223        args[0] = "gpg";
 224        args[1] = "-bsau";
 225        args[2] = signingkey;
 226        args[3] = NULL;
 227
 228        if (start_command(&gpg))
 229                return error("could not run gpg.");
 230
 231        if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
 232                close(gpg.in);
 233                finish_command(&gpg);
 234                return error("gpg did not accept the tag data");
 235        }
 236        close(gpg.in);
 237        gpg.close_in = 0;
 238        len = strbuf_read(buffer, gpg.out, 1024);
 239
 240        if (finish_command(&gpg) || !len || len < 0)
 241                return error("gpg failed to sign the tag");
 242
 243        if (len < 0)
 244                return error("could not read the entire signature from gpg.");
 245
 246        return 0;
 247}
 248
 249static const char tag_template[] =
 250        "\n"
 251        "#\n"
 252        "# Write a tag message\n"
 253        "#\n";
 254
 255static void set_signingkey(const char *value)
 256{
 257        if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
 258                die("signing key value too long (%.10s...)", value);
 259}
 260
 261static int git_tag_config(const char *var, const char *value)
 262{
 263        if (!strcmp(var, "user.signingkey")) {
 264                if (!value)
 265                        return config_error_nonbool(value);
 266                set_signingkey(value);
 267                return 0;
 268        }
 269
 270        return git_default_config(var, value);
 271}
 272
 273static void write_tag_body(int fd, const unsigned char *sha1)
 274{
 275        unsigned long size;
 276        enum object_type type;
 277        char *buf, *sp, *eob;
 278        size_t len;
 279
 280        buf = read_sha1_file(sha1, &type, &size);
 281        if (!buf)
 282                return;
 283        /* skip header */
 284        sp = strstr(buf, "\n\n");
 285
 286        if (!sp || !size || type != OBJ_TAG) {
 287                free(buf);
 288                return;
 289        }
 290        sp += 2; /* skip the 2 LFs */
 291        eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
 292        if (eob)
 293                len = eob - sp;
 294        else
 295                len = buf + size - sp;
 296        write_or_die(fd, sp, len);
 297
 298        free(buf);
 299}
 300
 301static void create_tag(const unsigned char *object, const char *tag,
 302                       struct strbuf *buf, int message, int sign,
 303                       unsigned char *prev, unsigned char *result)
 304{
 305        enum object_type type;
 306        char header_buf[1024];
 307        int header_len;
 308
 309        type = sha1_object_info(object, NULL);
 310        if (type <= OBJ_NONE)
 311            die("bad object type.");
 312
 313        header_len = snprintf(header_buf, sizeof(header_buf),
 314                          "object %s\n"
 315                          "type %s\n"
 316                          "tag %s\n"
 317                          "tagger %s\n\n",
 318                          sha1_to_hex(object),
 319                          typename(type),
 320                          tag,
 321                          git_committer_info(IDENT_ERROR_ON_NO_NAME));
 322
 323        if (header_len > sizeof(header_buf) - 1)
 324                die("tag header too big.");
 325
 326        if (!message) {
 327                char *path;
 328                int fd;
 329
 330                /* write the template message before editing: */
 331                path = xstrdup(git_path("TAG_EDITMSG"));
 332                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 333                if (fd < 0)
 334                        die("could not create file '%s': %s",
 335                                                path, strerror(errno));
 336
 337                if (!is_null_sha1(prev))
 338                        write_tag_body(fd, prev);
 339                else
 340                        write_or_die(fd, tag_template, strlen(tag_template));
 341                close(fd);
 342
 343                launch_editor(path, buf, NULL);
 344
 345                unlink(path);
 346                free(path);
 347        }
 348
 349        stripspace(buf, 1);
 350
 351        if (!message && !buf->len)
 352                die("no tag message?");
 353
 354        strbuf_insert(buf, 0, header_buf, header_len);
 355
 356        if (sign && do_sign(buf) < 0)
 357                die("unable to sign the tag");
 358        if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
 359                die("unable to write tag file");
 360}
 361
 362struct msg_arg {
 363        int given;
 364        struct strbuf buf;
 365};
 366
 367static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 368{
 369        struct msg_arg *msg = opt->value;
 370
 371        if (!arg)
 372                return -1;
 373        if (msg->buf.len)
 374                strbuf_addstr(&(msg->buf), "\n\n");
 375        strbuf_addstr(&(msg->buf), arg);
 376        msg->given = 1;
 377        return 0;
 378}
 379
 380int cmd_tag(int argc, const char **argv, const char *prefix)
 381{
 382        struct strbuf buf;
 383        unsigned char object[20], prev[20];
 384        char ref[PATH_MAX];
 385        const char *object_ref, *tag;
 386        struct ref_lock *lock;
 387
 388        int annotate = 0, sign = 0, force = 0, lines = 0,
 389                list = 0, delete = 0, verify = 0;
 390        char *msgfile = NULL, *keyid = NULL;
 391        struct msg_arg msg = { 0, STRBUF_INIT };
 392        struct option options[] = {
 393                OPT_BOOLEAN('l', NULL, &list, "list tag names"),
 394                { OPTION_INTEGER, 'n', NULL, &lines, NULL,
 395                                "print n lines of each tag message",
 396                                PARSE_OPT_OPTARG, NULL, 1 },
 397                OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
 398                OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
 399
 400                OPT_GROUP("Tag creation options"),
 401                OPT_BOOLEAN('a', NULL, &annotate,
 402                                        "annotated tag, needs a message"),
 403                OPT_CALLBACK('m', NULL, &msg, "msg",
 404                             "message for the tag", parse_msg_arg),
 405                OPT_STRING('F', NULL, &msgfile, "file", "message in a file"),
 406                OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
 407                OPT_STRING('u', NULL, &keyid, "key-id",
 408                                        "use another key to sign the tag"),
 409                OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"),
 410                OPT_END()
 411        };
 412
 413        git_config(git_tag_config);
 414
 415        argc = parse_options(argc, argv, options, git_tag_usage, 0);
 416
 417        if (keyid) {
 418                sign = 1;
 419                set_signingkey(keyid);
 420        }
 421        if (sign)
 422                annotate = 1;
 423
 424        if (list)
 425                return list_tags(argv[0], lines);
 426        if (delete)
 427                return for_each_tag_name(argv, delete_tag);
 428        if (verify)
 429                return for_each_tag_name(argv, verify_tag);
 430
 431        strbuf_init(&buf, 0);
 432        if (msg.given || msgfile) {
 433                if (msg.given && msgfile)
 434                        die("only one -F or -m option is allowed.");
 435                annotate = 1;
 436                if (msg.given)
 437                        strbuf_addbuf(&buf, &(msg.buf));
 438                else {
 439                        if (!strcmp(msgfile, "-")) {
 440                                if (strbuf_read(&buf, 0, 1024) < 0)
 441                                        die("cannot read %s", msgfile);
 442                        } else {
 443                                if (strbuf_read_file(&buf, msgfile, 1024) < 0)
 444                                        die("could not open or read '%s': %s",
 445                                                msgfile, strerror(errno));
 446                        }
 447                }
 448        }
 449
 450        if (argc == 0) {
 451                if (annotate)
 452                        usage_with_options(git_tag_usage, options);
 453                return list_tags(NULL, lines);
 454        }
 455        tag = argv[0];
 456
 457        object_ref = argc == 2 ? argv[1] : "HEAD";
 458        if (argc > 2)
 459                die("too many params");
 460
 461        if (get_sha1(object_ref, object))
 462                die("Failed to resolve '%s' as a valid ref.", object_ref);
 463
 464        if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
 465                die("tag name too long: %.*s...", 50, tag);
 466        if (check_ref_format(ref))
 467                die("'%s' is not a valid tag name.", tag);
 468
 469        if (!resolve_ref(ref, prev, 1, NULL))
 470                hashclr(prev);
 471        else if (!force)
 472                die("tag '%s' already exists", tag);
 473
 474        if (annotate)
 475                create_tag(object, tag, &buf, msg.given || msgfile,
 476                           sign, prev, object);
 477
 478        lock = lock_any_ref_for_update(ref, prev, 0);
 479        if (!lock)
 480                die("%s: cannot lock the ref", ref);
 481        if (write_ref_sha1(lock, object, NULL) < 0)
 482                die("%s: cannot update the ref", ref);
 483
 484        strbuf_release(&buf);
 485        return 0;
 486}