]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/verify-commit.c
Merge branch 'jc/format-patch-error-check'
[thirdparty/git.git] / builtin / verify-commit.c
CommitLineData
d07b00b7
MG
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"
b2141fc1 9#include "config.h"
d07b00b7 10#include "builtin.h"
cbd53a21 11#include "object-store.h"
c1f5eb49 12#include "repository.h"
d07b00b7
MG
13#include "commit.h"
14#include "run-command.h"
15#include <signal.h>
16#include "parse-options.h"
17#include "gpg-interface.h"
18
19static const char * const verify_commit_usage[] = {
9c9b4f2f 20 N_("git verify-commit [-v | --verbose] <commit>..."),
d07b00b7
MG
21 NULL
22};
23
eee886cf 24static int run_gpg_verify(const struct object_id *oid, const char *buf, unsigned long size, unsigned flags)
d07b00b7
MG
25{
26 struct signature_check signature_check;
434060ec 27 int ret;
d07b00b7
MG
28
29 memset(&signature_check, 0, sizeof(signature_check));
30
c1f5eb49
SB
31 ret = check_commit_signature(lookup_commit(the_repository, oid),
32 &signature_check);
aeff29dd 33 print_signature_buffer(&signature_check, flags);
d07b00b7
MG
34
35 signature_check_clear(&signature_check);
434060ec 36 return ret;
d07b00b7
MG
37}
38
aeff29dd 39static int verify_commit(const char *name, unsigned flags)
d07b00b7
MG
40{
41 enum object_type type;
eee886cf 42 struct object_id oid;
d07b00b7
MG
43 char *buf;
44 unsigned long size;
45 int ret;
46
eee886cf 47 if (get_oid(name, &oid))
d07b00b7
MG
48 return error("commit '%s' not found.", name);
49
b4f5aca4 50 buf = read_object_file(&oid, &type, &size);
d07b00b7
MG
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.",
debca9d2 55 name, type_name(type));
d07b00b7 56
eee886cf 57 ret = run_gpg_verify(&oid, buf, size, flags);
d07b00b7
MG
58
59 free(buf);
60 return ret;
61}
62
63static 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
71int cmd_verify_commit(int argc, const char **argv, const char *prefix)
72{
73 int i = 1, verbose = 0, had_error = 0;
aeff29dd 74 unsigned flags = 0;
d07b00b7
MG
75 const struct option verify_commit_options[] = {
76 OPT__VERBOSE(&verbose, N_("print commit contents")),
aeff29dd 77 OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
d07b00b7
MG
78 OPT_END()
79 };
80
81 git_config(git_verify_commit_config, NULL);
82
83 argc = parse_options(argc, argv, prefix, verify_commit_options,
84 verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
85 if (argc <= i)
86 usage_with_options(verify_commit_usage, verify_commit_options);
87
aeff29dd 88 if (verbose)
89 flags |= GPG_VERIFY_VERBOSE;
90
d07b00b7
MG
91 /* sometimes the program was terminated because this signal
92 * was received in the process of writing the gpg input: */
93 signal(SIGPIPE, SIG_IGN);
94 while (i < argc)
aeff29dd 95 if (verify_commit(argv[i++], flags))
d07b00b7
MG
96 had_error = 1;
97 return had_error;
98}