Skip to content

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

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

fix(git): check destination emptiness without full readdir#1141
AdeshDeshmukh wants to merge 1 commit into
kitops-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

gorkem commented Mar 26, 2026

Copy link
Copy Markdown
Member

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

gorkem commented Apr 3, 2026

Copy link
Copy Markdown
Member

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

@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/check-destination-read-one branch from 5dcdb18 to 253d087 Compare April 3, 2026 12:54

@amisevsk amisevsk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please reduce this changeset to only address the issue mentioned in the PR title; changing annotations used by Kit and adding legacy options should be done in a separate PR (personally I don't think this is very high priority at the moment).

Also, there are merge conflicts that need to be addressed.

Comment thread pkg/lib/constants/consts.go Outdated
Comment on lines +52 to +66
// KitOps-specific annotations for ModelKit artifacts.
// Canonical keys use the reverse-DNS form of kitops.org.
CliVersionAnnotation = "org.kitops.modelkit.cli-version"
KitfileJsonAnnotation = "org.kitops.modelkit.kitfile"

// Legacy annotation keys kept for backwards compatibility when reading/writing
// manifests produced by older CLI versions.
LegacyCliVersionAnnotation = "ml.kitops.modelkit.cli-version"
LegacyKitfileJsonAnnotation = "ml.kitops.modelkit.kitfile"

// LayerSubtypeAnnotation stores additional type information for a given OCI manifest
// layer within its annotations (e.g. storing prompts within code-type layers)
LayerSubtypeAnnotation = "ml.kitops.modelkit.layer-subtype"
LayerSubtypePrompt = "prompt"
LayerSubtypeAnnotation = "org.kitops.modelkit.layer-subtype"
LegacyLayerSubtypeAnnotation = "ml.kitops.modelkit.layer-subtype"
LayerSubtypePrompt = "prompt"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change, while generally something we probably should do, is unrelated to the purpose of this PR and should be broken out into a separate PR.

Additionally, it adds a decent amount of complexity for handling annotations; I think we're fine sticking with the ml prefix for now.

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.

Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/check-destination-read-one branch from 253d087 to 5954401 Compare June 23, 2026 04:39
@AdeshDeshmukh

Copy link
Copy Markdown
Author

@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

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.

4 participants