setup: remove global `git_work_tree_cfg` variable

The global `git_work_tree_cfg` variable used to be modified by both "setup.c" and by "builtin/init-db.c". We have refactored the latter user to not use that variable at all anymore in a preceding commit, which makes "setup.c" the only remaining user. Even for "setup.c" it is unnecessary though, as we only ever set it to the value we have stored in the discovered repository format. The consequence is that we only ever set it in case we already have it set to the same value in our discovered repository format, which makes it redundant. Refactor the code so that we instead use the worktree configuration as discovered via the repository format. Drop the global variable. Note that in `check_repository_format_gently()` we now have to free the candidate work tree variable. This change is required to retain previous semantics: before we essentially had an implicit `else` branch where we set `git_work_tree_cfg = NULL`, but we were able to elide that branch because we already knew that it would be `NULL` anyway. Now that we use the candidate work tree directly to populate the repository's work tree though we have to clear it to retain those semantics. 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 85f5f504f046bccf86b78ba02064a4b013d7264f
1 file changed +11 -17
setup.c
+11 -17
@@ -31,9 +31,6 @@ 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 -
34 static struct startup_info the_startup_info;
35 struct startup_info *startup_info = &the_startup_info;
36 const char *tmp_original_cwd;
@@ -799,13 +796,10 @@ static int check_repository_format_gently(const char *gitdir,
796 }
797
798 if (!has_common) {
802 - if (candidate->is_bare != -1) {
799 + if (candidate->is_bare != -1)
800 is_bare_repository_cfg = candidate->is_bare;
804 - }
805 - if (candidate->work_tree) {
806 - free(git_work_tree_cfg);
807 - git_work_tree_cfg = xstrdup(candidate->work_tree);
808 - }
801 + } else {
802 + FREE_AND_NULL(candidate->work_tree);
803 }
804
805 return 0;
@@ -1145,7 +1139,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1139 if (work_tree_env)
1140 set_git_work_tree(repo, work_tree_env);
1141 else if (is_bare_repository_cfg > 0) {
1148 - if (git_work_tree_cfg) {
1142 + if (repo_fmt->work_tree) {
1143 /* #22.2, #30 */
1144 warning("core.bare and core.worktree do not make sense");
1145 repo->worktree_config_is_bogus = true;
@@ -1156,15 +1150,15 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1150 free(gitfile);
1151 return NULL;
1152 }
1159 - else if (git_work_tree_cfg) { /* #6, #14 */
1160 - if (is_absolute_path(git_work_tree_cfg))
1161 - set_git_work_tree(repo, git_work_tree_cfg);
1153 + else if (repo_fmt->work_tree) { /* #6, #14 */
1154 + if (is_absolute_path(repo_fmt->work_tree))
1155 + set_git_work_tree(repo, repo_fmt->work_tree);
1156 else {
1157 char *core_worktree;
1158 if (chdir(gitdirenv))
1159 die_errno(_("cannot chdir to '%s'"), gitdirenv);
1166 - if (chdir(git_work_tree_cfg))
1167 - die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg);
1160 + if (chdir(repo_fmt->work_tree))
1161 + die_errno(_("cannot chdir to '%s'"), repo_fmt->work_tree);
1162 core_worktree = xgetcwd();
1163 if (chdir(cwd->buf))
1164 die_errno(_("cannot come back to cwd"));
@@ -1217,7 +1211,7 @@ static const char *setup_discovered_git_dir(struct repository *repo,
1211 return NULL;
1212
1213 /* --work-tree is set without --git-dir; use discovered one */
1220 - if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
1214 + if (getenv(GIT_WORK_TREE_ENVIRONMENT) || repo_fmt->work_tree) {
1215 char *to_free = NULL;
1216 const char *ret;
1217
@@ -1267,7 +1261,7 @@ static const char *setup_bare_git_dir(struct repository *repo,
1261 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
1262
1263 /* --work-tree is set without --git-dir; use discovered one */
1270 - if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
1264 + if (getenv(GIT_WORK_TREE_ENVIRONMENT) || repo_fmt->work_tree) {
1265 static const char *gitdir;
1266
1267 gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);