From ab2eb7f931e1c2ce548f117107898d091b719805 Mon Sep 17 00:00:00 2001 From: lufia Date: Thu, 14 Jan 2021 16:50:47 +0900 Subject: [PATCH] Fix "reference not found" error on checkout (#433) --- go.mod | 2 +- go.sum | 3 +++ pkg/common/git.go | 22 ++++++++-------------- pkg/common/git_test.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 1263e7a8811..a3711396b46 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( github.com/sirupsen/logrus v1.7.0 github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect github.com/spf13/cobra v1.1.1 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.7.0 github.com/xanzy/ssh-agent v0.3.0 // indirect golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9 // indirect golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect diff --git a/go.sum b/go.sum index caaf5b21c63..b833ccf3ff5 100644 --- a/go.sum +++ b/go.sum @@ -323,6 +323,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -504,6 +506,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= diff --git a/pkg/common/git.go b/pkg/common/git.go index c551c22652f..09292b2608a 100644 --- a/pkg/common/git.go +++ b/pkg/common/git.go @@ -238,6 +238,9 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor { // At this point we need to know if it's a tag or a branch // And the easiest way to do it is duck typing + // + // If err is nil, it's a tag so let's proceed with that hash like we would if + // it was a sha refType := "tag" rev := plumbing.Revision(path.Join("refs", "tags", input.Ref)) if _, err := r.Tag(input.Ref); errors.Is(err, git.ErrTagNotFound) { @@ -258,29 +261,20 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor { if hash.String() != input.Ref { // Run git fetch to make sure we have the latest sha - err = r.Fetch(&git.FetchOptions{}) - if err != nil && err.Error() != "already up-to-date" { + err := r.Fetch(&git.FetchOptions{}) + if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { logger.Debugf("Unable to fetch: %v", err) } - // At this point we need to know if it's a tag or a branch - // And the easiest way to do it is duck typing - // - // If err is nil, it's a tag so let's proceed with that hash like we would if - // it was a sha - hash, err = r.ResolveRevision(rev) - if err != nil { - logger.Errorf("Unable to resolve %s: %v", rev, err) - return err - } if refType == "branch" { logger.Debugf("Provided ref is not a sha. Checking out branch before pulling changes") + sourceRef := plumbing.ReferenceName(path.Join("refs", "remotes", "origin", input.Ref)) err := w.Checkout(&git.CheckoutOptions{ - Branch: refName, + Branch: sourceRef, Force: true, }) if err != nil { - logger.Errorf("Unable to checkout %s: %v", refName, err) + logger.Errorf("Unable to checkout %s: %v", sourceRef, err) return err } } diff --git a/pkg/common/git_test.go b/pkg/common/git_test.go index bd70d87d5c9..e09004b03ce 100644 --- a/pkg/common/git_test.go +++ b/pkg/common/git_test.go @@ -1,6 +1,7 @@ package common import ( + "context" "fmt" "io/ioutil" "os" @@ -169,6 +170,37 @@ func TestGitFindRef(t *testing.T) { } } +func TestGitCloneExecutor(t *testing.T) { + for name, tt := range map[string]struct { + URL string + Ref string + Err error + }{ + "tag": { + URL: "https://github.com/actions/checkout", + Ref: "v2", + Err: nil, + }, + "branch": { + URL: "https://github.com/anchore/scan-action", + Ref: "act-fails", + Err: nil, + }, + } { + tt := tt + name := name + t.Run(name, func(t *testing.T) { + clone := NewGitCloneExecutor(NewGitCloneExecutorInput{ + URL: tt.URL, + Ref: tt.Ref, + Dir: testDir(t), + }) + err := clone(context.Background()) + assert.ErrorIs(t, err, tt.Err) + }) + } +} + func gitConfig() { if os.Getenv("GITHUB_ACTIONS") == "true" { _ = gitCmd("config", "--global", "user.email", "test@test.com")