builtin/init: stop modifying global `git_work_tree_cfg` variable

When executing git-init(1) we need to figure out the final location of the worktree. This location can be configured in a couple of ways: via an environment variable, via the preexisting "core.worktree" config in case we're reinitializing, or implicitly when reinitializing a non-bare repository. When checking for the worktree location in "builtin/init-db.c" we populate any potentially-discovered value both by setting the global `git_work_tree_cfg` variable and via `set_git_work_tree()`, which ultimately ends up modifying `struct repository::worktree`. Modifying `git_work_tree_cfg` is unnecessary though: we configure the worktree in `create_default_files()`, and that function derives the worktree location via `repo_get_work_tree()`. Consequently, propagating the worktree via `set_git_work_tree()` is sufficient. Stop munging `git_work_tree_cfg` and make it file-local to "setup.c" and function-local to `cmd_init_db()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 11, 2026 at 08:44 UTC 9bef2cf33111dc844e46a8a19c266320774f4bd6
4 files changed +7 -4
builtin/init-db.c
+4
@@ -229,6 +229,8 @@ int cmd_init_db(int argc,
229
230 if (!is_bare_repository_cfg) {
231 const char *git_dir_parent = strrchr(git_dir, '/');
232 + char *git_work_tree_cfg = NULL;
233 +
234 if (git_dir_parent) {
235 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
236 git_work_tree_cfg = real_pathdup(rel, 1);
@@ -243,6 +245,8 @@ int cmd_init_db(int argc,
245 if (access(repo_get_work_tree(the_repository), X_OK))
246 die_errno (_("Cannot access work tree '%s'"),
247 repo_get_work_tree(the_repository));
248 +
249 + free(git_work_tree_cfg);
250 }
251 else {
252 if (real_git_dir)
environment.c
-3
@@ -100,9 +100,6 @@ int auto_comment_line_char;
100 bool warn_on_auto_comment_char;
101 #endif /* !WITH_BREAKING_CHANGES */
102
103 -/* This is set by setup_git_directory_gently() and/or git_default_config() */
104 -char *git_work_tree_cfg;
105 -
103 /*
104 * Repository-local GIT_* environment variables; see environment.h for details.
105 */
environment.h
-1
@@ -149,7 +149,6 @@ int have_git_dir(void);
149
150 extern int is_bare_repository_cfg;
151 int is_bare_repository(void);
152 -extern char *git_work_tree_cfg;
152
153 /* Environment bits from configuration mechanism */
154 extern int trust_executable_bit;
setup.c
+3
@@ -31,6 +31,9 @@ enum allowed_bare_repo {
31 ALLOWED_BARE_REPO_ALL,
32 };
33
34 +/* This is set by setup_git_directory_gently() and/or git_default_config() */
35 +static char *git_work_tree_cfg;
36 +
37 static struct startup_info the_startup_info;
38 struct startup_info *startup_info = &the_startup_info;
39 const char *tmp_original_cwd;