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