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