kitops icon indicating copy to clipboard operation
kitops copied to clipboard

fix(git): check destination emptiness without full readdir

Open AdeshDeshmukh opened this issue 8 months ago • 2 comments

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

AdeshDeshmukh avatar Mar 25 '26 13:03 AdeshDeshmukh

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

gorkem avatar Mar 26 '26 14:03 gorkem

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

gorkem avatar Apr 03 '26 12:04 gorkem

@amisevsk Thank you for your thorough review and valuable suggestions. I've removed the annotation migration changes as you recommended — the PR now focuses solely on the clone.go directory emptiness check. I've also resolved the merge conflicts and ensured DCO compliance. Would appreciate a re-review when you a bandwidth. PTAL

AdeshDeshmukh avatar Jun 23 '26 04:06 AdeshDeshmukh