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

[PATCH v2 08/13] setup: drop static `cwd` variable

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 7, 2026, 07:21 UTC
Message-ID
<20260707-pks-setup-split-discovery-and-setup-v2-8-aab372cd227c@pks.im>
In-Reply-To
<20260707-pks-setup-split-discovery-and-setup-v2-0-aab372cd227c@pks.im>

The current working directory is stored as part of a static strbuf variable. This variable had to have a lifetime longer than its containing function because the value we return typically points into that buffer.

In the preceding commit we have moved the prefix into the repository though. Consequently, we can now return the repository's prefix instead of the local one and thus properly manage the lifecycle of this local variable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 setup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/setup.c b/setup.c
index 6cc9fa2de8..f8e4cf100b 100644
--- a/setup.c
+++ b/setup.c
@@ -1933,7 +1933,7 @@ void set_git_work_tree(struct repository *repo, const char *new_work_tree)
 
 const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
 {
-	static struct strbuf cwd = STRBUF_INIT;
+	struct strbuf cwd = STRBUF_INIT;
 	struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
 	struct repo_discovery discovery = REPO_DISCOVERY_INIT;
 	const char *prefix = NULL;
@@ -2116,9 +2116,10 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
 
 	repo_discovery_release(&discovery);
 	strbuf_release(&dir);
+	strbuf_release(&cwd);
 	strbuf_release(&gitdir);
 	strbuf_release(&report);
-	return prefix;
+	return repo->prefix;
 }
 
 int git_config_perm(const char *var, const char *value)
-- 
2.55.0.141.g00534a21ce.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 18 in “setup: split up repository discovery and setup”
  1. 00/13 setup: split up repository discovery and setupPatrick Steinhardt, Jul 7, 2026
  2. 01/13 setup: rename `check_repository_format_gently()`Patrick Steinhardt, Jul 7, 2026
  3. 02/13 setup: mark bogus worktree in `apply_repository_format()`Patrick Steinhardt, Jul 7, 2026
  4. 03/13 setup: unify setup of shallow filePatrick Steinhardt, Jul 7, 2026
  5. 04/13 setup: split up concerns of `setup_git_env_internal()`Patrick Steinhardt, Jul 7, 2026
  6. 05/13 setup: introduce explicit repository discoveryPatrick Steinhardt, Jul 7, 2026
  7. Toon ClaesJul 8, 2026
  8. 06/13 setup: embed repository format in discoveryPatrick Steinhardt, Jul 7, 2026
  9. 08/13 setup: drop static `cwd` variablePatrick Steinhardt, Jul 7, 2026
  10. 07/13 setup: move prefix into repositoryPatrick Steinhardt, Jul 7, 2026
  11. 09/13 setup: propagate prefix via repository discoveryPatrick Steinhardt, Jul 7, 2026
  12. 10/13 setup: make repository discovery self-containedPatrick Steinhardt, Jul 7, 2026
  13. 11/13 setup: drop redundant configuration of `startup_info->have_repository`Patrick Steinhardt, Jul 7, 2026
  14. 12/13 setup: pass worktree to `init_db()`Patrick Steinhardt, Jul 7, 2026
  15. 13/13 setup: mark `set_git_work_tree()` as file-localPatrick Steinhardt, Jul 7, 2026
  16. Justin ToblerJul 7, 2026
  17. Junio C HamanoJul 7, 2026
  18. Toon ClaesJul 8, 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.