From: Patrick Steinhardt Date: Tue, 19 May 2026 09:52:07 +0000 (+0200) Subject: setup: stop using `the_repository` in `is_inside_work_tree()` X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=8da5ecdb4d72594ee126e949bfe813c0f89fe692;p=thirdparty%2Fgit.git setup: stop using `the_repository` in `is_inside_work_tree()` Similar as with the preceding commit, `is_inside_work_tree()` determines whether the current working directory is located inside the worktree of `the_repository`. Perform the same refactoring by dropping the caching mechanism and injecting the repository that shall be checked. Note that, same as in the preceding commit, we're also resolving the worktree path via `realpath()`. In theory this step is not necessary as we always set the worktree path via `repo_set_worktree()`, and that function already resolves the path for us. But resolving the path a second time is unlikely to matter performance-wise, and it feels fragile to rely on the repository's worktree path being absolute. We thus perform the same extra step even though it's ultimately not required. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- diff --git a/builtin/ls-files.c b/builtin/ls-files.c index b148607f7a..09d95111b3 100644 --- a/builtin/ls-files.c +++ b/builtin/ls-files.c @@ -703,7 +703,7 @@ int cmd_ls_files(int argc, if (dir.exclude_per_dir) exc_given = 1; - if (require_work_tree && !is_inside_work_tree()) + if (require_work_tree && !is_inside_work_tree(repo)) setup_work_tree(); if (recurse_submodules && diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index a216be63cf..2fcd6851d1 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -1006,7 +1006,7 @@ int cmd_rev_parse(int argc, } if (!strcmp(arg, "--show-cdup")) { const char *pfx = prefix; - if (!is_inside_work_tree()) { + if (!is_inside_work_tree(the_repository)) { const char *work_tree = repo_get_work_tree(the_repository); if (work_tree) @@ -1068,7 +1068,7 @@ int cmd_rev_parse(int argc, continue; } if (!strcmp(arg, "--is-inside-work-tree")) { - printf("%s\n", is_inside_work_tree() ? "true" + printf("%s\n", is_inside_work_tree(the_repository) ? "true" : "false"); continue; } diff --git a/object-name.c b/object-name.c index 21dcdc4a0e..37a9ce8e87 100644 --- a/object-name.c +++ b/object-name.c @@ -1703,7 +1703,7 @@ static char *resolve_relative_path(struct repository *r, const char *rel) if (!starts_with(rel, "./") && !starts_with(rel, "../")) return NULL; - if (r != the_repository || !is_inside_work_tree()) + if (r != the_repository || !is_inside_work_tree(the_repository)) die(_("relative path syntax can't be used outside working tree")); /* die() inside prefix_path() if resolved path is outside worktree */ diff --git a/setup.c b/setup.c index 80f3ba0d62..041e08b98d 100644 --- a/setup.c +++ b/setup.c @@ -26,7 +26,6 @@ #include "trace2.h" #include "worktree.h" -static int inside_work_tree = -1; static int work_tree_config_is_bogus; enum allowed_bare_repo { ALLOWED_BARE_REPO_EXPLICIT = 0, @@ -298,7 +297,7 @@ void verify_filename(const char *prefix, */ void verify_non_filename(const char *prefix, const char *arg) { - if (!is_inside_work_tree() || is_inside_git_dir(the_repository)) + if (!is_inside_work_tree(the_repository) || is_inside_git_dir(the_repository)) return; if (*arg == '-') return; /* flag */ @@ -477,11 +476,20 @@ int is_inside_git_dir(struct repository *repo) return ret; } -int is_inside_work_tree(void) +int is_inside_work_tree(struct repository *repo) { - if (inside_work_tree < 0) - inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository)); - return inside_work_tree; + struct strbuf buf = STRBUF_INIT; + const char *worktree; + int ret; + + worktree = repo_get_work_tree(repo); + if (!worktree) + return 0; + + ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1)); + + strbuf_release(&buf); + return ret; } void setup_work_tree(void) @@ -798,13 +806,10 @@ static int check_repository_format_gently(struct repository *repo, if (!has_common) { if (candidate->is_bare != -1) { is_bare_repository_cfg = candidate->is_bare; - if (is_bare_repository_cfg == 1) - inside_work_tree = -1; } if (candidate->work_tree) { free(git_work_tree_cfg); git_work_tree_cfg = xstrdup(candidate->work_tree); - inside_work_tree = -1; } } @@ -1251,7 +1256,6 @@ static const char *setup_discovered_git_dir(struct repository *repo, set_git_work_tree("."); if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) set_git_dir(repo, gitdir, 0); - inside_work_tree = 1; if (offset >= cwd->len) return NULL; @@ -1286,7 +1290,6 @@ static const char *setup_bare_git_dir(struct repository *repo, return setup_explicit_git_dir(repo, gitdir, cwd, repo_fmt, nongit_ok); } - inside_work_tree = 0; if (offset != cwd->len) { if (chdir(cwd->buf)) die_errno(_("cannot come back to cwd")); diff --git a/setup.h b/setup.h index 115bda647c..71d3f91883 100644 --- a/setup.h +++ b/setup.h @@ -5,7 +5,7 @@ #include "string-list.h" int is_inside_git_dir(struct repository *repo); -int is_inside_work_tree(void); +int is_inside_work_tree(struct repository *repo); int get_common_dir_noenv(struct strbuf *sb, const char *gitdir); int get_common_dir(struct strbuf *sb, const char *gitdir); diff --git a/submodule.c b/submodule.c index b1a0363f9d..a939ff5072 100644 --- a/submodule.c +++ b/submodule.c @@ -2620,7 +2620,7 @@ int get_superproject_working_tree(struct strbuf *buf) int code; ssize_t len; - if (!is_inside_work_tree()) + if (!is_inside_work_tree(the_repository)) /* * FIXME: * We might have a superproject, but it is harder