e30f7cfbc16c44cd5da8b122c29849a2106d4a2d
1/*
2 * Builtin "git commit-commit"
3 *
4 * Copyright (c) 2014 Michael J Gruber <git@drmicha.warpmail.net>
5 *
6 * Based on git-verify-tag
7 */
8#include "cache.h"
9#include "builtin.h"
10#include "commit.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_commit_usage[] = {
17 N_("git verify-commit [-v | --verbose] <commit>..."),
18 NULL
19};
20
21static int run_gpg_verify(const unsigned char *sha1, const char *buf, unsigned long size, int verbose)
22{
23 struct signature_check signature_check;
24 int ret;
25
26 memset(&signature_check, 0, sizeof(signature_check));
27
28 ret = check_commit_signature(lookup_commit(sha1), &signature_check);
29
30 if (verbose && signature_check.payload)
31 fputs(signature_check.payload, stdout);
32
33 if (signature_check.gpg_output)
34 fputs(signature_check.gpg_output, stderr);
35
36 signature_check_clear(&signature_check);
37 return ret;
38}
39
40static int verify_commit(const char *name, int verbose)
41{
42 enum object_type type;
43 unsigned char sha1[20];
44 char *buf;
45 unsigned long size;
46 int ret;
47
48 if (get_sha1(name, sha1))
49 return error("commit '%s' not found.", name);
50
51 buf = read_sha1_file(sha1, &type, &size);
52 if (!buf)
53 return error("%s: unable to read file.", name);
54 if (type != OBJ_COMMIT)
55 return error("%s: cannot verify a non-commit object of type %s.",
56 name, typename(type));
57
58 ret = run_gpg_verify(sha1, buf, size, verbose);
59
60 free(buf);
61 return ret;
62}
63
64static int git_verify_commit_config(const char *var, const char *value, void *cb)
65{
66 int status = git_gpg_config(var, value, cb);
67 if (status)
68 return status;
69 return git_default_config(var, value, cb);
70}
71
72int cmd_verify_commit(int argc, const char **argv, const char *prefix)
73{
74 int i = 1, verbose = 0, had_error = 0;
75 const struct option verify_commit_options[] = {
76 OPT__VERBOSE(&verbose, N_("print commit contents")),
77 OPT_END()
78 };
79
80 git_config(git_verify_commit_config, NULL);
81
82 argc = parse_options(argc, argv, prefix, verify_commit_options,
83 verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
84 if (argc <= i)
85 usage_with_options(verify_commit_usage, verify_commit_options);
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)
91 if (verify_commit(argv[i++], verbose))
92 had_error = 1;
93 return had_error;
94}