Merge branch 'mh/mingw-case-sensitive-build'
[alt-git.git] / builtin / verify-commit.c
blob9680b5870130615b7b4432a5ce456279db750aa4
1 /*
2 * Builtin "git commit-commit"
4 * Copyright (c) 2014 Michael J Gruber <git@drmicha.warpmail.net>
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 "object-store-ll.h"
13 #include "repository.h"
14 #include "commit.h"
15 #include "run-command.h"
16 #include "parse-options.h"
17 #include "gpg-interface.h"
19 static const char * const verify_commit_usage[] = {
20 N_("git verify-commit [-v | --verbose] [--raw] <commit>..."),
21 NULL
24 static int run_gpg_verify(struct commit *commit, unsigned flags)
26 struct signature_check signature_check;
27 int ret;
29 memset(&signature_check, 0, sizeof(signature_check));
31 ret = check_commit_signature(commit, &signature_check);
32 print_signature_buffer(&signature_check, flags);
34 signature_check_clear(&signature_check);
35 return ret;
38 static int verify_commit(const char *name, unsigned flags)
40 struct object_id oid;
41 struct object *obj;
43 if (repo_get_oid(the_repository, name, &oid))
44 return error("commit '%s' not found.", name);
46 obj = parse_object(the_repository, &oid);
47 if (!obj)
48 return error("%s: unable to read file.", name);
49 if (obj->type != OBJ_COMMIT)
50 return error("%s: cannot verify a non-commit object of type %s.",
51 name, type_name(obj->type));
53 return run_gpg_verify((struct commit *)obj, flags);
56 int cmd_verify_commit(int argc, const char **argv, const char *prefix)
58 int i = 1, verbose = 0, had_error = 0;
59 unsigned flags = 0;
60 const struct option verify_commit_options[] = {
61 OPT__VERBOSE(&verbose, N_("print commit contents")),
62 OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
63 OPT_END()
66 git_config(git_default_config, NULL);
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);
73 if (verbose)
74 flags |= GPG_VERIFY_VERBOSE;
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)
80 if (verify_commit(argv[i++], flags))
81 had_error = 1;
82 return had_error;