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

index c55517ad94d4470764fa7f97b4e0e06011351558..01bc27904e5fed01e3d8065822044ea22d74a4c5 100644 (file)
@@ -229,6 +229,8 @@ int cmd_init_db(int argc,
 
        if (!is_bare_repository_cfg) {
                const char *git_dir_parent = strrchr(git_dir, '/');
+               char *git_work_tree_cfg = NULL;
+
                if (git_dir_parent) {
                        char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
                        git_work_tree_cfg = real_pathdup(rel, 1);
@@ -243,6 +245,8 @@ int cmd_init_db(int argc,
                if (access(repo_get_work_tree(the_repository), X_OK))
                        die_errno (_("Cannot access work tree '%s'"),
                                   repo_get_work_tree(the_repository));
+
+               free(git_work_tree_cfg);
        }
        else {
                if (real_git_dir)
index fc3ed8bb1c7a66ae9be342c04700478a9af7f19d..4e86335f250f2aee0324f18785b33048d726ccbd 100644 (file)
@@ -100,9 +100,6 @@ int auto_comment_line_char;
 bool warn_on_auto_comment_char;
 #endif /* !WITH_BREAKING_CHANGES */
 
-/* This is set by setup_git_directory_gently() and/or git_default_config() */
-char *git_work_tree_cfg;
-
 /*
  * Repository-local GIT_* environment variables; see environment.h for details.
  */
index ccfcf37bfb9b99a1805355b093008f41b495d196..5d6e4e6c1bc0cfd45948795a7533f48bfef6f296 100644 (file)
@@ -149,7 +149,6 @@ int have_git_dir(void);
 
 extern int is_bare_repository_cfg;
 int is_bare_repository(void);
-extern char *git_work_tree_cfg;
 
 /* Environment bits from configuration mechanism */
 extern int trust_executable_bit;
diff --git a/setup.c b/setup.c
index b4652651dfd454b2288a3e784aa31c3158ea861f..52228b42a15235fd2f4ca3b29408861bde185407 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -31,6 +31,9 @@ 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;