verify-commit: add option to print raw gpg status information

verify-commit by default displays human-readable output on standard error. However, it can also be useful to get access to the raw gpg status information, which is machine-readable, allowing automated implementation of signing policy. Add a --raw option to make verify-commit produce the gpg status information on standard error instead of the human-readable format. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jun 21, 2015 at 23:14 UTC aeff29dd4dab01b497b2a2cf73e982e846a5fe4c
5 files changed +51 -7
Documentation/git-verify-commit.txt
+4
@@ -16,6 +16,10 @@ Validates the gpg signature created by 'git commit -S'.
16
17 OPTIONS
18 -------
19 +--raw::
20 + Print the raw gpg status output to standard error instead of the normal
21 + human-readable output.
22 +
23 -v::
24 --verbose::
25 Print the contents of the commit object before validating it.
builtin/verify-commit.c
+10 -5
@@ -18,7 +18,7 @@ static const char * const verify_commit_usage[] = {
18 NULL
19 };
20
21 -static int run_gpg_verify(const unsigned char *sha1, const char *buf, unsigned long size, int verbose)
21 +static int run_gpg_verify(const unsigned char *sha1, const char *buf, unsigned long size, unsigned flags)
22 {
23 struct signature_check signature_check;
24 int ret;
@@ -26,13 +26,13 @@ static int run_gpg_verify(const unsigned char *sha1, const char *buf, unsigned l
26 memset(&signature_check, 0, sizeof(signature_check));
27
28 ret = check_commit_signature(lookup_commit(sha1), &signature_check);
29 - print_signature_buffer(&signature_check, verbose ? GPG_VERIFY_VERBOSE : 0);
29 + print_signature_buffer(&signature_check, flags);
30
31 signature_check_clear(&signature_check);
32 return ret;
33 }
34
35 -static int verify_commit(const char *name, int verbose)
35 +static int verify_commit(const char *name, unsigned flags)
36 {
37 enum object_type type;
38 unsigned char sha1[20];
@@ -50,7 +50,7 @@ static int verify_commit(const char *name, int verbose)
50 return error("%s: cannot verify a non-commit object of type %s.",
51 name, typename(type));
52
53 - ret = run_gpg_verify(sha1, buf, size, verbose);
53 + ret = run_gpg_verify(sha1, buf, size, flags);
54
55 free(buf);
56 return ret;
@@ -67,8 +67,10 @@ static int git_verify_commit_config(const char *var, const char *value, void *cb
67 int cmd_verify_commit(int argc, const char **argv, const char *prefix)
68 {
69 int i = 1, verbose = 0, had_error = 0;
70 + unsigned flags = 0;
71 const struct option verify_commit_options[] = {
72 OPT__VERBOSE(&verbose, N_("print commit contents")),
73 + OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
74 OPT_END()
75 };
76
@@ -79,11 +81,14 @@ int cmd_verify_commit(int argc, const char **argv, const char *prefix)
81 if (argc <= i)
82 usage_with_options(verify_commit_usage, verify_commit_options);
83
84 + if (verbose)
85 + flags |= GPG_VERIFY_VERBOSE;
86 +
87 /* sometimes the program was terminated because this signal
88 * was received in the process of writing the gpg input: */
89 signal(SIGPIPE, SIG_IGN);
90 while (i < argc)
86 - if (verify_commit(argv[i++], verbose))
91 + if (verify_commit(argv[i++], flags))
92 had_error = 1;
93 return had_error;
94 }
gpg-interface.c
+5 -2
@@ -87,11 +87,14 @@ int check_signature(const char *payload, size_t plen, const char *signature,
87
88 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
89 {
90 + const char *output = flags & GPG_VERIFY_RAW ?
91 + sigc->gpg_status : sigc->gpg_output;
92 +
93 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
94 fputs(sigc->payload, stdout);
95
93 - if (sigc->gpg_output)
94 - fputs(sigc->gpg_output, stderr);
96 + if (output)
97 + fputs(output, stderr);
98 }
99
100 /*
gpg-interface.h
+1
@@ -2,6 +2,7 @@
2 #define GPG_INTERFACE_H
3
4 #define GPG_VERIFY_VERBOSE 1
5 +#define GPG_VERIFY_RAW 2
6
7 struct signature_check {
8 char *payload;
t/t7510-signed-commit.sh
+31
@@ -88,6 +88,37 @@ test_expect_success GPG 'verify-commit exits success on untrusted signature' '
88 grep "not certified" actual
89 '
90
91 +test_expect_success GPG 'verify signatures with --raw' '
92 + (
93 + for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
94 + do
95 + git verify-commit --raw $commit 2>actual &&
96 + grep "GOODSIG" actual &&
97 + ! grep "BADSIG" actual &&
98 + echo $commit OK || exit 1
99 + done
100 + ) &&
101 + (
102 + for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
103 + do
104 + test_must_fail git verify-commit --raw $commit 2>actual &&
105 + ! grep "GOODSIG" actual &&
106 + ! grep "BADSIG" actual &&
107 + echo $commit OK || exit 1
108 + done
109 + ) &&
110 + (
111 + for commit in eighth-signed-alt
112 + do
113 + git verify-commit --raw $commit 2>actual &&
114 + grep "GOODSIG" actual &&
115 + ! grep "BADSIG" actual &&
116 + grep "TRUST_UNDEFINED" actual &&
117 + echo $commit OK || exit 1
118 + done
119 + )
120 +'
121 +
122 test_expect_success GPG 'show signed commit with signature' '
123 git show -s initial >commit &&
124 git show -s --show-signature initial >show &&