builtin / verify-tag.con commit verify-tag: share code with verify-commit (a4cc18f)
   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#include "gpg-interface.h"
  15
  16static const char * const verify_tag_usage[] = {
  17                N_("git verify-tag [-v | --verbose] <tag>..."),
  18                NULL
  19};
  20
  21static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
  22{
  23        struct signature_check sigc;
  24        int len;
  25
  26        memset(&sigc, 0, sizeof(sigc));
  27
  28        len = parse_signature(buf, size);
  29        if (verbose)
  30                write_in_full(1, buf, len);
  31
  32        if (size == len)
  33                return error("no signature found");
  34
  35        check_signature(buf, len, buf + len, size - len, &sigc);
  36        fputs(sigc.gpg_output, stderr);
  37
  38        signature_check_clear(&sigc);
  39        return sigc.result != 'G' && sigc.result != 'U';
  40}
  41
  42static int verify_tag(const char *name, int verbose)
  43{
  44        enum object_type type;
  45        unsigned char sha1[20];
  46        char *buf;
  47        unsigned long size;
  48        int ret;
  49
  50        if (get_sha1(name, sha1))
  51                return error("tag '%s' not found.", name);
  52
  53        type = sha1_object_info(sha1, NULL);
  54        if (type != OBJ_TAG)
  55                return error("%s: cannot verify a non-tag object of type %s.",
  56                                name, typename(type));
  57
  58        buf = read_sha1_file(sha1, &type, &size);
  59        if (!buf)
  60                return error("%s: unable to read file.", name);
  61
  62        ret = run_gpg_verify(buf, size, verbose);
  63
  64        free(buf);
  65        return ret;
  66}
  67
  68static int git_verify_tag_config(const char *var, const char *value, void *cb)
  69{
  70        int status = git_gpg_config(var, value, cb);
  71        if (status)
  72                return status;
  73        return git_default_config(var, value, cb);
  74}
  75
  76int cmd_verify_tag(int argc, const char **argv, const char *prefix)
  77{
  78        int i = 1, verbose = 0, had_error = 0;
  79        const struct option verify_tag_options[] = {
  80                OPT__VERBOSE(&verbose, N_("print tag contents")),
  81                OPT_END()
  82        };
  83
  84        git_config(git_verify_tag_config, NULL);
  85
  86        argc = parse_options(argc, argv, prefix, verify_tag_options,
  87                             verify_tag_usage, PARSE_OPT_KEEP_ARGV0);
  88        if (argc <= i)
  89                usage_with_options(verify_tag_usage, verify_tag_options);
  90
  91        /* sometimes the program was terminated because this signal
  92         * was received in the process of writing the gpg input: */
  93        signal(SIGPIPE, SIG_IGN);
  94        while (i < argc)
  95                if (verify_tag(argv[i++], verbose))
  96                        had_error = 1;
  97        return had_error;
  98}