From: Usman Akinyemi <usmanakinyemi202@gmail.com>
To: git@vger.kernel.org, christian.couder@gmail.com
Cc: gitster@pobox.com, johncai86@gmail.com, me@ttaylorr.com,
ps@pks.im, shejialuo@gmail.com, phillip.wood123@gmail.com,
Christian Couder <chriscool@tuxfamily.org>
Subject: [PATCH v4 6/8] builtin/ls-files: stop using `the_repository`
Date: Sat, 8 Mar 2025 05:05:05 +0530 [thread overview]
Message-ID: <20250307233543.1721552-7-usmanakinyemi202@gmail.com> (raw)
In-Reply-To: <20250307233543.1721552-1-usmanakinyemi202@gmail.com>
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>
---
builtin/ls-files.c | 32 ++++++++++++++++----------------
t/t3004-ls-files-basic.sh | 7 +++++++
2 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index a4431429b7..70a377e9c0 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -6,7 +6,6 @@
* Copyright (C) Linus Torvalds, 2005
*/
-#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS
#include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,
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)
{
if (type == OBJ_BLOB) {
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'"),
oid_to_hex(oid));
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))
strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
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);
else if (skip_prefix(format, "(objectsize)", &format))
- expand_objectsize(&sb, &ce->oid,
+ expand_objectsize(repo, &sb, &ce->oid,
object_type(ce->ce_mode), 0);
else if (skip_prefix(format, "(stage)", &format))
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;
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)
if (!ui->mode[i])
continue;
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);
write_name(path);
}
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,
int cmd_ls_files(int argc,
const char **argv,
const char *cmd_prefix,
- struct repository *repo UNUSED)
+ struct repository *repo)
{
int require_work_tree = 0, show_tag = 0, i;
char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,
show_usage_with_options_if_asked(argc, argv,
ls_files_usage, builtin_ls_files_options);
- prepare_repo_settings(the_repository);
- the_repository->settings.command_requires_full_index = 0;
+ prepare_repo_settings(repo);
+ repo->settings.command_requires_full_index = 0;
prefix = cmd_prefix;
if (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");
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_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 */
if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,
*/
if (show_stage || show_unmerged)
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)
- show_ru_info(the_repository->index);
+ show_ru_info(repo, repo->index);
if (ps_matched && report_path_error(ps_matched, &pathspec)) {
fprintf(stderr, "Did you forget to 'git add'?\n");
diff --git a/t/t3004-ls-files-basic.sh b/t/t3004-ls-files-basic.sh
index a1078f8701..4034a5a59f 100755
--- a/t/t3004-ls-files-basic.sh
+++ b/t/t3004-ls-files-basic.sh
@@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' '
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' '
mkdir subs &&
ln -s nosuch link &&
--
2.48.1
next prev parent reply other threads:[~2025-03-07 23:36 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-14 22:57 [PATCH 0/7][Outreachy] stop using the_repository global variable Usman Akinyemi
2025-02-14 22:57 ` [PATCH 1/7] builtin/verify-tag: stop using `the_repository` Usman Akinyemi
2025-02-17 6:55 ` Patrick Steinhardt
2025-02-17 10:05 ` Usman Akinyemi
2025-02-17 10:22 ` Patrick Steinhardt
2025-02-17 10:42 ` Usman Akinyemi
2025-02-17 15:47 ` Patrick Steinhardt
2025-02-14 22:57 ` [PATCH 2/7] builtin/verify-commit.c: " Usman Akinyemi
2025-02-16 5:32 ` shejialuo
2025-02-17 8:55 ` Usman Akinyemi
2025-02-14 22:57 ` [PATCH 3/7] builtin/send-pack.c: " Usman Akinyemi
2025-02-14 22:57 ` [PATCH 4/7] builtin/pack-refs: " Usman Akinyemi
2025-02-14 22:57 ` [PATCH 5/7] builtin/ls-files: " Usman Akinyemi
2025-02-17 6:55 ` Patrick Steinhardt
2025-02-17 8:57 ` Usman Akinyemi
2025-02-14 22:57 ` [PATCH 6/7] builtin/for-each-ref: " Usman Akinyemi
2025-02-14 22:57 ` [PATCH 7/7] builtin/checkout-index.c: " Usman Akinyemi
2025-02-16 5:41 ` [PATCH 0/7][Outreachy] stop using the_repository global variable shejialuo
2025-02-17 8:56 ` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 00/12][Outreachy] " Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 01/12] builtin/verify-tag: refactor `cmd_verify_tag()` Usman Akinyemi
2025-02-20 15:32 ` Junio C Hamano
2025-02-19 20:32 ` [PATCH v2 02/12] builtin/verify-tag: stop using `the_repository` Usman Akinyemi
2025-02-20 15:43 ` Junio C Hamano
2025-02-27 17:56 ` Usman Akinyemi
2025-02-27 22:39 ` Junio C Hamano
2025-02-19 20:32 ` [PATCH v2 03/12] builtin/verify-commit: refactor `cmd_verify_commit()` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 04/12] builtin/verify-commit: stop using `the_repository` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 05/12] builtin/send-pack: refactor `cmd_send_pack()` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 06/12] builtin/send-pack: stop using `the_repository` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 07/12] builtin/pack-refs: refactor `cmd_pack_refs()` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 08/12] builtin/pack-refs: stop using `the_repository` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 09/12] builtin/ls-files: " Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 10/12] builtin/for-each-ref: refactor `cmd_for_each_ref()` Usman Akinyemi
2025-02-19 20:32 ` [PATCH v2 11/12] builtin/for-each-ref: stop using `the_repository` Usman Akinyemi
2025-02-19 20:33 ` [PATCH v2 12/12] builtin/checkout-index: " Usman Akinyemi
2025-03-06 14:35 ` [Outreachy PATCH v3 0/8] stop using the_repository global variable Usman Akinyemi
2025-03-06 14:35 ` [PATCH v3 1/8] config: teach repo_config to allow `repo` to be NULL Usman Akinyemi
2025-03-06 17:53 ` Junio C Hamano
2025-03-07 1:33 ` Usman Akinyemi
2025-03-07 10:37 ` Phillip Wood
2025-03-06 14:35 ` [PATCH v3 2/8] builtin/verify-tag: stop using `the_repository` Usman Akinyemi
2025-03-06 17:56 ` Junio C Hamano
2025-03-06 14:35 ` [PATCH v3 3/8] builtin/verify-commit: " Usman Akinyemi
2025-03-06 14:35 ` [PATCH v3 4/8] builtin/send-pack: " Usman Akinyemi
2025-03-06 14:35 ` [PATCH v3 5/8] builtin/pack-refs: " Usman Akinyemi
2025-03-06 14:35 ` [PATCH v3 6/8] builtin/ls-files: " Usman Akinyemi
2025-03-06 17:59 ` Junio C Hamano
2025-03-06 14:35 ` [PATCH v3 7/8] builtin/for-each-ref: " Usman Akinyemi
2025-03-06 14:35 ` [PATCH v3 8/8] builtin/checkout-index: " Usman Akinyemi
2025-03-06 18:18 ` Junio C Hamano
2025-03-07 1:15 ` Usman Akinyemi
2025-03-07 14:15 ` Junio C Hamano
2025-03-07 19:41 ` Usman Akinyemi
2025-03-07 23:34 ` [Outreachy PATCH v4 0/8] stop using the_repository global variable Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 1/8] config: teach repo_config to allow `repo` to be NULL Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 2/8] builtin/verify-tag: stop using `the_repository` Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 3/8] builtin/verify-commit: " Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 4/8] builtin/send-pack: " Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 5/8] builtin/pack-refs: " Usman Akinyemi
2025-03-07 23:35 ` Usman Akinyemi [this message]
2025-03-07 23:35 ` [PATCH v4 7/8] builtin/for-each-ref: " Usman Akinyemi
2025-03-07 23:35 ` [PATCH v4 8/8] builtin/checkout-index: " Usman Akinyemi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250307233543.1721552-7-usmanakinyemi202@gmail.com \
--to=usmanakinyemi202@gmail.com \
--cc=chriscool@tuxfamily.org \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johncai86@gmail.com \
--cc=me@ttaylorr.com \
--cc=phillip.wood123@gmail.com \
--cc=ps@pks.im \
--cc=shejialuo@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).