]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/verify-commit.c
Start the 2.46 cycle
[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 */
d07b00b7 8#include "builtin.h"
bc5c5ec0 9#include "config.h"
f394e093 10#include "gettext.h"
dabab1d6 11#include "object-name.h"
c1f5eb49 12#include "repository.h"
d07b00b7 13#include "commit.h"
d07b00b7
MG
14#include "parse-options.h"
15#include "gpg-interface.h"
16
17static const char * const verify_commit_usage[] = {
8c9e292d 18 N_("git verify-commit [-v | --verbose] [--raw] <commit>..."),
d07b00b7
MG
19 NULL
20};
21
83730370 22static int run_gpg_verify(struct commit *commit, 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
83730370 29 ret = check_commit_signature(commit, &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 37{
eee886cf 38 struct object_id oid;
83730370 39 struct object *obj;
d07b00b7 40
d850b7a5 41 if (repo_get_oid(the_repository, name, &oid))
d07b00b7
MG
42 return error("commit '%s' not found.", name);
43
83730370
JK
44 obj = parse_object(the_repository, &oid);
45 if (!obj)
d07b00b7 46 return error("%s: unable to read file.", name);
83730370 47 if (obj->type != OBJ_COMMIT)
d07b00b7 48 return error("%s: cannot verify a non-commit object of type %s.",
83730370 49 name, type_name(obj->type));
d07b00b7 50
83730370 51 return run_gpg_verify((struct commit *)obj, flags);
d07b00b7
MG
52}
53
d07b00b7
MG
54int cmd_verify_commit(int argc, const char **argv, const char *prefix)
55{
56 int i = 1, verbose = 0, had_error = 0;
aeff29dd 57 unsigned flags = 0;
d07b00b7
MG
58 const struct option verify_commit_options[] = {
59 OPT__VERBOSE(&verbose, N_("print commit contents")),
aeff29dd 60 OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
d07b00b7
MG
61 OPT_END()
62 };
63
cc5d1d32 64 git_config(git_default_config, NULL);
d07b00b7
MG
65
66 argc = parse_options(argc, argv, prefix, verify_commit_options,
67 verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
68 if (argc <= i)
69 usage_with_options(verify_commit_usage, verify_commit_options);
70
aeff29dd 71 if (verbose)
72 flags |= GPG_VERIFY_VERBOSE;
73
d07b00b7
MG
74 /* sometimes the program was terminated because this signal
75 * was received in the process of writing the gpg input: */
76 signal(SIGPIPE, SIG_IGN);
77 while (i < argc)
aeff29dd 78 if (verify_commit(argv[i++], flags))
d07b00b7
MG
79 had_error = 1;
80 return had_error;
81}