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>
main
Patrick Steinhardt 2026-05-19 11:52:14 +02:00 committed by Junio C Hamano
parent bd2851d84f
commit 7a6a82fba0
5 changed files with 17 additions and 18 deletions

View 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) {

View 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;

View 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;

/*

24
setup.c
View File

@ -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)

View File

@ -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 {