verify-tag: share code with verify-commit
[alt-git.git] / builtin / verify-tag.c
blobe1eb341baeaa4833fdff08b11901264f4bd207ea
1 /*
2 * Builtin "git verify-tag"
4 * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com>
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"
16 static const char * const verify_tag_usage[] = {
17 N_("git verify-tag [-v | --verbose] <tag>..."),
18 NULL
21 static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
23 struct signature_check sigc;
24 int len;
26 memset(&sigc, 0, sizeof(sigc));
28 len = parse_signature(buf, size);
29 if (verbose)
30 write_in_full(1, buf, len);
32 if (size == len)
33 return error("no signature found");
35 check_signature(buf, len, buf + len, size - len, &sigc);
36 fputs(sigc.gpg_output, stderr);
38 signature_check_clear(&sigc);
39 return sigc.result != 'G' && sigc.result != 'U';
42 static int verify_tag(const char *name, int verbose)
44 enum object_type type;
45 unsigned char sha1[20];
46 char *buf;
47 unsigned long size;
48 int ret;
50 if (get_sha1(name, sha1))
51 return error("tag '%s' not found.", name);
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));
58 buf = read_sha1_file(sha1, &type, &size);
59 if (!buf)
60 return error("%s: unable to read file.", name);
62 ret = run_gpg_verify(buf, size, verbose);
64 free(buf);
65 return ret;
68 static int git_verify_tag_config(const char *var, const char *value, void *cb)
70 int status = git_gpg_config(var, value, cb);
71 if (status)
72 return status;
73 return git_default_config(var, value, cb);
76 int cmd_verify_tag(int argc, const char **argv, const char *prefix)
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()
84 git_config(git_verify_tag_config, NULL);
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);
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;