Git 2.22.4
[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 "config.h"
10 #include "builtin.h"
11 #include "object-store.h"
12 #include "repository.h"
13 #include "commit.h"
14 #include "run-command.h"
15 #include "parse-options.h"
16 #include "gpg-interface.h"
17
18 static const char * const verify_commit_usage[] = {
19                 N_("git verify-commit [-v | --verbose] <commit>..."),
20                 NULL
21 };
22
23 static int run_gpg_verify(const struct object_id *oid, const char *buf, unsigned long size, unsigned flags)
24 {
25         struct signature_check signature_check;
26         int ret;
27
28         memset(&signature_check, 0, sizeof(signature_check));
29
30         ret = check_commit_signature(lookup_commit(the_repository, oid),
31                                      &signature_check);
32         print_signature_buffer(&signature_check, flags);
33
34         signature_check_clear(&signature_check);
35         return ret;
36 }
37
38 static int verify_commit(const char *name, unsigned flags)
39 {
40         enum object_type type;
41         struct object_id oid;
42         char *buf;
43         unsigned long size;
44         int ret;
45
46         if (get_oid(name, &oid))
47                 return error("commit '%s' not found.", name);
48
49         buf = read_object_file(&oid, &type, &size);
50         if (!buf)
51                 return error("%s: unable to read file.", name);
52         if (type != OBJ_COMMIT)
53                 return error("%s: cannot verify a non-commit object of type %s.",
54                                 name, type_name(type));
55
56         ret = run_gpg_verify(&oid, buf, size, flags);
57
58         free(buf);
59         return ret;
60 }
61
62 static int git_verify_commit_config(const char *var, const char *value, void *cb)
63 {
64         int status = git_gpg_config(var, value, cb);
65         if (status)
66                 return status;
67         return git_default_config(var, value, cb);
68 }
69
70 int cmd_verify_commit(int argc, const char **argv, const char *prefix)
71 {
72         int i = 1, verbose = 0, had_error = 0;
73         unsigned flags = 0;
74         const struct option verify_commit_options[] = {
75                 OPT__VERBOSE(&verbose, N_("print commit contents")),
76                 OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
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         if (verbose)
88                 flags |= GPG_VERIFY_VERBOSE;
89
90         /* sometimes the program was terminated because this signal
91          * was received in the process of writing the gpg input: */
92         signal(SIGPIPE, SIG_IGN);
93         while (i < argc)
94                 if (verify_commit(argv[i++], flags))
95                         had_error = 1;
96         return had_error;
97 }