Skip to content

Commit

Permalink
Merge pull request #400 from Manoramsharma/patch-pretest
Browse files Browse the repository at this point in the history
[PRETEST1]: Initial changes for adding support to clone bare repo
  • Loading branch information
Peefy authored Jul 26, 2024
2 parents c3823c2 + cabc886 commit 5145a44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type CloneOptions struct {
Branch string
LocalPath string
Writer io.Writer
Bare bool // New field to indicate if the clone should be bare
}

// CloneOption is a function that modifies CloneOptions
Expand All @@ -39,6 +40,13 @@ func NewCloneOptions(repoUrl, commit, tag, branch, localpath string, Writer io.W
}
}

// WithBare sets the bare flag for CloneOptions
func WithBare(isBare bool) CloneOption {
return func(o *CloneOptions) {
o.Bare = isBare
}
}

// WithRepoURL sets the repo URL for CloneOptions
func WithRepoURL(repoURL string) CloneOption {
return func(o *CloneOptions) {
Expand Down Expand Up @@ -101,6 +109,23 @@ func (cloneOpts *CloneOptions) Validate() error {
return nil
}

// Clone clones a git repository
func (cloneOpts *CloneOptions) CloneBare() (*git.Repository, error) {
if err := cloneOpts.Validate(); err != nil {
return nil, err
}

repo, err := git.PlainClone(cloneOpts.LocalPath, cloneOpts.Bare, &git.CloneOptions{
URL: cloneOpts.RepoURL,
Progress: cloneOpts.Writer,
})
if err != nil {
return nil, err
}

return repo, nil
}

// Clone clones a git repository
func (cloneOpts *CloneOptions) Clone() (*git.Repository, error) {
if err := cloneOpts.Validate(); err != nil {
Expand All @@ -126,7 +151,6 @@ func (cloneOpts *CloneOptions) Clone() (*git.Repository, error) {
}

repo, err := git.PlainOpen(cloneOpts.LocalPath)

if err != nil {
return nil, err
}
Expand All @@ -145,6 +169,9 @@ func CloneWithOpts(opts ...CloneOption) (*git.Repository, error) {
if err != nil {
return nil, err
}
if cloneOpts.Bare {
return cloneOpts.CloneBare()
}

return cloneOpts.Clone()
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

func TestWithGitOptions(t *testing.T) {
cloneOpts := &CloneOptions{}
WithBare(true)(cloneOpts)
assert.Equal(t, cloneOpts.Bare, true)
WithRepoURL("test_url")(cloneOpts)
assert.Equal(t, cloneOpts.RepoURL, "test_url")
WithBranch("test_branch")(cloneOpts)
Expand Down

0 comments on commit 5145a44

Please sign in to comment.