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 <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 d9c5cfb18f4dff87db7d28846bd754c009f6043d
2 files changed +23 -16
builtin/ls-files.c
+16 -16
@@ -6,7 +6,6 @@
6 * Copyright (C) Linus Torvalds, 2005
7 */
8
9 -#define USE_THE_REPOSITORY_VARIABLE
9 #define DISABLE_SIGN_COMPARE_WARNINGS
10
11 #include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,
244 repo_clear(&subrepo);
245 }
246
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,
249 const enum object_type type, unsigned int padded)
250 {
251 if (type == OBJ_BLOB) {
252 unsigned long size;
253 - if (oid_object_info(the_repository, oid, &size) < 0)
253 + if (oid_object_info(repo, oid, &size) < 0)
254 die(_("could not get object info about '%s'"),
255 oid_to_hex(oid));
256 if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
283 else if (skip_prefix(format, "(objecttype)", &format))
284 strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
285 else if (skip_prefix(format, "(objectsize:padded)", &format))
286 - expand_objectsize(&sb, &ce->oid,
286 + expand_objectsize(repo, &sb, &ce->oid,
287 object_type(ce->ce_mode), 1);
288 else if (skip_prefix(format, "(objectsize)", &format))
289 - expand_objectsize(&sb, &ce->oid,
289 + expand_objectsize(repo, &sb, &ce->oid,
290 object_type(ce->ce_mode), 0);
291 else if (skip_prefix(format, "(stage)", &format))
292 strbuf_addf(&sb, "%d", ce_stage(ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
348 }
349 }
350
351 -static void show_ru_info(struct index_state *istate)
351 +static void show_ru_info(struct repository *repo, struct index_state *istate)
352 {
353 struct string_list_item *item;
354
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)
370 if (!ui->mode[i])
371 continue;
372 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),
374 i + 1);
375 write_name(path);
376 }
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,
567 int cmd_ls_files(int argc,
568 const char **argv,
569 const char *cmd_prefix,
570 - struct repository *repo UNUSED)
570 + struct repository *repo)
571 {
572 int require_work_tree = 0, show_tag = 0, i;
573 char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,
647 show_usage_with_options_if_asked(argc, argv,
648 ls_files_usage, builtin_ls_files_options);
649
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;
652
653 prefix = cmd_prefix;
654 if (prefix)
655 prefix_len = strlen(prefix);
656 - git_config(git_default_config, NULL);
656 + repo_config(repo, git_default_config, NULL);
657
658 - if (repo_read_index(the_repository) < 0)
658 + if (repo_read_index(repo) < 0)
659 die("index file corrupt");
660
661 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc,
724 max_prefix = common_prefix(&pathspec);
725 max_prefix_len = get_common_prefix_len(max_prefix);
726
727 - prune_index(the_repository->index, max_prefix, max_prefix_len);
727 + prune_index(repo->index, max_prefix, max_prefix_len);
728
729 /* Treat unmatching pathspec elements as errors */
730 if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,
748 */
749 if (show_stage || show_unmerged)
750 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);
752 }
753
754 - show_files(the_repository, &dir);
754 + show_files(repo, &dir);
755
756 if (show_resolve_undo)
757 - show_ru_info(the_repository->index);
757 + show_ru_info(repo, repo->index);
758
759 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
760 fprintf(stderr, "Did you forget to 'git add'?\n");
t/t3004-ls-files-basic.sh
+7
@@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' '
34 test_grep "[Uu]sage: git ls-files " broken/usage
35 '
36
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 +
44 test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
45 mkdir subs &&
46 ln -s nosuch link &&