]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: stop using `the_repository` in `is_inside_work_tree()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 19 May 2026 09:52:07 +0000 (11:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 May 2026 10:36:23 +0000 (19:36 +0900)
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>
builtin/ls-files.c
builtin/rev-parse.c
object-name.c
setup.c
setup.h
submodule.c

index b148607f7a14684570f4ec78b17282c958776e72..09d95111b35b9f5032721d8b21cad3d8363fe2a2 100644 (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 &&
index a216be63cf7cecb7c0180dac13255b18bb692d8c..2fcd6851d183974abf037df31380b171a55b3af8 100644 (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;
                        }
index 21dcdc4a0e7c550cd65266ca55d61ef030b140f6..37a9ce8e872572b073f08d5e1085d0b96f0cd41a 100644 (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 */
diff --git a/setup.c b/setup.c
index 80f3ba0d621f44a74a19530685652e3a9916e830..041e08b98ddf89ebe60b97a2aa4181f0fcc50f6c 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -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"));
diff --git a/setup.h b/setup.h
index 115bda647c6e948eb956442babade14530333c32..71d3f918837873f58a02945e86c6b3eb490c31fc 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -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);
 
index b1a0363f9d2a96a1084953bf46f9c788d425741f..a939ff5072726f8ba6096bd321d7e426efd7cc3c 100644 (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