Skip to content

fix(git): check destination emptiness without full readdir#1141

Open
AdeshDeshmukh wants to merge 1 commit intokitops-ml:mainfrom
AdeshDeshmukh:fix/check-destination-read-one
Open

fix(git): check destination emptiness without full readdir#1141
AdeshDeshmukh wants to merge 1 commit intokitops-ml:mainfrom
AdeshDeshmukh:fix/check-destination-read-one

Conversation

@AdeshDeshmukh
Copy link
Copy Markdown

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.

Description

This PR improves clone destination validation in pkg/lib/external/git/clone.go.

Previously, checkDestination used os.ReadDir(path) to determine whether a directory is empty, which reads the entire directory contents even though only an empty/non-empty check is needed.

This change replaces that with a one-entry probe:

  • os.Open(path)
  • ReadDir(1)

Behavior is preserved:

  • err == nil => directory is non-empty
  • err == io.EOF => directory is empty
  • other errors => return wrapped inspection error

Validation:

  • go test ./pkg/lib/external/git (pass)

Linked issues

N/A

@gorkem
Copy link
Copy Markdown
Member

gorkem commented Mar 26, 2026

Can you add the sign-off messages to your commits. Click on the failing DCO check above for how to instructions.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves clone destination validation in the Git external helper by checking whether the destination directory is empty without reading the entire directory listing, reducing unnecessary I/O for large directories.

Changes:

  • Replace os.ReadDir(path) full directory reads with os.Open(path) + ReadDir(1) to detect emptiness.
  • Add io import to handle io.EOF from the one-entry probe and preserve existing error semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gorkem
Copy link
Copy Markdown
Member

gorkem commented Apr 3, 2026

@AdeshDeshmukh please fix failing DCO check before we can review this.

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>
Signed-off-by: AdeshDeshmukh <adeshkd123@gmail.com>
@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/check-destination-read-one branch from 5dcdb18 to 253d087 Compare April 3, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants