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>
main
Usman Akinyemi 2025-03-08 05:05:05 +05:30 committed by Junio C Hamano
parent 72fe8bfac8
commit d9c5cfb18f
2 changed files with 23 additions and 16 deletions

View File

@ -6,7 +6,6 @@
* Copyright (C) Linus Torvalds, 2005 * Copyright (C) Linus Torvalds, 2005
*/ */


#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS #define DISABLE_SIGN_COMPARE_WARNINGS


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


static void expand_objectsize(struct strbuf *line, const struct object_id *oid, static void expand_objectsize(struct repository *repo, struct strbuf *line,
const struct object_id *oid,
const enum object_type type, unsigned int padded) const enum object_type type, unsigned int padded)
{ {
if (type == OBJ_BLOB) { if (type == OBJ_BLOB) {
unsigned long size; unsigned long size;
if (oid_object_info(the_repository, oid, &size) < 0) if (oid_object_info(repo, oid, &size) < 0)
die(_("could not get object info about '%s'"), die(_("could not get object info about '%s'"),
oid_to_hex(oid)); oid_to_hex(oid));
if (padded) if (padded)
@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
else if (skip_prefix(format, "(objecttype)", &format)) else if (skip_prefix(format, "(objecttype)", &format))
strbuf_addstr(&sb, type_name(object_type(ce->ce_mode))); strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
else if (skip_prefix(format, "(objectsize:padded)", &format)) else if (skip_prefix(format, "(objectsize:padded)", &format))
expand_objectsize(&sb, &ce->oid, expand_objectsize(repo, &sb, &ce->oid,
object_type(ce->ce_mode), 1); object_type(ce->ce_mode), 1);
else if (skip_prefix(format, "(objectsize)", &format)) else if (skip_prefix(format, "(objectsize)", &format))
expand_objectsize(&sb, &ce->oid, expand_objectsize(repo, &sb, &ce->oid,
object_type(ce->ce_mode), 0); object_type(ce->ce_mode), 0);
else if (skip_prefix(format, "(stage)", &format)) else if (skip_prefix(format, "(stage)", &format))
strbuf_addf(&sb, "%d", ce_stage(ce)); strbuf_addf(&sb, "%d", ce_stage(ce));
@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
} }
} }


static void show_ru_info(struct index_state *istate) static void show_ru_info(struct repository *repo, struct index_state *istate)
{ {
struct string_list_item *item; struct string_list_item *item;


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


prepare_repo_settings(the_repository); prepare_repo_settings(repo);
the_repository->settings.command_requires_full_index = 0; repo->settings.command_requires_full_index = 0;


prefix = cmd_prefix; prefix = cmd_prefix;
if (prefix) if (prefix)
prefix_len = strlen(prefix); prefix_len = strlen(prefix);
git_config(git_default_config, NULL); repo_config(repo, git_default_config, NULL);


if (repo_read_index(the_repository) < 0) if (repo_read_index(repo) < 0)
die("index file corrupt"); die("index file corrupt");


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


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


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


show_files(the_repository, &dir); show_files(repo, &dir);


if (show_resolve_undo) if (show_resolve_undo)
show_ru_info(the_repository->index); show_ru_info(repo, repo->index);


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

View File

@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' '
test_grep "[Uu]sage: git ls-files " broken/usage test_grep "[Uu]sage: git ls-files " broken/usage
' '


test_expect_success 'ls-files does not crash with -h' '
test_expect_code 129 git ls-files -h >usage &&
test_grep "[Uu]sage: git ls-files " usage &&
test_expect_code 129 nongit git ls-files -h >usage &&
test_grep "[Uu]sage: git ls-files " usage
'

test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' ' test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
mkdir subs && mkdir subs &&
ln -s nosuch link && ln -s nosuch link &&