]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: stop using `the_repository` in `set_git_work_tree()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 19 May 2026 09:52:14 +0000 (11:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 May 2026 10:36:24 +0000 (19:36 +0900)
Stop using `the_repository` in `set_git_work_tree()` and instead accept
the repository as a parameter. The injection of `the_repository` is thus
bumped one level higher, where callers now pass it in explicitly.

Similar as with the preceding commit, we track whether the worktree has
been initialized already via a global variable so that we can die in
case the repository is re-initialized with a different worktree path.
Store this info in the `struct repository` instead so that we correctly
handle this per repository.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
builtin/init-db.c
repository.h
setup.c
setup.h

index 09f6d976587cab3e1c749c90c4e72e1c8efc9029..8844e3d4811281daac0ca3d0f7eb3391868636fd 100644 (file)
@@ -1116,7 +1116,7 @@ int cmd_clone(int argc,
                        die_errno(_("could not create work tree dir '%s'"),
                                  work_tree);
                junk_work_tree = work_tree;
-               set_git_work_tree(work_tree);
+               set_git_work_tree(the_repository, work_tree);
        }
 
        if (real_git_dir) {
index bb853e69f5426ea6e8e570bcd5041c0c43d07683..e626b0d8b7ccc6366f632e38ee064db2843e15c0 100644 (file)
@@ -237,9 +237,9 @@ int cmd_init_db(int argc,
                if (!git_work_tree_cfg)
                        git_work_tree_cfg = xgetcwd();
                if (work_tree)
-                       set_git_work_tree(work_tree);
+                       set_git_work_tree(the_repository, work_tree);
                else
-                       set_git_work_tree(git_work_tree_cfg);
+                       set_git_work_tree(the_repository, git_work_tree_cfg);
                if (access(repo_get_work_tree(the_repository), X_OK))
                        die_errno (_("Cannot access work tree '%s'"),
                                   repo_get_work_tree(the_repository));
@@ -248,7 +248,7 @@ int cmd_init_db(int argc,
                if (real_git_dir)
                        die(_("--separate-git-dir incompatible with bare repository"));
                if (work_tree)
-                       set_git_work_tree(work_tree);
+                       set_git_work_tree(the_repository, work_tree);
        }
 
        flags |= INIT_DB_EXIST_OK;
index 832451fc6165938310d0c8311aef98046006ba73..d391aff8ab4a9086f45d38842c693ee524959a32 100644 (file)
@@ -114,6 +114,7 @@ struct repository {
         * A NULL value indicates that there is no working directory.
         */
        char *worktree;
+       bool worktree_initialized;
        bool worktree_config_is_bogus;
 
        /*
diff --git a/setup.c b/setup.c
index 50324f8f37081aec71ef0b625a17a34d501f7428..796ac5792fe9fded941fb7ccf873f2bd2607c88d 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1152,7 +1152,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
 
        /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
        if (work_tree_env)
-               set_git_work_tree(work_tree_env);
+               set_git_work_tree(repo, work_tree_env);
        else if (is_bare_repository_cfg > 0) {
                if (git_work_tree_cfg) {
                        /* #22.2, #30 */
@@ -1167,7 +1167,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
        }
        else if (git_work_tree_cfg) { /* #6, #14 */
                if (is_absolute_path(git_work_tree_cfg))
-                       set_git_work_tree(git_work_tree_cfg);
+                       set_git_work_tree(repo, git_work_tree_cfg);
                else {
                        char *core_worktree;
                        if (chdir(gitdirenv))
@@ -1177,7 +1177,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
                        core_worktree = xgetcwd();
                        if (chdir(cwd->buf))
                                die_errno(_("cannot come back to cwd"));
-                       set_git_work_tree(core_worktree);
+                       set_git_work_tree(repo, core_worktree);
                        free(core_worktree);
                }
        }
@@ -1188,7 +1188,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
                return NULL;
        }
        else /* #2, #10 */
-               set_git_work_tree(".");
+               set_git_work_tree(repo, ".");
 
        /* set_git_work_tree() must have been called by now */
        worktree = repo_get_work_tree(repo);
@@ -1248,7 +1248,7 @@ static const char *setup_discovered_git_dir(struct repository *repo,
        }
 
        /* #0, #1, #5, #8, #9, #12, #13 */
-       set_git_work_tree(".");
+       set_git_work_tree(repo, ".");
        if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
                set_git_dir(repo, gitdir, 0);
        if (offset >= cwd->len)
@@ -1839,29 +1839,27 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
        return NULL;
 }
 
-static int git_work_tree_initialized;
-
 /*
  * Note.  This works only before you used a work tree.  This was added
  * primarily to support git-clone to work in a new repository it just
  * created, and is not meant to flip between different work trees.
  */
-void set_git_work_tree(const char *new_work_tree)
+void set_git_work_tree(struct repository *repo, const char *new_work_tree)
 {
-       if (git_work_tree_initialized) {
+       if (repo->worktree_initialized) {
                struct strbuf realpath = STRBUF_INIT;
 
                strbuf_realpath(&realpath, new_work_tree, 1);
                new_work_tree = realpath.buf;
-               if (strcmp(new_work_tree, the_repository->worktree))
+               if (strcmp(new_work_tree, repo->worktree))
                        die("internal error: work tree has already been set\n"
                            "Current worktree: %s\nNew worktree: %s",
-                           the_repository->worktree, new_work_tree);
+                           repo->worktree, new_work_tree);
                strbuf_release(&realpath);
                return;
        }
-       git_work_tree_initialized = 1;
-       repo_set_worktree(the_repository, new_work_tree);
+       repo->worktree_initialized = true;
+       repo_set_worktree(repo, new_work_tree);
 }
 
 const char *setup_git_directory_gently(int *nongit_ok)
diff --git a/setup.h b/setup.h
index 8fed365637ec2bf32a6ee75818ec31f0288a3421..1a37089fa0aa54b1b77fddcbf82afb5630a6bf1b 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -96,7 +96,7 @@ static inline int discover_git_directory(struct strbuf *commondir,
        return 0;
 }
 
-void set_git_work_tree(const char *tree);
+void set_git_work_tree(struct repository *repo, const char *tree);
 
 /* Flags that can be passed to `enter_repo()`. */
 enum {