]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: drop static `cwd` variable
authorPatrick Steinhardt <ps@pks.im>
Tue, 7 Jul 2026 07:21:27 +0000 (09:21 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 Jul 2026 17:49:40 +0000 (10:49 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c

diff --git a/setup.c b/setup.c
index 6cc9fa2de8c059fd4f354cd13c78327b0017cf84..f8e4cf100b0825eac4875ecd401521271a1e3858 100644 (file)
--- 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)