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