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