Skip to content

Commit d5b3c38

Browse files
shejialuogitster
authored andcommitted
fsck: ignore missing "refs" directory for linked worktrees
"git refs verify" doesn't work if there are worktrees created on Git v2.43.0 or older versions. These versions don't automatically create the "refs" directory, causing the error: error: cannot open directory .git/worktrees/<worktree name>/refs: No such file or directory Since 8f4c00d (builtin/worktree: create refdb via ref backend, 2024-01-08), we automatically create the "refs" directory for new worktrees. And in 7c78d81 (ref: support multiple worktrees check for refs, 2024-11-20), we assume that all linked worktrees have this directory and would wrongly report an error to the user, thus introducing compatibility issue. Check for ENOENT errno before reporting directory access errors for linked worktrees to maintain backward compatibility. Reported-by: Kristoffer Haugsbakk <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d50a5e8 commit d5b3c38

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

refs/files-backend.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,6 +3772,9 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
37723772

37733773
iter = dir_iterator_begin(sb.buf, 0);
37743774
if (!iter) {
3775+
if (errno == ENOENT && !is_main_worktree(wt))
3776+
goto out;
3777+
37753778
ret = error_errno(_("cannot open directory %s"), sb.buf);
37763779
goto out;
37773780
}

t/t0602-reffiles-fsck.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ test_expect_success 'ref name check should be adapted into fsck messages' '
106106
test_must_be_empty err
107107
'
108108

109+
test_expect_success 'no refs directory of worktree should not cause problems' '
110+
test_when_finished "rm -rf repo" &&
111+
git init repo &&
112+
(
113+
cd repo &&
114+
test_commit initial &&
115+
git worktree add --detach ./worktree &&
116+
117+
(
118+
cd worktree &&
119+
worktree_refdir="$(git rev-parse --git-dir)/refs" &&
120+
# Simulate old directory layout
121+
rmdir "$worktree_refdir" &&
122+
git refs verify 2>err &&
123+
test_must_be_empty err
124+
)
125+
)
126+
'
127+
109128
test_expect_success 'ref name check should work for multiple worktrees' '
110129
test_when_finished "rm -rf repo" &&
111130
git init repo &&

0 commit comments

Comments
 (0)