]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/verify-commit: stop using `the_repository`
authorUsman Akinyemi <usmanakinyemi202@gmail.com>
Fri, 7 Mar 2025 23:35:02 +0000 (05:05 +0530)
committerJunio C Hamano <gitster@pobox.com>
Sat, 8 Mar 2025 00:52:01 +0000 (16:52 -0800)
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>
builtin/verify-commit.c
t/t7510-signed-commit.sh

index 779b7988ca97847c087d4ec697616d4506cb71d2..5f749a30daf0155514f54de628a217b09ea0cf91 100644 (file)
@@ -5,7 +5,6 @@
  *
  * Based on git-verify-tag
  */
-#define USE_THE_REPOSITORY_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)
        return ret;
 }
 
-static int verify_commit(const char *name, unsigned flags)
+static int verify_commit(struct repository *repo, const char *name, unsigned flags)
 {
        struct object_id oid;
        struct object *obj;
 
-       if (repo_get_oid(the_repository, name, &oid))
+       if (repo_get_oid(repo, name, &oid))
                return error("commit '%s' not found.", name);
 
-       obj = parse_object(the_repository, &oid);
+       obj = parse_object(repo, &oid);
        if (!obj)
                return error("%s: unable to read file.", name);
        if (obj->type != OBJ_COMMIT)
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)
 int cmd_verify_commit(int argc,
                      const char **argv,
                      const char *prefix,
-                     struct repository *repo UNUSED)
+                     struct repository *repo)
 {
        int i = 1, verbose = 0, had_error = 0;
        unsigned flags = 0;
@@ -64,7 +63,7 @@ int cmd_verify_commit(int argc,
                OPT_END()
        };
 
-       git_config(git_default_config, NULL);
+       repo_config(repo, git_default_config, NULL);
 
        argc = parse_options(argc, argv, prefix, verify_commit_options,
                             verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
@@ -78,7 +77,7 @@ int cmd_verify_commit(int argc,
         * was received in the process of writing the gpg input: */
        signal(SIGPIPE, SIG_IGN);
        while (i < argc)
-               if (verify_commit(argv[i++], flags))
+               if (verify_commit(repo, argv[i++], flags))
                        had_error = 1;
        return had_error;
 }
index 0d2dd29fe6a12c20ffbbe169afabd4c31bc6545c..39677e859ab31173f7eaa36df1a0d3ecfe285abf 100755 (executable)
@@ -8,6 +8,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 GNUPGHOME_NOT_USED=$GNUPGHOME
 . "$TEST_DIRECTORY/lib-gpg.sh"
 
+test_expect_success GPG 'verify-commit does not crash with -h' '
+       test_expect_code 129 git verify-commit -h >usage &&
+       test_grep "[Uu]sage: git verify-commit " usage &&
+       test_expect_code 129 nongit git verify-commit -h >usage &&
+       test_grep "[Uu]sage: git verify-commit " usage
+'
+
 test_expect_success GPG 'create signed commits' '
        test_oid_cache <<-\EOF &&
        header sha1:gpgsig