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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 19, 2026 at 11:52 UTC 7a6a82fba02fb6644647e5beddb54d978918cec0
5 files changed +17 -18
builtin/clone.c
+1 -1
@@ -1116,7 +1116,7 @@ int cmd_clone(int argc,
1116 die_errno(_("could not create work tree dir '%s'"),
1117 work_tree);
1118 junk_work_tree = work_tree;
1119 - set_git_work_tree(work_tree);
1119 + set_git_work_tree(the_repository, work_tree);
1120 }
1121
1122 if (real_git_dir) {
builtin/init-db.c
+3 -3
@@ -237,9 +237,9 @@ int cmd_init_db(int argc,
237 if (!git_work_tree_cfg)
238 git_work_tree_cfg = xgetcwd();
239 if (work_tree)
240 - set_git_work_tree(work_tree);
240 + set_git_work_tree(the_repository, work_tree);
241 else
242 - set_git_work_tree(git_work_tree_cfg);
242 + set_git_work_tree(the_repository, git_work_tree_cfg);
243 if (access(repo_get_work_tree(the_repository), X_OK))
244 die_errno (_("Cannot access work tree '%s'"),
245 repo_get_work_tree(the_repository));
@@ -248,7 +248,7 @@ int cmd_init_db(int argc,
248 if (real_git_dir)
249 die(_("--separate-git-dir incompatible with bare repository"));
250 if (work_tree)
251 - set_git_work_tree(work_tree);
251 + set_git_work_tree(the_repository, work_tree);
252 }
253
254 flags |= INIT_DB_EXIST_OK;
repository.h
+1
@@ -114,6 +114,7 @@ struct repository {
114 * A NULL value indicates that there is no working directory.
115 */
116 char *worktree;
117 + bool worktree_initialized;
118 bool worktree_config_is_bogus;
119
120 /*
setup.c
+11 -13
@@ -1152,7 +1152,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1152
1153 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
1154 if (work_tree_env)
1155 - set_git_work_tree(work_tree_env);
1155 + set_git_work_tree(repo, work_tree_env);
1156 else if (is_bare_repository_cfg > 0) {
1157 if (git_work_tree_cfg) {
1158 /* #22.2, #30 */
@@ -1167,7 +1167,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1167 }
1168 else if (git_work_tree_cfg) { /* #6, #14 */
1169 if (is_absolute_path(git_work_tree_cfg))
1170 - set_git_work_tree(git_work_tree_cfg);
1170 + set_git_work_tree(repo, git_work_tree_cfg);
1171 else {
1172 char *core_worktree;
1173 if (chdir(gitdirenv))
@@ -1177,7 +1177,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1177 core_worktree = xgetcwd();
1178 if (chdir(cwd->buf))
1179 die_errno(_("cannot come back to cwd"));
1180 - set_git_work_tree(core_worktree);
1180 + set_git_work_tree(repo, core_worktree);
1181 free(core_worktree);
1182 }
1183 }
@@ -1188,7 +1188,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1188 return NULL;
1189 }
1190 else /* #2, #10 */
1191 - set_git_work_tree(".");
1191 + set_git_work_tree(repo, ".");
1192
1193 /* set_git_work_tree() must have been called by now */
1194 worktree = repo_get_work_tree(repo);
@@ -1248,7 +1248,7 @@ static const char *setup_discovered_git_dir(struct repository *repo,
1248 }
1249
1250 /* #0, #1, #5, #8, #9, #12, #13 */
1251 - set_git_work_tree(".");
1251 + set_git_work_tree(repo, ".");
1252 if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
1253 set_git_dir(repo, gitdir, 0);
1254 if (offset >= cwd->len)
@@ -1839,29 +1839,27 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
1839 return NULL;
1840 }
1841
1842 -static int git_work_tree_initialized;
1843 -
1842 /*
1843 * Note. This works only before you used a work tree. This was added
1844 * primarily to support git-clone to work in a new repository it just
1845 * created, and is not meant to flip between different work trees.
1846 */
1849 -void set_git_work_tree(const char *new_work_tree)
1847 +void set_git_work_tree(struct repository *repo, const char *new_work_tree)
1848 {
1851 - if (git_work_tree_initialized) {
1849 + if (repo->worktree_initialized) {
1850 struct strbuf realpath = STRBUF_INIT;
1851
1852 strbuf_realpath(&realpath, new_work_tree, 1);
1853 new_work_tree = realpath.buf;
1856 - if (strcmp(new_work_tree, the_repository->worktree))
1854 + if (strcmp(new_work_tree, repo->worktree))
1855 die("internal error: work tree has already been set\n"
1856 "Current worktree: %s\nNew worktree: %s",
1859 - the_repository->worktree, new_work_tree);
1857 + repo->worktree, new_work_tree);
1858 strbuf_release(&realpath);
1859 return;
1860 }
1863 - git_work_tree_initialized = 1;
1864 - repo_set_worktree(the_repository, new_work_tree);
1861 + repo->worktree_initialized = true;
1862 + repo_set_worktree(repo, new_work_tree);
1863 }
1864
1865 const char *setup_git_directory_gently(int *nongit_ok)
setup.h
+1 -1
@@ -96,7 +96,7 @@ static inline int discover_git_directory(struct strbuf *commondir,
96 return 0;
97 }
98
99 -void set_git_work_tree(const char *tree);
99 +void set_git_work_tree(struct repository *repo, const char *tree);
100
101 /* Flags that can be passed to `enter_repo()`. */
102 enum {