Skip to content

Commit

Permalink
fix(): fix the git trigger branch or tag switch bug (argoproj#205)
Browse files Browse the repository at this point in the history
* fix(): fix the git trigger branch or tag switch bug

* fix(): git trigger
  • Loading branch information
VaibhavPage committed Mar 7, 2019
1 parent 40d4ef4 commit 50772c9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions store/git.go
Expand Up @@ -36,6 +36,13 @@ const (
DefaultBranch = "master"
)

var (
fetchRefSpec = []config.RefSpec{
"refs/*:refs/*",
"HEAD:refs/heads/HEAD",
}
)

type GitArtifactReader struct {
kubeClientset kubernetes.Interface
artifact *v1alpha1.GitArtifact
Expand Down Expand Up @@ -108,6 +115,7 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository) ([]byte, error

fetchOptions := &git.FetchOptions{
RemoteName: g.artifact.Remote.Name,
RefSpecs: fetchRefSpec,
}
if auth != nil {
fetchOptions.Auth = auth
Expand All @@ -123,12 +131,21 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository) ([]byte, error
return nil, fmt.Errorf("failed to get working tree. err: %+v", err)
}

if err := r.Fetch(&git.FetchOptions{
RemoteName: g.getRemote(),
RefSpecs: fetchRefSpec,
}); err != nil && err != git.NoErrAlreadyUpToDate {
return nil, fmt.Errorf("failed to fetch. err: %v", err)
}

if err := w.Checkout(g.getBranchOrTag()); err != nil {
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
Expand All @@ -143,13 +160,16 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository) ([]byte, error

func (g *GitArtifactReader) getBranchOrTag() *git.CheckoutOptions {
opts := &git.CheckoutOptions{}

opts.Branch = plumbing.NewBranchReferenceName(DefaultBranch)

if g.artifact.Branch != "" {
opts.Branch = plumbing.NewRemoteReferenceName(g.getRemote(), g.artifact.Branch)
opts.Branch = plumbing.NewBranchReferenceName(g.artifact.Branch)
}
if g.artifact.Tag != "" {
opts.Branch = plumbing.NewTagReferenceName(g.artifact.Tag)
}
opts.Branch = plumbing.NewRemoteReferenceName(g.getRemote(), DefaultBranch)

return opts
}

Expand Down

0 comments on commit 50772c9

Please sign in to comment.