Fix "reference not found" error (#433)

If an action uses the branch to pin the major version, `- use: user/action@v1`
will stop with an error: "v1: reference not found."

In this case `act` should use refs/remotes/origin/v1 as a name to resolve v1 revision.

Co-authored-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
KADOTA, Kyohei 2021-01-12 15:47:33 +09:00 committed by GitHub
parent 760daebf5d
commit 1b38d5c4d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -235,7 +236,15 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
return err return err
} }
hash, err := r.ResolveRevision(plumbing.Revision(input.Ref)) // 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
refType := "tag"
rev := plumbing.Revision(path.Join("refs", "tags", input.Ref))
if _, err := r.Tag(input.Ref); errors.Is(err, git.ErrTagNotFound) {
refType = "branch"
rev = plumbing.Revision(path.Join("refs", "remotes", "origin", input.Ref))
}
hash, err := r.ResolveRevision(rev)
if err != nil { if err != nil {
logger.Errorf("Unable to resolve %s: %v", input.Ref, err) logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
return err return err
@ -259,31 +268,21 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
// //
// If err is nil, it's a tag so let's proceed with that hash like we would if // If err is nil, it's a tag so let's proceed with that hash like we would if
// it was a sha // it was a sha
rev := fmt.Sprintf("refs/tags/%s", input.Ref) hash, err = r.ResolveRevision(rev)
hash, err = r.ResolveRevision(plumbing.Revision(rev))
// But if it's not nil, then the ref provided isn't a tag, and it's not a sha
// so we assume that it's a branch
if err != nil { if err != nil {
rev := fmt.Sprintf("refs/remotes/origin/%s", input.Ref) logger.Errorf("Unable to resolve %s: %v", rev, err)
hash, err = r.ResolveRevision(plumbing.Revision(rev)) return err
if err != nil { }
logger.Errorf("Unable to resolve %s: %v", rev, err) if refType == "branch" {
return err
}
logger.Debugf("Provided ref is not a sha. Checking out branch before pulling changes") logger.Debugf("Provided ref is not a sha. Checking out branch before pulling changes")
err := w.Checkout(&git.CheckoutOptions{
err = w.Checkout(&git.CheckoutOptions{
Branch: refName, Branch: refName,
Force: true, Force: true,
}) })
if err != nil { if err != nil {
logger.Errorf("Unable to checkout %s: %v", refName, err) logger.Errorf("Unable to checkout %s: %v", refName, err)
return err return err
} }
} }
} }