]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: remove global `git_work_tree_cfg` variable
authorPatrick Steinhardt <ps@pks.im>
Thu, 11 Jun 2026 06:44:41 +0000 (08:44 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Jun 2026 12:05:54 +0000 (05:05 -0700)
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>
setup.c

diff --git a/setup.c b/setup.c
index 52228b42a15235fd2f4ca3b29408861bde185407..71fc6b33da142b2c294022d33bf0adb2e9fc0240 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -31,9 +31,6 @@ enum allowed_bare_repo {
        ALLOWED_BARE_REPO_ALL,
 };
 
-/* This is set by setup_git_directory_gently() and/or git_default_config() */
-static char *git_work_tree_cfg;
-
 static struct startup_info the_startup_info;
 struct startup_info *startup_info = &the_startup_info;
 const char *tmp_original_cwd;
@@ -799,13 +796,10 @@ static int check_repository_format_gently(const char *gitdir,
        }
 
        if (!has_common) {
-               if (candidate->is_bare != -1) {
+               if (candidate->is_bare != -1)
                        is_bare_repository_cfg = candidate->is_bare;
-               }
-               if (candidate->work_tree) {
-                       free(git_work_tree_cfg);
-                       git_work_tree_cfg = xstrdup(candidate->work_tree);
-               }
+       } else {
+               FREE_AND_NULL(candidate->work_tree);
        }
 
        return 0;
@@ -1145,7 +1139,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
        if (work_tree_env)
                set_git_work_tree(repo, work_tree_env);
        else if (is_bare_repository_cfg > 0) {
-               if (git_work_tree_cfg) {
+               if (repo_fmt->work_tree) {
                        /* #22.2, #30 */
                        warning("core.bare and core.worktree do not make sense");
                        repo->worktree_config_is_bogus = true;
@@ -1156,15 +1150,15 @@ static const char *setup_explicit_git_dir(struct repository *repo,
                free(gitfile);
                return NULL;
        }
-       else if (git_work_tree_cfg) { /* #6, #14 */
-               if (is_absolute_path(git_work_tree_cfg))
-                       set_git_work_tree(repo, git_work_tree_cfg);
+       else if (repo_fmt->work_tree) { /* #6, #14 */
+               if (is_absolute_path(repo_fmt->work_tree))
+                       set_git_work_tree(repo, repo_fmt->work_tree);
                else {
                        char *core_worktree;
                        if (chdir(gitdirenv))
                                die_errno(_("cannot chdir to '%s'"), gitdirenv);
-                       if (chdir(git_work_tree_cfg))
-                               die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg);
+                       if (chdir(repo_fmt->work_tree))
+                               die_errno(_("cannot chdir to '%s'"), repo_fmt->work_tree);
                        core_worktree = xgetcwd();
                        if (chdir(cwd->buf))
                                die_errno(_("cannot come back to cwd"));
@@ -1217,7 +1211,7 @@ static const char *setup_discovered_git_dir(struct repository *repo,
                return NULL;
 
        /* --work-tree is set without --git-dir; use discovered one */
-       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
+       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || repo_fmt->work_tree) {
                char *to_free = NULL;
                const char *ret;
 
@@ -1267,7 +1261,7 @@ static const char *setup_bare_git_dir(struct repository *repo,
        setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
 
        /* --work-tree is set without --git-dir; use discovered one */
-       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
+       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || repo_fmt->work_tree) {
                static const char *gitdir;
 
                gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);