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

From: Junio C Hamano <[email-masked]>
Date: Tue, 19 May 2026 10:22:20 +0900

Patrick Steinhardt <[email-masked]> writes:

>> I do not offhand know if other code paths that are called from this
>> function are thread-safe, but yeah, this use of file-scope static is
>> not a safe thing to do.
>
> In the current status quo this is a safe conversion to do, as we still
> have the assumption ingrained that this only works for a single repo.
> We were using static variables before, and we're still using static
> variables now.
>
> That being said, I wouldn't mind dropping the static variable if this is
> something we'd rather want to get rid of. It's a smell that I'm not
> particularly happy about myself.
>
> I'll send a revised version in a bit, thanks!

OK.  In the v2 (perhaps v3?) series, I can see the removal of two
static strbuf based "optimizations" is the only change since the
previous round (after merging the previous one to the base of the
new iteration and comparing the result with the new iteration).

Looking very good.  Shall we mark the topic for 'next' now?

Thanks.

diff --git w/setup.c c/setup.c
index 1847111616..6aee839d8c 100644
--- w/setup.c
+++ c/setup.c
@@ -471,17 +471,26 @@ int is_nonbare_repository_dir(struct strbuf *path)
 
 int is_inside_git_dir(struct repository *repo)
 {
-	static struct strbuf buf = STRBUF_INIT;
-	return is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
+	struct strbuf buf = STRBUF_INIT;
+	int ret = is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
+	strbuf_release(&buf);
+	return ret;
 }
 
 int is_inside_work_tree(struct repository *repo)
 {
-	static struct strbuf buf = STRBUF_INIT;
-	const char *worktree = repo_get_work_tree(repo);
+	struct strbuf buf = STRBUF_INIT;
+	const char *worktree;
+	int ret;
+
+	worktree = repo_get_work_tree(repo);
 	if (!worktree)
 		return 0;
-	return is_inside_dir(strbuf_realpath(&buf, worktree, 1));
+
+	ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1));
+
+	strbuf_release(&buf);
+	return ret;
 }
 
 void setup_work_tree(struct repository *repo)