summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-09-07 09:32:06 -0700
committerJunio C Hamano <gitster@pobox.com>2026-09-07 09:32:06 -0700
commitd59500321923d2f21db38edfb8490f17a1ff1b83 (patch)
treea7658dc64383e214d53002c4e4203659b692a8f8
parent098d38059e2151176cd996ef67d5502d0071b799 (diff)
parent53d330c360227e66298e0f4dba2ce6b6a3a36c37 (diff)
Merge branch 'yn/worktree-repair-relative'
The git worktree repair command failed to rewrite the .git file of a working tree from a relative path to an absolute path when the command was run in the working tree itself. The read_gitfile_gently() function was modified to also return whether the path originally recorded in the file was absolute, and this new capability is used to correctly detect such mismatches. * yn/worktree-repair-relative: worktree repair: detect relative path in .git file correctly
-rw-r--r--setup.c69
-rw-r--r--setup.h1
-rwxr-xr-xt/t2406-worktree-repair.sh54
-rw-r--r--worktree.c20
4 files changed, 94 insertions, 50 deletions
diff --git a/setup.c b/setup.c
index dfe05d9a03..0d157ac254 100644
--- a/setup.c
+++ b/setup.c
@@ -963,15 +963,53 @@ void read_gitfile_error_die(int error_code, const char *path)
*/
const char *read_gitfile_gently(const char *path, int *return_error_code)
{
+ int error_code = 0;
+ const char *slash;
+ struct strbuf contents = STRBUF_INIT;
+ static struct strbuf realpath = STRBUF_INIT;
+
+ error_code = read_gitfile_raw(&contents, path);
+ if (error_code)
+ goto cleanup_return;
+
+ if (!is_absolute_path(contents.buf) && (slash = strrchr(path, '/'))) {
+ size_t pathlen = slash+1 - path;
+ char *dir = xstrfmt("%.*s%s", (int)pathlen, path, contents.buf);
+ strbuf_reset(&contents);
+ strbuf_addstr(&contents, dir);
+ free(dir);
+ }
+ if (!is_git_directory(contents.buf)) {
+ error_code = READ_GITFILE_ERR_NOT_A_REPO;
+ goto cleanup_return;
+ }
+
+ strbuf_realpath(&realpath, contents.buf, 1);
+
+cleanup_return:
+ if (return_error_code)
+ *return_error_code = error_code;
+ else if (error_code)
+ read_gitfile_error_die(error_code, path);
+
+ strbuf_release(&contents);
+ return error_code ? NULL : realpath.buf;
+}
+
+/*
+ * Read the path following "gitdir: " from the .git file into strbuf.
+ *
+ * Unlike read_gitfile_gently(), this function does not resolve a
+ * relative path or validate it using is_git_directory().
+ */
+int read_gitfile_raw(struct strbuf *contents, const char *path)
+{
const int max_file_size = 1 << 20; /* 1MB */
int error_code = 0;
char *buf = NULL;
- char *dir = NULL;
- const char *slash;
struct stat st;
int fd;
ssize_t len;
- static struct strbuf realpath = STRBUF_INIT;
if (stat(path, &st)) {
if (errno == ENOENT || errno == ENOTDIR)
@@ -1014,32 +1052,11 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
error_code = READ_GITFILE_ERR_NO_PATH;
goto cleanup_return;
}
- buf[len] = '\0';
- dir = buf + 8;
-
- if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
- size_t pathlen = slash+1 - path;
- dir = xstrfmt("%.*s%.*s", (int)pathlen, path,
- (int)(len - 8), buf + 8);
- free(buf);
- buf = dir;
- }
- if (!is_git_directory(dir)) {
- error_code = READ_GITFILE_ERR_NOT_A_REPO;
- goto cleanup_return;
- }
-
- strbuf_realpath(&realpath, dir, 1);
- path = realpath.buf;
+ strbuf_add(contents, buf+8, len-8);
cleanup_return:
- if (return_error_code)
- *return_error_code = error_code;
- else if (error_code)
- read_gitfile_error_die(error_code, path);
-
free(buf);
- return error_code ? NULL : path;
+ return error_code;
}
static void apply_gitdir_and_environment(struct repository *repo, const char *path)
diff --git a/setup.h b/setup.h
index 763fd384e8..7394473e95 100644
--- a/setup.h
+++ b/setup.h
@@ -40,6 +40,7 @@ int is_nonbare_repository_dir(struct strbuf *path);
#define READ_GITFILE_ERR_IS_A_DIR 10
void read_gitfile_error_die(int error_code, const char *path);
const char *read_gitfile_gently(const char *path, int *return_error_code);
+int read_gitfile_raw(struct strbuf *contents, const char *path);
#define read_gitfile(path) read_gitfile_gently((path), NULL)
const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
index f5f19b3169..d4e53d492b 100755
--- a/t/t2406-worktree-repair.sh
+++ b/t/t2406-worktree-repair.sh
@@ -228,30 +228,60 @@ test_expect_success 'repair worktree with relative path with missing gitfile' '
test_cmp expect wt/.git
'
-test_expect_success 'repair absolute worktree to use relative paths' '
- test_when_finished "rm -rf main side sidemoved" &&
+test_expect_success 'repair absolute to relative from side worktree' '
+ test_when_finished "rm -rf main side" &&
test_create_repo main &&
test_commit -C main init &&
git -C main worktree add --detach ../side &&
- echo "../../../../sidemoved/.git" >expect-gitdir &&
+ echo "../../../../side/.git" >expect-gitdir &&
echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
- mv side sidemoved &&
- git -C main worktree repair --relative-paths ../sidemoved &&
+ git -C main worktree repair --relative-paths ../side 2>main/err &&
+ test_grep "gitdir absolute/relative path mismatch" main/err &&
test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
- test_cmp expect-gitfile sidemoved/.git
+ test_cmp expect-gitfile side/.git
'
-test_expect_success 'repair relative worktree to use absolute paths' '
- test_when_finished "rm -rf main side sidemoved" &&
+test_expect_success 'repair relative to absolute from side worktree' '
+ test_when_finished "rm -rf main side" &&
test_create_repo main &&
test_commit -C main init &&
git -C main worktree add --relative-paths --detach ../side &&
- echo "$(pwd)/sidemoved/.git" >expect-gitdir &&
+ echo "$(pwd)/side/.git" >expect-gitdir &&
echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
- mv side sidemoved &&
- git -C main worktree repair ../sidemoved &&
+ git -C main worktree repair ../side 2>main/err &&
+ test_grep "gitdir absolute/relative path mismatch" main/err &&
test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
- test_cmp expect-gitfile sidemoved/.git
+ test_cmp expect-gitfile side/.git
+'
+
+test_expect_success 'repair absolute to relative from main worktree' '
+ test_when_finished "rm -rf main side" &&
+ test_create_repo main &&
+ git -C main config worktree.useRelativePaths false &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ echo "../../../../side/.git" >expect-gitdir &&
+ echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile &&
+ git -C main config worktree.useRelativePaths true &&
+ git -C main worktree repair 2>main/err &&
+ test_grep ".git file absolute/relative path mismatch" main/err &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile side/.git
+'
+
+test_expect_success 'repair relative to absolute from main worktree' '
+ test_when_finished "rm -rf main side" &&
+ test_create_repo main &&
+ git -C main config worktree.useRelativePaths true &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ echo "$(pwd)/side/.git" >expect-gitdir &&
+ echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile &&
+ git -C main config worktree.useRelativePaths false &&
+ git -C main worktree repair 2>main/err &&
+ test_grep ".git file absolute/relative path mismatch" main/err &&
+ test_cmp expect-gitdir main/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile side/.git
'
test_done
diff --git a/worktree.c b/worktree.c
index cbf95328a3..8cb8637b18 100644
--- a/worktree.c
+++ b/worktree.c
@@ -649,7 +649,8 @@ static void repair_gitfile(struct worktree *wt,
struct strbuf gitdir = STRBUF_INIT;
struct strbuf repo = STRBUF_INIT;
struct strbuf backlink = STRBUF_INIT;
- char *dotgit_contents = NULL;
+ struct strbuf contents = STRBUF_INIT;
+ const char *dotgit_contents = NULL;
const char *repair = NULL;
char *path = NULL;
int err;
@@ -667,7 +668,9 @@ static void repair_gitfile(struct worktree *wt,
strbuf_realpath(&repo, path, 1);
strbuf_addf(&dotgit, "%s/.git", wt->path);
strbuf_addf(&gitdir, "%s/gitdir", repo.buf);
- dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
+ err = read_gitfile_raw(&contents, dotgit.buf);
+ if (!err)
+ dotgit_contents = contents.buf;
if (dotgit_contents) {
if (is_absolute_path(dotgit_contents)) {
@@ -681,7 +684,7 @@ static void repair_gitfile(struct worktree *wt,
if (err == READ_GITFILE_ERR_NOT_A_FILE ||
err == READ_GITFILE_ERR_IS_A_DIR)
fn(1, wt->path, _(".git is not a file"), cb_data);
- else if (err)
+ else if (err || !is_git_directory(backlink.buf))
repair = _(".git file broken");
else if (fspathcmp(backlink.buf, repo.buf))
repair = _(".git file incorrect");
@@ -695,12 +698,12 @@ static void repair_gitfile(struct worktree *wt,
}
done:
- free(dotgit_contents);
free(path);
strbuf_release(&repo);
strbuf_release(&dotgit);
strbuf_release(&gitdir);
strbuf_release(&backlink);
+ strbuf_release(&contents);
}
static void repair_noop(int iserr UNUSED,
@@ -857,14 +860,7 @@ void repair_worktree_at_path(struct repository *repo,
strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
if (dotgit_contents) {
- if (is_absolute_path(dotgit_contents)) {
- strbuf_addstr(&backlink, dotgit_contents);
- } else {
- strbuf_addbuf(&backlink, &dotgit);
- strbuf_strip_suffix(&backlink, ".git");
- strbuf_addstr(&backlink, dotgit_contents);
- strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
- }
+ strbuf_addstr(&backlink, dotgit_contents);
} else if (err == READ_GITFILE_ERR_NOT_A_FILE ||
err == READ_GITFILE_ERR_IS_A_DIR) {
fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);