builtin / verify-tag.con commit i18n: git-fetch split up "(non-fast-forward)" message (321a153)
   1/*
   2 * Builtin "git verify-tag"
   3 *
   4 * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com>
   5 *
   6 * Based on git-verify-tag.sh
   7 */
   8#include "cache.h"
   9#include "builtin.h"
  10#include "tag.h"
  11#include "run-command.h"
  12#include <signal.h>
  13#include "parse-options.h"
  14
  15static const char * const verify_tag_usage[] = {
  16                "git verify-tag [-v|--verbose] <tag>...",
  17                NULL
  18};
  19
  20static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
  21{
  22        struct child_process gpg;
  23        const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
  24        char path[PATH_MAX];
  25        size_t len;
  26        int fd, ret;
  27
  28        fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
  29        if (fd < 0)
  30                return error("could not create temporary file '%s': %s",
  31                                                path, strerror(errno));
  32        if (write_in_full(fd, buf, size) < 0)
  33                return error("failed writing temporary file '%s': %s",
  34                                                path, strerror(errno));
  35        close(fd);
  36
  37        /* find the length without signature */
  38        len = parse_signature(buf, size);
  39        if (verbose)
  40                write_in_full(1, buf, len);
  41
  42        memset(&gpg, 0, sizeof(gpg));
  43        gpg.argv = args_gpg;
  44        gpg.in = -1;
  45        args_gpg[2] = path;
  46        if (start_command(&gpg)) {
  47                unlink(path);
  48                return error("could not run gpg.");
  49        }
  50
  51        write_in_full(gpg.in, buf, len);
  52        close(gpg.in);
  53        ret = finish_command(&gpg);
  54
  55        unlink_or_warn(path);
  56
  57        return ret;
  58}
  59
  60static int verify_tag(const char *name, int verbose)
  61{
  62        enum object_type type;
  63        unsigned char sha1[20];
  64        char *buf;
  65        unsigned long size;
  66        int ret;
  67
  68        if (get_sha1(name, sha1))
  69                return error("tag '%s' not found.", name);
  70
  71        type = sha1_object_info(sha1, NULL);
  72        if (type != OBJ_TAG)
  73                return error("%s: cannot verify a non-tag object of type %s.",
  74                                name, typename(type));
  75
  76        buf = read_sha1_file(sha1, &type, &size);
  77        if (!buf)
  78                return error("%s: unable to read file.", name);
  79
  80        ret = run_gpg_verify(buf, size, verbose);
  81
  82        free(buf);
  83        return ret;
  84}
  85
  86int cmd_verify_tag(int argc, const char **argv, const char *prefix)
  87{
  88        int i = 1, verbose = 0, had_error = 0;
  89        const struct option verify_tag_options[] = {
  90                OPT__VERBOSE(&verbose, "print tag contents"),
  91                OPT_END()
  92        };
  93
  94        git_config(git_default_config, NULL);
  95
  96        argc = parse_options(argc, argv, prefix, verify_tag_options,
  97                             verify_tag_usage, PARSE_OPT_KEEP_ARGV0);
  98        if (argc <= i)
  99                usage_with_options(verify_tag_usage, verify_tag_options);
 100
 101        /* sometimes the program was terminated because this signal
 102         * was received in the process of writing the gpg input: */
 103        signal(SIGPIPE, SIG_IGN);
 104        while (i < argc)
 105                if (verify_tag(argv[i++], verbose))
 106                        had_error = 1;
 107        return had_error;
 108}