Skip to content

Commit

Permalink
fix: git clone on non-default branch fails (Fixes #5629) (#5630)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytrytek committed Apr 14, 2021
1 parent d5e492c commit ec3b82d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions workflow/artifacts/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ func (g *ArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
if inputArtifact.Git.Revision != "" {
// We still rely on forking git for checkout, since go-git does not have a reliable
// way of resolving revisions (e.g. mybranch, HEAD^, v1.2.3)
log.Infof("Checking out revision %s", inputArtifact.Git.Revision)
cmd := exec.Command("git", "checkout", inputArtifact.Git.Revision)
rev := getRevisionForCheckout(inputArtifact.Git.Revision)
log.Info("Checking out revision ", rev)
cmd := exec.Command("git", "checkout", rev)
cmd.Dir = path
cmd.Env = env
output, err := cmd.Output()
Expand All @@ -170,6 +171,12 @@ func (g *ArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
return nil
}

// getRevisionForCheckout trims "refs/heads/" from the revision name (if present)
// so that `git checkout` will succeed.
func getRevisionForCheckout(revision string) string {
return strings.TrimPrefix(revision, "refs/heads/")
}

func isAlreadyUpToDateErr(err error) bool {
return err != nil && err.Error() != "already up-to-date"
}
Expand Down
17 changes: 17 additions & 0 deletions workflow/artifacts/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ func TestGitArtifactDriverLoad_SSL(t *testing.T) {
}
}

func TestGetCheckoutRevision(t *testing.T) {
for _, tt := range []struct {
in string
expected string
}{
{"my-branch", "my-branch"},
{"refs/heads/my-branch", "my-branch"},
{"refs/tags/1.0.0", "refs/tags/1.0.0"},
{"ae7b5432cfa15577d4740fb047762254be3652db", "ae7b5432cfa15577d4740fb047762254be3652db"},
} {
t.Run(tt.in, func(t *testing.T) {
result := getRevisionForCheckout(tt.in)
assert.Equal(t, result, tt.expected)
})
}
}

func TestGetUser(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down

0 comments on commit ec3b82d

Please sign in to comment.