@@ -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
|
||||
}
|
||||
}
|
||||
|
@@ -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")
|
||||
|
Reference in New Issue
Block a user