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

[PATCH 03/18] setup: stop using `the_repository` in `is_inside_git_dir()`

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 30, 2026, 13:17 UTC
Message-ID
<20260330-pks-setup-wo-the-repository-v1-3-0d2e822837aa@pks.im>
In-Reply-To
<20260330-pks-setup-wo-the-repository-v1-0-0d2e822837aa@pks.im>

Similar as with the preceding commit, `is_inside_git_dir()` determines whether the current working directory is located inside the gitdir of `the_repository`. Perform the same refactoring by dropping the caching mechanism and injecting the repository that shall be checked.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/rev-parse.c |  2 +-
 setup.c             | 12 ++++--------
 setup.h             |  2 +-
 3 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 9629e1ccf7..6dd49ae3f4 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -1063,7 +1063,7 @@ int cmd_rev_parse(int argc,
 				continue;
 			}
 			if (!strcmp(arg, "--is-inside-git-dir")) {
-				printf("%s\n", is_inside_git_dir() ? "true"
+				printf("%s\n", is_inside_git_dir(the_repository) ? "true"
 						: "false");
 				continue;
 			}
diff --git a/setup.c b/setup.c
index 4df65ba2e8..864ce41116 100644
--- a/setup.c
+++ b/setup.c
@@ -26,7 +26,6 @@
 #include "trace2.h"
 #include "worktree.h"
 
-static int inside_git_dir = -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(the_repository) || is_inside_git_dir())
+	if (!is_inside_work_tree(the_repository) || is_inside_git_dir(the_repository))
 		return;
 	if (*arg == '-')
 		return; /* flag */
@@ -469,11 +468,10 @@ int is_nonbare_repository_dir(struct strbuf *path)
 	return ret;
 }
 
-int is_inside_git_dir(void)
+int is_inside_git_dir(struct repository *repo)
 {
-	if (inside_git_dir < 0)
-		inside_git_dir = is_inside_dir(repo_get_git_dir(the_repository));
-	return inside_git_dir;
+	static struct strbuf buf = STRBUF_INIT;
+	return is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
 }
 
 int is_inside_work_tree(struct repository *repo)
@@ -1249,7 +1247,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_git_dir = 0;
 	if (offset >= cwd->len)
 		return NULL;
 
@@ -1284,7 +1281,6 @@ static const char *setup_bare_git_dir(struct repository *repo,
 		return setup_explicit_git_dir(repo, gitdir, cwd, repo_fmt, nongit_ok);
 	}
 
-	inside_git_dir = 1;
 	if (offset != cwd->len) {
 		if (chdir(cwd->buf))
 			die_errno(_("cannot come back to cwd"));
diff --git a/setup.h b/setup.h
index 7c0aa75319..71d3f91883 100644
--- a/setup.h
+++ b/setup.h
@@ -4,7 +4,7 @@
 #include "refs.h"
 #include "string-list.h"
 
-int is_inside_git_dir(void);
+int is_inside_git_dir(struct repository *repo);
 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);
-- 
2.53.0.1185.g05d4b7b318.dirty
Previous: Karthik NayakNext: Karthik Nayak
Message 5 of 31 in “setup: drop uses of `the_repository`”
  1. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, Mar 30, 2026
  2. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, Mar 30, 2026
  3. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, Mar 30, 2026
  4. Karthik NayakApr 9, 2026
  5. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, Mar 30, 2026
  6. Karthik NayakApr 9, 2026
  7. Patrick SteinhardtApr 13, 2026
  8. Junio C HamanoApr 13, 2026
  9. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, Mar 30, 2026
  10. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, Mar 30, 2026
  11. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, Mar 30, 2026
  12. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, Mar 30, 2026
  13. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, Mar 30, 2026
  14. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, Mar 30, 2026
  15. Elijah NewrenApr 18, 2026
  16. Patrick SteinhardtApr 20, 2026
  17. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, Mar 30, 2026
  18. Elijah NewrenApr 18, 2026
  19. Patrick SteinhardtApr 20, 2026
  20. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, Mar 30, 2026
  21. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, Mar 30, 2026
  22. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, Mar 30, 2026
  23. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, Mar 30, 2026
  24. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, Mar 30, 2026
  25. Elijah NewrenApr 18, 2026
  26. Patrick SteinhardtApr 20, 2026
  27. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, Mar 30, 2026
  28. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, Mar 30, 2026
  29. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, Mar 30, 2026
  30. Karthik NayakApr 9, 2026
  31. Patrick SteinhardtApr 13, 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.