From 2d5b34cfc2558774d8a0f9b4505f4cf4decc6c77 Mon Sep 17 00:00:00 2001 From: "KADOTA, Kyohei" Date: Fri, 15 Jan 2021 14:24:17 +0900 Subject: [PATCH] Fix "reference not found" error on checkout (#433) (#471) --- act/common/git.go | 22 ++++++++-------------- act/common/git_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/act/common/git.go b/act/common/git.go index c551c226..09292b26 100644 --- a/act/common/git.go +++ b/act/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/act/common/git_test.go b/act/common/git_test.go index bd70d87d..e09004b0 100644 --- a/act/common/git_test.go +++ b/act/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")