2 * Builtin "git verify-tag"
4 * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com>
6 * Based on git-verify-tag.sh
11 #include "run-command.h"
13 #include "parse-options.h"
15 static const char * const verify_tag_usage[] = {
16 "git verify-tag [-v|--verbose] <tag>...",
20 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
22 static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
24 struct child_process gpg;
25 const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
26 char path[PATH_MAX], *eol;
30 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
32 return error("could not create temporary file '%s': %s",
33 path, strerror(errno));
34 if (write_in_full(fd, buf, size) < 0)
35 return error("failed writing temporary file '%s': %s",
36 path, strerror(errno));
39 /* find the length without signature */
41 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
42 eol = memchr(buf + len, '\n', size - len);
43 len += eol ? eol - (buf + len) + 1 : size - len;
46 write_in_full(1, buf, len);
48 memset(&gpg, 0, sizeof(gpg));
52 if (start_command(&gpg)) {
54 return error("could not run gpg.");
57 write_in_full(gpg.in, buf, len);
59 ret = finish_command(&gpg);
66 static int verify_tag(const char *name, int verbose)
68 enum object_type type;
69 unsigned char sha1[20];
74 if (get_sha1(name, sha1))
75 return error("tag '%s' not found.", name);
77 type = sha1_object_info(sha1, NULL);
79 return error("%s: cannot verify a non-tag object of type %s.",
80 name, typename(type));
82 buf = read_sha1_file(sha1, &type, &size);
84 return error("%s: unable to read file.", name);
86 ret = run_gpg_verify(buf, size, verbose);
92 int cmd_verify_tag(int argc, const char **argv, const char *prefix)
94 int i = 1, verbose = 0, had_error = 0;
95 const struct option verify_tag_options[] = {
96 OPT__VERBOSE(&verbose),
100 git_config(git_default_config, NULL);
102 argc = parse_options(argc, argv, prefix, verify_tag_options,
103 verify_tag_usage, PARSE_OPT_KEEP_ARGV0);
105 usage_with_options(verify_tag_usage, verify_tag_options);
107 /* sometimes the program was terminated because this signal
108 * was received in the process of writing the gpg input: */
109 signal(SIGPIPE, SIG_IGN);
111 if (verify_tag(argv[i++], verbose))