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