git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 02/18] setup: stop using `the_repository` in `is_inside_worktree()`

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 20, 2026, 08:22 UTC
Message-ID
<20260420-pks-setup-wo-the-repository-v1-2-f4a81c4988e8@pks.im>
In-Reply-To
<20260420-pks-setup-wo-the-repository-v1-0-f4a81c4988e8@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.54.0.rc2.529.gd9106f7525.dirty
Previous: Patrick SteinhardtNext: Tian Yuchen
Message 3 of 73 in “setup: drop uses of `the_repository`”
  1. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, Apr 20, 2026
  2. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, Apr 20, 2026
  3. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, Apr 20, 2026
  4. Tian YuchenMay 17, 2026
  5. Junio C HamanoMay 17, 2026
  6. Patrick SteinhardtMay 18, 2026
  7. Junio C HamanoMay 19, 2026
  8. Patrick SteinhardtMay 19, 2026
  9. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, Apr 20, 2026
  10. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, Apr 20, 2026
  11. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, Apr 20, 2026
  12. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, Apr 20, 2026
  13. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, Apr 20, 2026
  14. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, Apr 20, 2026
  15. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, Apr 20, 2026
  16. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, Apr 20, 2026
  17. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, Apr 20, 2026
  18. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, Apr 20, 2026
  19. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, Apr 20, 2026
  20. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, Apr 20, 2026
  21. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, Apr 20, 2026
  22. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, Apr 20, 2026
  23. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, Apr 20, 2026
  24. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, Apr 20, 2026
  25. Elijah NewrenApr 20, 2026
  26. Patrick SteinhardtApr 21, 2026
  27. Junio C HamanoApr 20, 2026
  28. Patrick SteinhardtApr 21, 2026
  29. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 18, 2026
  30. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 18, 2026
  31. Karthik NayakMay 19, 2026
  32. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, May 18, 2026
  33. Karthik NayakMay 19, 2026
  34. Patrick SteinhardtMay 19, 2026
  35. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 18, 2026
  36. Karthik NayakMay 19, 2026
  37. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 18, 2026
  38. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 18, 2026
  39. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 18, 2026
  40. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 18, 2026
  41. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 18, 2026
  42. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 18, 2026
  43. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 18, 2026
  44. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 18, 2026
  45. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 18, 2026
  46. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 18, 2026
  47. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 18, 2026
  48. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 18, 2026
  49. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 18, 2026
  50. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 18, 2026
  51. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 18, 2026
  52. Karthik NayakMay 19, 2026
  53. Patrick SteinhardtMay 19, 2026
  54. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 19, 2026
  55. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 19, 2026
  56. 02/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 19, 2026
  57. 03/18 setup: stop using `the_repository` in `is_inside_work_tree()`Patrick Steinhardt, May 19, 2026
  58. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 19, 2026
  59. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 19, 2026
  60. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 19, 2026
  61. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 19, 2026
  62. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 19, 2026
  63. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 19, 2026
  64. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 19, 2026
  65. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 19, 2026
  66. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 19, 2026
  67. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 19, 2026
  68. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 19, 2026
  69. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 19, 2026
  70. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 19, 2026
  71. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 19, 2026
  72. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 19, 2026
  73. Junio C HamanoMay 20, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.