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