Skip to content

Commit d9c5cfb

Browse files
Unique-Usmangitster
authored andcommitted
builtin/ls-files: stop using the_repository
Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/ls-files.c". When `-h` is passed to the command outside a Git repository, the `run_builtin()` will call the `cmd_ls_files()` function with `repo` set to NULL and then early in the function, `show_usage_with_options_if_asked()` call will give the options help and exit. Pass the repository available in the calling context to both `expand_objectsize()` and `show_ru_info()` to remove their dependency on the global `the_repository` variable. Mentored-by: Christian Couder <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 72fe8bf commit d9c5cfb

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

builtin/ls-files.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* Copyright (C) Linus Torvalds, 2005
77
*/
88

9-
#define USE_THE_REPOSITORY_VARIABLE
109
#define DISABLE_SIGN_COMPARE_WARNINGS
1110

1211
#include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,
245244
repo_clear(&subrepo);
246245
}
247246

248-
static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
247+
static void expand_objectsize(struct repository *repo, struct strbuf *line,
248+
const struct object_id *oid,
249249
const enum object_type type, unsigned int padded)
250250
{
251251
if (type == OBJ_BLOB) {
252252
unsigned long size;
253-
if (oid_object_info(the_repository, oid, &size) < 0)
253+
if (oid_object_info(repo, oid, &size) < 0)
254254
die(_("could not get object info about '%s'"),
255255
oid_to_hex(oid));
256256
if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
283283
else if (skip_prefix(format, "(objecttype)", &format))
284284
strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
285285
else if (skip_prefix(format, "(objectsize:padded)", &format))
286-
expand_objectsize(&sb, &ce->oid,
286+
expand_objectsize(repo, &sb, &ce->oid,
287287
object_type(ce->ce_mode), 1);
288288
else if (skip_prefix(format, "(objectsize)", &format))
289-
expand_objectsize(&sb, &ce->oid,
289+
expand_objectsize(repo, &sb, &ce->oid,
290290
object_type(ce->ce_mode), 0);
291291
else if (skip_prefix(format, "(stage)", &format))
292292
strbuf_addf(&sb, "%d", ce_stage(ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
348348
}
349349
}
350350

351-
static void show_ru_info(struct index_state *istate)
351+
static void show_ru_info(struct repository *repo, struct index_state *istate)
352352
{
353353
struct string_list_item *item;
354354

@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)
370370
if (!ui->mode[i])
371371
continue;
372372
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
373-
repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
373+
repo_find_unique_abbrev(repo, &ui->oid[i], abbrev),
374374
i + 1);
375375
write_name(path);
376376
}
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,
567567
int cmd_ls_files(int argc,
568568
const char **argv,
569569
const char *cmd_prefix,
570-
struct repository *repo UNUSED)
570+
struct repository *repo)
571571
{
572572
int require_work_tree = 0, show_tag = 0, i;
573573
char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,
647647
show_usage_with_options_if_asked(argc, argv,
648648
ls_files_usage, builtin_ls_files_options);
649649

650-
prepare_repo_settings(the_repository);
651-
the_repository->settings.command_requires_full_index = 0;
650+
prepare_repo_settings(repo);
651+
repo->settings.command_requires_full_index = 0;
652652

653653
prefix = cmd_prefix;
654654
if (prefix)
655655
prefix_len = strlen(prefix);
656-
git_config(git_default_config, NULL);
656+
repo_config(repo, git_default_config, NULL);
657657

658-
if (repo_read_index(the_repository) < 0)
658+
if (repo_read_index(repo) < 0)
659659
die("index file corrupt");
660660

661661
argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc,
724724
max_prefix = common_prefix(&pathspec);
725725
max_prefix_len = get_common_prefix_len(max_prefix);
726726

727-
prune_index(the_repository->index, max_prefix, max_prefix_len);
727+
prune_index(repo->index, max_prefix, max_prefix_len);
728728

729729
/* Treat unmatching pathspec elements as errors */
730730
if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,
748748
*/
749749
if (show_stage || show_unmerged)
750750
die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
751-
overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
751+
overlay_tree_on_index(repo->index, with_tree, max_prefix);
752752
}
753753

754-
show_files(the_repository, &dir);
754+
show_files(repo, &dir);
755755

756756
if (show_resolve_undo)
757-
show_ru_info(the_repository->index);
757+
show_ru_info(repo, repo->index);
758758

759759
if (ps_matched && report_path_error(ps_matched, &pathspec)) {
760760
fprintf(stderr, "Did you forget to 'git add'?\n");

t/t3004-ls-files-basic.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' '
3434
test_grep "[Uu]sage: git ls-files " broken/usage
3535
'
3636

37+
test_expect_success 'ls-files does not crash with -h' '
38+
test_expect_code 129 git ls-files -h >usage &&
39+
test_grep "[Uu]sage: git ls-files " usage &&
40+
test_expect_code 129 nongit git ls-files -h >usage &&
41+
test_grep "[Uu]sage: git ls-files " usage
42+
'
43+
3744
test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
3845
mkdir subs &&
3946
ln -s nosuch link &&

0 commit comments

Comments
 (0)