From 7a6a82fba02fb6644647e5beddb54d978918cec0 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 19 May 2026 11:52:14 +0200 Subject: [PATCH] setup: stop using `the_repository` in `set_git_work_tree()` 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 Signed-off-by: Junio C Hamano --- builtin/clone.c | 2 +- builtin/init-db.c | 6 +++--- repository.h | 1 + setup.c | 24 +++++++++++------------- setup.h | 2 +- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index 09f6d97658..8844e3d481 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -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) { diff --git a/builtin/init-db.c b/builtin/init-db.c index bb853e69f5..e626b0d8b7 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -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; diff --git a/repository.h b/repository.h index 832451fc61..d391aff8ab 100644 --- a/repository.h +++ b/repository.h @@ -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 50324f8f37..796ac5792f 100644 --- 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 8fed365637..1a37089fa0 100644 --- 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 {