builtin/verify-commit: stop using `the_repository`

Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-commit.c". When `-h` is passed to the command outside a Git repository, the `run_builtin()` will call the `cmd_verify_commit()` function with `repo` set to NULL and then early in the function, `parse_options()` call will give the options help and exit. Pass the repository available in the calling context to `verify_commit()` to remove it's dependency on the global `the_repository` variable. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Usman Akinyemi committed Mar 8, 2025 at 05:05 UTC db58d5a351031ec51817c1a6566ec5672ba17138
2 files changed +13 -7
builtin/verify-commit.c
+6 -7
@@ -5,7 +5,6 @@
5 *
6 * Based on git-verify-tag
7 */
8 -#define USE_THE_REPOSITORY_VARIABLE
8 #include "builtin.h"
9 #include "config.h"
10 #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)
32 return ret;
33 }
34
36 -static int verify_commit(const char *name, unsigned flags)
35 +static int verify_commit(struct repository *repo, const char *name, unsigned flags)
36 {
37 struct object_id oid;
38 struct object *obj;
39
41 - if (repo_get_oid(the_repository, name, &oid))
40 + if (repo_get_oid(repo, name, &oid))
41 return error("commit '%s' not found.", name);
42
44 - obj = parse_object(the_repository, &oid);
43 + obj = parse_object(repo, &oid);
44 if (!obj)
45 return error("%s: unable to read file.", name);
46 if (obj->type != OBJ_COMMIT)
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)
53 int cmd_verify_commit(int argc,
54 const char **argv,
55 const char *prefix,
57 - struct repository *repo UNUSED)
56 + struct repository *repo)
57 {
58 int i = 1, verbose = 0, had_error = 0;
59 unsigned flags = 0;
@@ -64,7 +63,7 @@ int cmd_verify_commit(int argc,
63 OPT_END()
64 };
65
67 - git_config(git_default_config, NULL);
66 + repo_config(repo, git_default_config, NULL);
67
68 argc = parse_options(argc, argv, prefix, verify_commit_options,
69 verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
@@ -78,7 +77,7 @@ int cmd_verify_commit(int argc,
77 * was received in the process of writing the gpg input: */
78 signal(SIGPIPE, SIG_IGN);
79 while (i < argc)
81 - if (verify_commit(argv[i++], flags))
80 + if (verify_commit(repo, argv[i++], flags))
81 had_error = 1;
82 return had_error;
83 }
t/t7510-signed-commit.sh
+7
@@ -8,6 +8,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 GNUPGHOME_NOT_USED=$GNUPGHOME
9 . "$TEST_DIRECTORY/lib-gpg.sh"
10
11 +test_expect_success GPG 'verify-commit does not crash with -h' '
12 + test_expect_code 129 git verify-commit -h >usage &&
13 + test_grep "[Uu]sage: git verify-commit " usage &&
14 + test_expect_code 129 nongit git verify-commit -h >usage &&
15 + test_grep "[Uu]sage: git verify-commit " usage
16 +'
17 +
18 test_expect_success GPG 'create signed commits' '
19 test_oid_cache <<-\EOF &&
20 header sha1:gpgsig