Skip to content

Commit d9dce89

Browse files
Unique-Usmangitster
authored andcommitted
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 <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9c5cfb commit d9dce89

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

builtin/for-each-ref.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define USE_THE_REPOSITORY_VARIABLE
21
#include "builtin.h"
32
#include "commit.h"
43
#include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {
2019
int cmd_for_each_ref(int argc,
2120
const char **argv,
2221
const char *prefix,
23-
struct repository *repo UNUSED)
22+
struct repository *repo)
2423
{
2524
struct ref_sorting *sorting;
2625
struct string_list sorting_options = STRING_LIST_INIT_DUP;
@@ -63,7 +62,7 @@ int cmd_for_each_ref(int argc,
6362

6463
format.format = "%(objectname) %(objecttype)\t%(refname)";
6564

66-
git_config(git_default_config, NULL);
65+
repo_config(repo, git_default_config, NULL);
6766

6867
/* Set default (refname) sorting */
6968
string_list_append(&sorting_options, "refname");

t/t6300-for-each-ref.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ test_expect_success 'Check invalid atoms names are errors' '
292292
test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
293293
'
294294

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+
295302
test_expect_success 'Check format specifiers are ignored in naming date atoms' '
296303
git for-each-ref --format="%(authordate)" refs/heads &&
297304
git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&

0 commit comments

Comments
 (0)