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