setup: stop using `the_repository` in `is_inside_work_tree()`

Similar as with the preceding commit, `is_inside_work_tree()` determines
whether the current working directory is located inside the worktree of
`the_repository`. Perform the same refactoring by dropping the caching
mechanism and injecting the repository that shall be checked.

Note that, same as in the preceding commit, we're also resolving the
worktree path via `realpath()`. In theory this step is not necessary as
we always set the worktree path via `repo_set_worktree()`, and that
function already resolves the path for us. But resolving the path a
second time is unlikely to matter performance-wise, and it feels fragile
to rely on the repository's worktree path being absolute. We thus
perform the same extra step even though it's ultimately not required.

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:07 +02:00 committed by Junio C Hamano
parent ce70cbc294
commit 8da5ecdb4d
6 changed files with 20 additions and 17 deletions

View File

@ -703,7 +703,7 @@ int cmd_ls_files(int argc,
if (dir.exclude_per_dir)
exc_given = 1;

if (require_work_tree && !is_inside_work_tree())
if (require_work_tree && !is_inside_work_tree(repo))
setup_work_tree();

if (recurse_submodules &&

View File

@ -1006,7 +1006,7 @@ int cmd_rev_parse(int argc,
}
if (!strcmp(arg, "--show-cdup")) {
const char *pfx = prefix;
if (!is_inside_work_tree()) {
if (!is_inside_work_tree(the_repository)) {
const char *work_tree =
repo_get_work_tree(the_repository);
if (work_tree)
@ -1068,7 +1068,7 @@ int cmd_rev_parse(int argc,
continue;
}
if (!strcmp(arg, "--is-inside-work-tree")) {
printf("%s\n", is_inside_work_tree() ? "true"
printf("%s\n", is_inside_work_tree(the_repository) ? "true"
: "false");
continue;
}

View File

@ -1703,7 +1703,7 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
if (!starts_with(rel, "./") && !starts_with(rel, "../"))
return NULL;

if (r != the_repository || !is_inside_work_tree())
if (r != the_repository || !is_inside_work_tree(the_repository))
die(_("relative path syntax can't be used outside working tree"));

/* die() inside prefix_path() if resolved path is outside worktree */

25
setup.c
View File

@ -26,7 +26,6 @@
#include "trace2.h"
#include "worktree.h"

static int inside_work_tree = -1;
static int work_tree_config_is_bogus;
enum allowed_bare_repo {
ALLOWED_BARE_REPO_EXPLICIT = 0,
@ -298,7 +297,7 @@ void verify_filename(const char *prefix,
*/
void verify_non_filename(const char *prefix, const char *arg)
{
if (!is_inside_work_tree() || is_inside_git_dir(the_repository))
if (!is_inside_work_tree(the_repository) || is_inside_git_dir(the_repository))
return;
if (*arg == '-')
return; /* flag */
@ -477,11 +476,20 @@ int is_inside_git_dir(struct repository *repo)
return ret;
}

int is_inside_work_tree(void)
int is_inside_work_tree(struct repository *repo)
{
if (inside_work_tree < 0)
inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
return inside_work_tree;
struct strbuf buf = STRBUF_INIT;
const char *worktree;
int ret;

worktree = repo_get_work_tree(repo);
if (!worktree)
return 0;

ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1));

strbuf_release(&buf);
return ret;
}

void setup_work_tree(void)
@ -798,13 +806,10 @@ static int check_repository_format_gently(struct repository *repo,
if (!has_common) {
if (candidate->is_bare != -1) {
is_bare_repository_cfg = candidate->is_bare;
if (is_bare_repository_cfg == 1)
inside_work_tree = -1;
}
if (candidate->work_tree) {
free(git_work_tree_cfg);
git_work_tree_cfg = xstrdup(candidate->work_tree);
inside_work_tree = -1;
}
}

@ -1251,7 +1256,6 @@ static const char *setup_discovered_git_dir(struct repository *repo,
set_git_work_tree(".");
if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
set_git_dir(repo, gitdir, 0);
inside_work_tree = 1;
if (offset >= cwd->len)
return NULL;

@ -1286,7 +1290,6 @@ static const char *setup_bare_git_dir(struct repository *repo,
return setup_explicit_git_dir(repo, gitdir, cwd, repo_fmt, nongit_ok);
}

inside_work_tree = 0;
if (offset != cwd->len) {
if (chdir(cwd->buf))
die_errno(_("cannot come back to cwd"));

View File

@ -5,7 +5,7 @@
#include "string-list.h"

int is_inside_git_dir(struct repository *repo);
int is_inside_work_tree(void);
int is_inside_work_tree(struct repository *repo);
int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
int get_common_dir(struct strbuf *sb, const char *gitdir);


View File

@ -2620,7 +2620,7 @@ int get_superproject_working_tree(struct strbuf *buf)
int code;
ssize_t len;

if (!is_inside_work_tree())
if (!is_inside_work_tree(the_repository))
/*
* FIXME:
* We might have a superproject, but it is harder