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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 19, 2026 at 11:52 UTC 8da5ecdb4d72594ee126e949bfe813c0f89fe692
6 files changed +20 -17
builtin/ls-files.c
+1 -1
@@ -703,7 +703,7 @@ int cmd_ls_files(int argc,
703 if (dir.exclude_per_dir)
704 exc_given = 1;
705
706 - if (require_work_tree && !is_inside_work_tree())
706 + if (require_work_tree && !is_inside_work_tree(repo))
707 setup_work_tree();
708
709 if (recurse_submodules &&
builtin/rev-parse.c
+2 -2
@@ -1006,7 +1006,7 @@ int cmd_rev_parse(int argc,
1006 }
1007 if (!strcmp(arg, "--show-cdup")) {
1008 const char *pfx = prefix;
1009 - if (!is_inside_work_tree()) {
1009 + if (!is_inside_work_tree(the_repository)) {
1010 const char *work_tree =
1011 repo_get_work_tree(the_repository);
1012 if (work_tree)
@@ -1068,7 +1068,7 @@ int cmd_rev_parse(int argc,
1068 continue;
1069 }
1070 if (!strcmp(arg, "--is-inside-work-tree")) {
1071 - printf("%s\n", is_inside_work_tree() ? "true"
1071 + printf("%s\n", is_inside_work_tree(the_repository) ? "true"
1072 : "false");
1073 continue;
1074 }
object-name.c
+1 -1
@@ -1703,7 +1703,7 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
1703 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1704 return NULL;
1705
1706 - if (r != the_repository || !is_inside_work_tree())
1706 + if (r != the_repository || !is_inside_work_tree(the_repository))
1707 die(_("relative path syntax can't be used outside working tree"));
1708
1709 /* die() inside prefix_path() if resolved path is outside worktree */
setup.c
+14 -11
@@ -26,7 +26,6 @@
26 #include "trace2.h"
27 #include "worktree.h"
28
29 -static int inside_work_tree = -1;
29 static int work_tree_config_is_bogus;
30 enum allowed_bare_repo {
31 ALLOWED_BARE_REPO_EXPLICIT = 0,
@@ -298,7 +297,7 @@ void verify_filename(const char *prefix,
297 */
298 void verify_non_filename(const char *prefix, const char *arg)
299 {
301 - if (!is_inside_work_tree() || is_inside_git_dir(the_repository))
300 + if (!is_inside_work_tree(the_repository) || is_inside_git_dir(the_repository))
301 return;
302 if (*arg == '-')
303 return; /* flag */
@@ -477,11 +476,20 @@ int is_inside_git_dir(struct repository *repo)
476 return ret;
477 }
478
480 -int is_inside_work_tree(void)
479 +int is_inside_work_tree(struct repository *repo)
480 {
482 - if (inside_work_tree < 0)
483 - inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
484 - return inside_work_tree;
481 + struct strbuf buf = STRBUF_INIT;
482 + const char *worktree;
483 + int ret;
484 +
485 + worktree = repo_get_work_tree(repo);
486 + if (!worktree)
487 + return 0;
488 +
489 + ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1));
490 +
491 + strbuf_release(&buf);
492 + return ret;
493 }
494
495 void setup_work_tree(void)
@@ -798,13 +806,10 @@ static int check_repository_format_gently(struct repository *repo,
806 if (!has_common) {
807 if (candidate->is_bare != -1) {
808 is_bare_repository_cfg = candidate->is_bare;
801 - if (is_bare_repository_cfg == 1)
802 - inside_work_tree = -1;
809 }
810 if (candidate->work_tree) {
811 free(git_work_tree_cfg);
812 git_work_tree_cfg = xstrdup(candidate->work_tree);
807 - inside_work_tree = -1;
813 }
814 }
815
@@ -1251,7 +1256,6 @@ static const char *setup_discovered_git_dir(struct repository *repo,
1256 set_git_work_tree(".");
1257 if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
1258 set_git_dir(repo, gitdir, 0);
1254 - inside_work_tree = 1;
1259 if (offset >= cwd->len)
1260 return NULL;
1261
@@ -1286,7 +1290,6 @@ static const char *setup_bare_git_dir(struct repository *repo,
1290 return setup_explicit_git_dir(repo, gitdir, cwd, repo_fmt, nongit_ok);
1291 }
1292
1289 - inside_work_tree = 0;
1293 if (offset != cwd->len) {
1294 if (chdir(cwd->buf))
1295 die_errno(_("cannot come back to cwd"));
setup.h
+1 -1
@@ -5,7 +5,7 @@
5 #include "string-list.h"
6
7 int is_inside_git_dir(struct repository *repo);
8 -int is_inside_work_tree(void);
8 +int is_inside_work_tree(struct repository *repo);
9 int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
10 int get_common_dir(struct strbuf *sb, const char *gitdir);
11
submodule.c
+1 -1
@@ -2620,7 +2620,7 @@ int get_superproject_working_tree(struct strbuf *buf)
2620 int code;
2621 ssize_t len;
2622
2623 - if (!is_inside_work_tree())
2623 + if (!is_inside_work_tree(the_repository))
2624 /*
2625 * FIXME:
2626 * We might have a superproject, but it is harder