builtin-tag.con commit user-manual: failed push to public repository (81eb417)
   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
 127        if (pattern == NULL)
 128                pattern = "*";
 129
 130        filter.pattern = pattern;
 131        filter.lines = lines;
 132
 133        for_each_tag_ref(show_reference, (void *) &filter);
 134
 135        return 0;
 136}
 137
 138typedef int (*each_tag_name_fn)(const char *name, const char *ref,
 139                                const unsigned char *sha1);
 140
 141static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
 142{
 143        const char **p;
 144        char ref[PATH_MAX];
 145        int had_error = 0;
 146        unsigned char sha1[20];
 147
 148        for (p = argv; *p; p++) {
 149                if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
 150                                        >= sizeof(ref)) {
 151                        error("tag name too long: %.*s...", 50, *p);
 152                        had_error = 1;
 153                        continue;
 154                }
 155                if (!resolve_ref(ref, sha1, 1, NULL)) {
 156                        error("tag '%s' not found.", *p);
 157                        had_error = 1;
 158                        continue;
 159                }
 160                if (fn(*p, ref, sha1))
 161                        had_error = 1;
 162        }
 163        return had_error;
 164}
 165
 166static int delete_tag(const char *name, const char *ref,
 167                                const unsigned char *sha1)
 168{
 169        if (delete_ref(ref, sha1))
 170                return 1;
 171        printf("Deleted tag '%s'\n", name);
 172        return 0;
 173}
 174
 175static int verify_tag(const char *name, const char *ref,
 176                                const unsigned char *sha1)
 177{
 178        const char *argv_verify_tag[] = {"git-verify-tag",
 179                                        "-v", "SHA1_HEX", NULL};
 180        argv_verify_tag[2] = sha1_to_hex(sha1);
 181
 182        if (run_command_v_opt(argv_verify_tag, 0))
 183                return error("could not verify the tag '%s'", name);
 184        return 0;
 185}
 186
 187static ssize_t do_sign(char *buffer, size_t size, size_t max)
 188{
 189        struct child_process gpg;
 190        const char *args[4];
 191        char *bracket;
 192        int len;
 193
 194        if (!*signingkey) {
 195                if (strlcpy(signingkey, git_committer_info(1),
 196                                sizeof(signingkey)) > sizeof(signingkey) - 1)
 197                        return error("committer info too long.");
 198                bracket = strchr(signingkey, '>');
 199                if (bracket)
 200                        bracket[1] = '\0';
 201        }
 202
 203        /* When the username signingkey is bad, program could be terminated
 204         * because gpg exits without reading and then write gets SIGPIPE. */
 205        signal(SIGPIPE, SIG_IGN);
 206
 207        memset(&gpg, 0, sizeof(gpg));
 208        gpg.argv = args;
 209        gpg.in = -1;
 210        gpg.out = -1;
 211        args[0] = "gpg";
 212        args[1] = "-bsau";
 213        args[2] = signingkey;
 214        args[3] = NULL;
 215
 216        if (start_command(&gpg))
 217                return error("could not run gpg.");
 218
 219        if (write_in_full(gpg.in, buffer, size) != size) {
 220                close(gpg.in);
 221                finish_command(&gpg);
 222                return error("gpg did not accept the tag data");
 223        }
 224        close(gpg.in);
 225        gpg.close_in = 0;
 226        len = read_in_full(gpg.out, buffer + size, max - size);
 227
 228        if (finish_command(&gpg) || !len || len < 0)
 229                return error("gpg failed to sign the tag");
 230
 231        if (len == max - size)
 232                return error("could not read the entire signature from gpg.");
 233
 234        return size + len;
 235}
 236
 237static const char tag_template[] =
 238        "\n"
 239        "#\n"
 240        "# Write a tag message\n"
 241        "#\n";
 242
 243static int git_tag_config(const char *var, const char *value)
 244{
 245        if (!strcmp(var, "user.signingkey")) {
 246                if (!value)
 247                        die("user.signingkey without value");
 248                if (strlcpy(signingkey, value, sizeof(signingkey))
 249                                                >= sizeof(signingkey))
 250                        die("user.signingkey value too long");
 251                return 0;
 252        }
 253
 254        return git_default_config(var, value);
 255}
 256
 257#define MAX_SIGNATURE_LENGTH 1024
 258/* message must be NULL or allocated, it will be reallocated and freed */
 259static void create_tag(const unsigned char *object, const char *tag,
 260                       char *message, int sign, unsigned char *result)
 261{
 262        enum object_type type;
 263        char header_buf[1024], *buffer = NULL;
 264        int header_len, max_size;
 265        unsigned long size = 0;
 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(1));
 280
 281        if (header_len > sizeof(header_buf) - 1)
 282                die("tag header too big.");
 283
 284        if (!message) {
 285                char *path;
 286                int fd;
 287
 288                /* write the template message before editing: */
 289                path = xstrdup(git_path("TAG_EDITMSG"));
 290                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 291                if (fd < 0)
 292                        die("could not create file '%s': %s",
 293                                                path, strerror(errno));
 294                write_or_die(fd, tag_template, strlen(tag_template));
 295                close(fd);
 296
 297                launch_editor(path, &buffer, &size);
 298
 299                unlink(path);
 300                free(path);
 301        }
 302        else {
 303                buffer = message;
 304                size = strlen(message);
 305        }
 306
 307        size = stripspace(buffer, size, 1);
 308
 309        if (!message && !size)
 310                die("no tag message?");
 311
 312        /* insert the header and add the '\n' if needed: */
 313        max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1;
 314        buffer = xrealloc(buffer, max_size);
 315        if (size)
 316                buffer[size++] = '\n';
 317        memmove(buffer + header_len, buffer, size);
 318        memcpy(buffer, header_buf, header_len);
 319        size += header_len;
 320
 321        if (sign) {
 322                ssize_t r = do_sign(buffer, size, max_size);
 323                if (r < 0)
 324                        die("unable to sign the tag");
 325                size = r;
 326        }
 327
 328        if (write_sha1_file(buffer, size, tag_type, result) < 0)
 329                die("unable to write tag file");
 330        free(buffer);
 331}
 332
 333int cmd_tag(int argc, const char **argv, const char *prefix)
 334{
 335        unsigned char object[20], prev[20];
 336        int annotate = 0, sign = 0, force = 0, lines = 0;
 337        char *message = NULL;
 338        char ref[PATH_MAX];
 339        const char *object_ref, *tag;
 340        int i;
 341        struct ref_lock *lock;
 342
 343        git_config(git_tag_config);
 344
 345        for (i = 1; i < argc; i++) {
 346                const char *arg = argv[i];
 347
 348                if (arg[0] != '-')
 349                        break;
 350                if (!strcmp(arg, "-a")) {
 351                        annotate = 1;
 352                        continue;
 353                }
 354                if (!strcmp(arg, "-s")) {
 355                        annotate = 1;
 356                        sign = 1;
 357                        continue;
 358                }
 359                if (!strcmp(arg, "-f")) {
 360                        force = 1;
 361                        continue;
 362                }
 363                if (!strcmp(arg, "-n")) {
 364                        if (i + 1 == argc || *argv[i + 1] == '-')
 365                                /* no argument */
 366                                lines = 1;
 367                        else
 368                                lines = isdigit(*argv[++i]) ?
 369                                        atoi(argv[i]) : 1;
 370                        continue;
 371                }
 372                if (!strcmp(arg, "-m")) {
 373                        annotate = 1;
 374                        i++;
 375                        if (i == argc)
 376                                die("option -m needs an argument.");
 377                        if (message)
 378                                die("only one -F or -m option is allowed.");
 379                        message = xstrdup(argv[i]);
 380                        continue;
 381                }
 382                if (!strcmp(arg, "-F")) {
 383                        unsigned long len;
 384                        int fd;
 385
 386                        annotate = 1;
 387                        i++;
 388                        if (i == argc)
 389                                die("option -F needs an argument.");
 390                        if (message)
 391                                die("only one -F or -m option is allowed.");
 392
 393                        if (!strcmp(argv[i], "-"))
 394                                fd = 0;
 395                        else {
 396                                fd = open(argv[i], O_RDONLY);
 397                                if (fd < 0)
 398                                        die("could not open '%s': %s",
 399                                                argv[i], strerror(errno));
 400                        }
 401                        len = 1024;
 402                        message = xmalloc(len);
 403                        if (read_fd(fd, &message, &len)) {
 404                                free(message);
 405                                die("cannot read %s", argv[i]);
 406                        }
 407                        continue;
 408                }
 409                if (!strcmp(arg, "-u")) {
 410                        annotate = 1;
 411                        sign = 1;
 412                        i++;
 413                        if (i == argc)
 414                                die("option -u needs an argument.");
 415                        if (strlcpy(signingkey, argv[i], sizeof(signingkey))
 416                                                        >= sizeof(signingkey))
 417                                die("argument to option -u too long");
 418                        continue;
 419                }
 420                if (!strcmp(arg, "-l"))
 421                        return list_tags(argv[i + 1], lines);
 422                if (!strcmp(arg, "-d"))
 423                        return for_each_tag_name(argv + i + 1, delete_tag);
 424                if (!strcmp(arg, "-v"))
 425                        return for_each_tag_name(argv + i + 1, verify_tag);
 426                usage(builtin_tag_usage);
 427        }
 428
 429        if (i == argc) {
 430                if (annotate)
 431                        usage(builtin_tag_usage);
 432                return list_tags(NULL, lines);
 433        }
 434        tag = argv[i++];
 435
 436        object_ref = i < argc ? argv[i] : "HEAD";
 437        if (i + 1 < argc)
 438                die("too many params");
 439
 440        if (get_sha1(object_ref, object))
 441                die("Failed to resolve '%s' as a valid ref.", object_ref);
 442
 443        if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
 444                die("tag name too long: %.*s...", 50, tag);
 445        if (check_ref_format(ref))
 446                die("'%s' is not a valid tag name.", tag);
 447
 448        if (!resolve_ref(ref, prev, 1, NULL))
 449                hashclr(prev);
 450        else if (!force)
 451                die("tag '%s' already exists", tag);
 452
 453        if (annotate)
 454                create_tag(object, tag, message, sign, object);
 455
 456        lock = lock_any_ref_for_update(ref, prev, 0);
 457        if (!lock)
 458                die("%s: cannot lock the ref", ref);
 459        if (write_ref_sha1(lock, object, NULL) < 0)
 460                die("%s: cannot update the ref", ref);
 461
 462        return 0;
 463}