]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: ignore missing "refs" directory for linked worktrees
authorshejialuo <shejialuo@gmail.com>
Mon, 2 Jun 2025 14:41:35 +0000 (22:41 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Jun 2025 18:20:19 +0000 (11:20 -0700)
"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 8f4c00de95 (builtin/worktree: create refdb via ref backend,
2024-01-08), we automatically create the "refs" directory for new
worktrees. And in 7c78d819e6 (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 <code@khaugsbakk.name>
Signed-off-by: shejialuo <shejialuo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/files-backend.c
t/t0602-reffiles-fsck.sh

index 6c6e67dc1c48ed0936fdc8395faf663f2c8c7c26..7fd9d49c87df0306c6f6a256243540130ba9aa7d 100644 (file)
@@ -3772,6 +3772,9 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
 
        iter = dir_iterator_begin(sb.buf, 0);
        if (!iter) {
+               if (errno == ENOENT && !is_main_worktree(wt))
+                       goto out;
+
                ret = error_errno(_("cannot open directory %s"), sb.buf);
                goto out;
        }
index d4a08b823b7db71cd98e7bdb32c3b8897a8e226c..a7f25c96057bf93fcf53f5aa1c075c70e5d463e5 100755 (executable)
@@ -106,6 +106,25 @@ test_expect_success 'ref name check should be adapted into fsck messages' '
        test_must_be_empty err
 '
 
+test_expect_success 'no refs directory of worktree should not cause problems' '
+       test_when_finished "rm -rf repo" &&
+       git init repo &&
+       (
+               cd repo &&
+               test_commit initial &&
+               git worktree add --detach ./worktree &&
+
+               (
+                       cd worktree &&
+                       worktree_refdir="$(git rev-parse --git-dir)/refs" &&
+                       # Simulate old directory layout
+                       rmdir "$worktree_refdir" &&
+                       git refs verify 2>err &&
+                       test_must_be_empty err
+               )
+       )
+'
+
 test_expect_success 'ref name check should work for multiple worktrees' '
        test_when_finished "rm -rf repo" &&
        git init repo &&