Skip to content

Commit

Permalink
Fix clone for a branch ref for github action (#28)
Browse files Browse the repository at this point in the history
* Fix clone for a branch ref for github action

* fix nil ptr

---------

Co-authored-by: raghavharness <[email protected]>
  • Loading branch information
shubham149 and raghavharness authored May 30, 2023
1 parent 05adaad commit 30ed8fa
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cloner/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"io"
"os"
"regexp"
"strings"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -68,7 +69,8 @@ func (c *cloner) Clone(ctx context.Context, params Params) error {
}
// clone the repository
r, err := git.PlainClone(params.Dir, false, opts)
if errors.Is(plumbing.ErrReferenceNotFound, err) && !strings.HasPrefix(params.Ref, "refs/") {
if (errors.Is(plumbing.ErrReferenceNotFound, err) || matchRefNotFoundErr(err)) &&
!strings.HasPrefix(params.Ref, "refs/") {
// If params.Ref is provided without refs/*, then we are assuming it to either refs/heads/ or refs/tags.
// Try clone again with inverse ref.
if opts.ReferenceName.IsBranch() {
Expand Down Expand Up @@ -100,3 +102,12 @@ func (c *cloner) Clone(ctx context.Context, params Params) error {
Hash: plumbing.NewHash(params.Sha),
})
}

func matchRefNotFoundErr(err error) bool {
if err == nil {
return false
}
pattern := `couldn't find remote ref.*`
regex := regexp.MustCompile(pattern)
return regex.MatchString(err.Error())
}

0 comments on commit 30ed8fa

Please sign in to comment.