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