]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | static const char * const verify_commit_usage[] = { | |
9c9b4f2f | 20 | N_("git verify-commit [-v | --verbose] <commit>..."), |
d07b00b7 MG |
21 | NULL |
22 | }; | |
23 | ||
83730370 | 24 | static int run_gpg_verify(struct commit *commit, 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 | ||
83730370 | 31 | ret = check_commit_signature(commit, &signature_check); |
aeff29dd | 32 | print_signature_buffer(&signature_check, flags); |
d07b00b7 MG |
33 | |
34 | signature_check_clear(&signature_check); | |
434060ec | 35 | return ret; |
d07b00b7 MG |
36 | } |
37 | ||
aeff29dd | 38 | static int verify_commit(const char *name, unsigned flags) |
d07b00b7 | 39 | { |
eee886cf | 40 | struct object_id oid; |
83730370 | 41 | struct object *obj; |
d07b00b7 | 42 | |
eee886cf | 43 | if (get_oid(name, &oid)) |
d07b00b7 MG |
44 | return error("commit '%s' not found.", name); |
45 | ||
83730370 JK |
46 | obj = parse_object(the_repository, &oid); |
47 | if (!obj) | |
d07b00b7 | 48 | return error("%s: unable to read file.", name); |
83730370 | 49 | if (obj->type != OBJ_COMMIT) |
d07b00b7 | 50 | return error("%s: cannot verify a non-commit object of type %s.", |
83730370 | 51 | name, type_name(obj->type)); |
d07b00b7 | 52 | |
83730370 | 53 | return run_gpg_verify((struct commit *)obj, flags); |
d07b00b7 MG |
54 | } |
55 | ||
56 | static int git_verify_commit_config(const char *var, const char *value, void *cb) | |
57 | { | |
58 | int status = git_gpg_config(var, value, cb); | |
59 | if (status) | |
60 | return status; | |
61 | return git_default_config(var, value, cb); | |
62 | } | |
63 | ||
64 | int cmd_verify_commit(int argc, const char **argv, const char *prefix) | |
65 | { | |
66 | int i = 1, verbose = 0, had_error = 0; | |
aeff29dd | 67 | unsigned flags = 0; |
d07b00b7 MG |
68 | const struct option verify_commit_options[] = { |
69 | OPT__VERBOSE(&verbose, N_("print commit contents")), | |
aeff29dd | 70 | OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW), |
d07b00b7 MG |
71 | OPT_END() |
72 | }; | |
73 | ||
74 | git_config(git_verify_commit_config, NULL); | |
75 | ||
76 | argc = parse_options(argc, argv, prefix, verify_commit_options, | |
77 | verify_commit_usage, PARSE_OPT_KEEP_ARGV0); | |
78 | if (argc <= i) | |
79 | usage_with_options(verify_commit_usage, verify_commit_options); | |
80 | ||
aeff29dd | 81 | if (verbose) |
82 | flags |= GPG_VERIFY_VERBOSE; | |
83 | ||
d07b00b7 MG |
84 | /* sometimes the program was terminated because this signal |
85 | * was received in the process of writing the gpg input: */ | |
86 | signal(SIGPIPE, SIG_IGN); | |
87 | while (i < argc) | |
aeff29dd | 88 | if (verify_commit(argv[i++], flags)) |
d07b00b7 MG |
89 | had_error = 1; |
90 | return had_error; | |
91 | } |