Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fetching/checkouts of non-branch/-tag git refs #322

Merged
merged 3 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/apis/sensor/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,15 @@ type GitArtifact struct {
// +optional
Tag string `json:"tag,omitempty" protobuf:"bytes,8,opt,name=tag"`

// Ref to use to pull trigger resource. Will result in a shallow clone and
// fetch.
// +optional
Ref string `json:"ref,omitempty" protobuf:"bytes,9,opt,name=ref"`

// Remote to manage set of tracked repositories. Defaults to "origin".
// Refer https://git-scm.com/docs/git-remote
// +optional
Remote *GitRemoteConfig `json:"remote" protobuf:"bytes,9,opt,name=remote"`
Remote *GitRemoteConfig `json:"remote" protobuf:"bytes,10,opt,name=remote"`
}

// GitRemoteConfig contains the configuration of a Git remote
Expand Down
39 changes: 29 additions & 10 deletions store/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository) ([]byte, error
fetchOptions.Auth = auth
}

// In the case of a specific given ref, it isn't necessary to fetch anything
// but the single ref
if g.artifact.Ref != "" {
fetchOptions.Depth = 1
fetchOptions.RefSpecs = []config.RefSpec{config.RefSpec(g.artifact.Ref + ":" + g.artifact.Ref)}
}

if err := r.Fetch(fetchOptions); err != nil && err != git.NoErrAlreadyUpToDate {
return nil, fmt.Errorf("failed to fetch. err: %v", err)
}
Expand All @@ -146,17 +153,20 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository) ([]byte, error
return nil, fmt.Errorf("failed to checkout. err: %+v", err)
}

pullOpts := &git.PullOptions{
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
ReferenceName: g.getBranchOrTag().Branch,
Force: true,
}
if auth != nil {
pullOpts.Auth = auth
}
// In the case of a specific given ref, it shouldn't be necessary to pull
if g.artifact.Ref != "" {
pullOpts := &git.PullOptions{
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
ReferenceName: g.getBranchOrTag().Branch,
Force: true,
}
if auth != nil {
pullOpts.Auth = auth
}

if err := w.Pull(pullOpts); err != nil && err != git.NoErrAlreadyUpToDate {
return nil, fmt.Errorf("failed to pull latest updates. err: %+v", err)
if err := w.Pull(pullOpts); err != nil && err != git.NoErrAlreadyUpToDate {
return nil, fmt.Errorf("failed to pull latest updates. err: %+v", err)
}
}

return ioutil.ReadFile(fmt.Sprintf("%s/%s", g.artifact.CloneDirectory, g.artifact.FilePath))
Expand All @@ -173,6 +183,9 @@ func (g *GitArtifactReader) getBranchOrTag() *git.CheckoutOptions {
if g.artifact.Tag != "" {
opts.Branch = plumbing.NewTagReferenceName(g.artifact.Tag)
}
if g.artifact.Ref != "" {
opts.Branch = plumbing.ReferenceName(g.artifact.Ref)
}

return opts
}
Expand All @@ -197,6 +210,12 @@ func (g *GitArtifactReader) Read() ([]byte, error) {
cloneOpt.Auth = auth
}

// In the case of a specific given ref, it isn't necessary to have branch
// histories
if g.artifact.Ref != "" {
cloneOpt.Depth = 1
}

r, err = git.PlainClone(g.artifact.CloneDirectory, false, cloneOpt)
if err != nil {
return nil, fmt.Errorf("failed to clone repository. err: %+v", err)
Expand Down
6 changes: 6 additions & 0 deletions store/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ func TestGetBranchOrTag(t *testing.T) {
tag := gar.getBranchOrTag()
convey.So(tag.Branch, convey.ShouldNotEqual, "refs/heads/master")
})

convey.Convey("Given a git artifact with a specific ref, get the ref", t, func() {
gar.artifact.Ref = "refs/something/weird/or/specific"
br := gar.getBranchOrTag()
convey.So(br.Branch, convey.ShouldEqual, "refs/something/weird/or/specific")
})
}