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