Skip to content

Commit 5dcdb18

Browse files
fix(git): check destination emptiness without full readdir
Avoid reading an entire directory just to determine if clone destination is empty. Use ReadDir(1) to detect emptiness efficiently while preserving error behavior for non-empty and inaccessible paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 85bf690 commit 5dcdb18

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

pkg/lib/external/git/clone.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package git
1919
import (
2020
"errors"
2121
"fmt"
22+
"io"
2223
"io/fs"
2324
"os"
2425
"os/exec"
@@ -113,13 +114,19 @@ func checkDestination(path string) error {
113114
if !stat.IsDir() {
114115
return fmt.Errorf("path %s exists and is not a directory", path)
115116
}
116-
// TODO: probably don't need to read the _whole_ directory
117-
contents, err := os.ReadDir(path)
117+
// Only check whether at least one entry exists; avoid reading full directory.
118+
dir, err := os.Open(path)
118119
if err != nil {
119120
return fmt.Errorf("failed to inspect directory %s: %w", path, err)
120121
}
121-
if len(contents) > 0 {
122+
defer dir.Close()
123+
124+
_, err = dir.ReadDir(1)
125+
if err == nil {
122126
return fmt.Errorf("cannot clone to a non-empty directory")
123127
}
128+
if !errors.Is(err, io.EOF) {
129+
return fmt.Errorf("failed to inspect directory %s: %w", path, err)
130+
}
124131
return nil
125132
}

0 commit comments

Comments
 (0)