]> git.ipfire.org Git - thirdparty/git.git/blobdiff - setup.c
setup: return correct prefix if worktree is '/'
[thirdparty/git.git] / setup.c
diff --git a/setup.c b/setup.c
index dadc66659a4037b614b215b7f812c4df8969562b..f0468c6864195e0ff9532f9ec5251d5288b38397 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -322,6 +322,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
        const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
        const char *worktree;
        char *gitfile;
+       int offset;
 
        if (PATH_MAX - 40 < strlen(gitdirenv))
                die("'$%s' too big", GIT_DIR_ENVIRONMENT);
@@ -387,15 +388,15 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
                return NULL;
        }
 
-       if (!prefixcmp(cwd, worktree) &&
-           cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
+       offset = dir_inside_of(cwd, worktree);
+       if (offset >= 0) {      /* cwd inside worktree? */
                set_git_dir(make_absolute_path(gitdirenv));
                if (chdir(worktree))
                        die_errno("Could not chdir to '%s'", worktree);
                cwd[len++] = '/';
                cwd[len] = '\0';
                free(gitfile);
-               return cwd + strlen(worktree) + 1;
+               return cwd + offset;
        }
 
        /* cwd outside worktree */