Skip to content

Commit 776d00f

Browse files
committed
git: Allow MkdirAll on worktree-root paths
The worktree filesystem wrapper rejects `""`, `"."`, and `"/"` in `MkdirAll` via `validPath`, even though `validReadPath` was already relaxed to treat those paths as legitimate references to the worktree root. The asymmetry was deliberate for genuine mutations — mutating the root itself is not a sensible operation for `Create` or `Remove` — but `MkdirAll` on an existing directory is not a mutation: it asks the filesystem to ensure a directory exists, against one that always does. Short-circuit `MkdirAll` to a no-op for root-equivalents before `validPath` runs. `validPath` itself stays strict, so the other mutating operations continue to reject the root and the existing `TestValidPath` table is untouched. A new `TestWorktreeFilesystemMkdirAllRootIsNoop` locks in the no-op contract against the wrapper. Backport of #2115. Assisted-by: Claude Opus 4.7 Signed-off-by: Hidde Beydals <[email protected]>
1 parent 3235bb6 commit 776d00f

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎worktree_fs.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ func (sfs *worktreeFilesystem) Readlink(link string) (string, error) {
123123
}
124124

125125
func (sfs *worktreeFilesystem) MkdirAll(path string, perm os.FileMode) error {
126+
// MkdirAll on the worktree root is a no-op: the root always exists,
127+
// so there is nothing to materialise. Mirroring the tolerance that
128+
// validReadPath gives to read-side operations avoids breaking callers
129+
// that walk a directory tree and pass the relative-to-root prefix
130+
// ("") through to the worktree FS.
131+
if path == "" || path == "." || path == "/" {
132+
return nil
133+
}
126134
if err := sfs.validPath(path); err != nil {
127135
return fmt.Errorf("mkdirall: %w", err)
128136
}

‎worktree_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/go-git/go-git/v5/storage/filesystem"
2525
"github.com/go-git/go-git/v5/storage/memory"
2626
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
2728

2829
"github.com/go-git/go-billy/v5"
2930
"github.com/go-git/go-billy/v5/memfs"
@@ -3193,6 +3194,31 @@ func TestValidPath(t *testing.T) {
31933194
}
31943195
}
31953196

3197+
// TestWorktreeFilesystemMkdirAllRootIsNoop locks in the contract that
3198+
// MkdirAll on a root-equivalent path is a silent no-op against the
3199+
// wrapper. validPath itself still rejects "", ".", and "/" (see
3200+
// TestValidPath), but MkdirAll specifically tolerates them because
3201+
// "ensure the root exists" is always trivially satisfied.
3202+
func TestWorktreeFilesystemMkdirAllRootIsNoop(t *testing.T) {
3203+
t.Parallel()
3204+
3205+
rootPaths := []string{"", ".", "/"}
3206+
for _, p := range rootPaths {
3207+
t.Run(p, func(t *testing.T) {
3208+
t.Parallel()
3209+
3210+
mfs := memfs.New()
3211+
fs := newWorktreeFilesystem(mfs, true, true)
3212+
3213+
require.NoError(t, fs.MkdirAll(p, 0o755))
3214+
3215+
entries, err := mfs.ReadDir("/")
3216+
require.NoError(t, err)
3217+
assert.Empty(t, entries, "MkdirAll(%q) must not materialise a directory entry", p)
3218+
})
3219+
}
3220+
}
3221+
31963222
var statusCodeNames = map[StatusCode]string{
31973223
Unmodified: "Unmodified",
31983224
Untracked: "Untracked",

0 commit comments

Comments
 (0)