[PATCH 02/18] setup: stop using `the_repository` in `is_inside_worktree()`
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Mar 30, 2026, 13:17 UTC
- Message-ID
- <20260330-pks-setup-wo-the-repository-v1-2-0d2e822837aa@pks.im>
- In-Reply-To
- <20260330-pks-setup-wo-the-repository-v1-0-0d2e822837aa@pks.im>
The function `is_inside_worktree()` verifies whether or not the current working directory is located inside the worktree of `the_repository`. This is done by taking the worktree path and verifying that it's a prefix of the current working directory.
This information is cached so that we don't have to re-do this change multiple times. Furthermore, we proactively set the value in multiple locations so that we don't even have to perform the check when we have discovered the repository.
While we could simply move the caching variable into the repository, the current layout doesn't really feel sensible in the first place:
- It can easily lead to false positives or negatives if at any point
in time we may switch the current working directory. - We don't call the function in a hot loop, and neither is it overly
expensive to compute.Drop the caching infrastructure and instead compute the property ad-hoc via an injected repository.
Note that there is one small gotcha: we sometimes may end up with relative directory paths, and if so `is_inside_dir()` might fail. This wasn't an issue before because of how we proactively set the cached value during repository discovery. Now that we stop doing that it becomes a problem though, but it is worked around by resolving the repository directory via `realpath()`.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- builtin/ls-files.c | 2 +- builtin/rev-parse.c | 4 ++-- object-name.c | 2 +- setup.c | 18 +++++++----------- setup.h | 2 +- submodule.c | 2 +- 6 files changed, 13 insertions(+), 17 deletions(-)
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 01a62800e8..9629e1ccf7 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 e5adec4c9d..7b7e546b1f 100644 --- a/object-name.c +++ b/object-name.c @@ -1990,7 +1990,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 ba2898473a..4df65ba2e8 100644 --- a/setup.c +++ b/setup.c @@ -27,7 +27,6 @@ #include "worktree.h" static int inside_git_dir = -1; -static int inside_work_tree = -1; static int work_tree_config_is_bogus; enum allowed_bare_repo { ALLOWED_BARE_REPO_EXPLICIT = 0, @@ -299,7 +298,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()) + if (!is_inside_work_tree(the_repository) || is_inside_git_dir()) return; if (*arg == '-') return; /* flag */ @@ -477,11 +476,13 @@ int is_inside_git_dir(void) return inside_git_dir; } -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; + static struct strbuf buf = STRBUF_INIT; + const char *worktree = repo_get_work_tree(repo); + if (!worktree) + return 0; + return is_inside_dir(strbuf_realpath(&buf, worktree, 1)); } void setup_work_tree(void) @@ -798,13 +799,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; } } @@ -1252,7 +1250,6 @@ static const char *setup_discovered_git_dir(struct repository *repo, if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) set_git_dir(repo, gitdir, 0); inside_git_dir = 0; - inside_work_tree = 1; if (offset >= cwd->len) return NULL; @@ -1288,7 +1285,6 @@ static const char *setup_bare_git_dir(struct repository *repo, } inside_git_dir = 1; - 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 80bc6e5f07..7c0aa75319 100644 --- a/setup.h +++ b/setup.h @@ -5,7 +5,7 @@ #include "string-list.h" int is_inside_git_dir(void); -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 e20537ba8d..46116cee88 100644 --- a/submodule.c +++ b/submodule.c @@ -2622,7 +2622,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
-- 2.53.0.1185.g05d4b7b318.dirty