builtin/for-each-ref: stop using `the_repository`

Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/for-each-ref.c". When `-h` is passed to the command outside a Git repository, the `run_builtin()` will call the `cmd_for_each_ref()` function with `repo` set to NULL and then early in the function, `parse_options()` call will give the options help and exit. 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 d9dce89192504c63787b98961ee514937af88e61
2 files changed +9 -3
builtin/for-each-ref.c
+2 -3
@@ -1,4 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
1 #include "builtin.h"
2 #include "commit.h"
3 #include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {
19 int cmd_for_each_ref(int argc,
20 const char **argv,
21 const char *prefix,
23 - struct repository *repo UNUSED)
22 + struct repository *repo)
23 {
24 struct ref_sorting *sorting;
25 struct string_list sorting_options = STRING_LIST_INIT_DUP;
@@ -63,7 +62,7 @@ int cmd_for_each_ref(int argc,
62
63 format.format = "%(objectname) %(objecttype)\t%(refname)";
64
66 - git_config(git_default_config, NULL);
65 + repo_config(repo, git_default_config, NULL);
66
67 /* Set default (refname) sorting */
68 string_list_append(&sorting_options, "refname");
t/t6300-for-each-ref.sh
+7
@@ -292,6 +292,13 @@ test_expect_success 'Check invalid atoms names are errors' '
292 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
293 '
294
295 +test_expect_success 'for-each-ref does not crash with -h' '
296 + test_expect_code 129 git for-each-ref -h >usage &&
297 + test_grep "[Uu]sage: git for-each-ref " usage &&
298 + test_expect_code 129 nongit git for-each-ref -h >usage &&
299 + test_grep "[Uu]sage: git for-each-ref " usage
300 +'
301 +
302 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
303 git for-each-ref --format="%(authordate)" refs/heads &&
304 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&