2022-06-10 16:16:42 -05:00
|
|
|
package git
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2022-06-10 16:16:42 -05:00
|
|
|
"errors"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
2020-02-10 17:27:05 -06:00
|
|
|
"io"
|
2019-01-12 22:45:25 -06:00
|
|
|
"os"
|
2021-01-12 00:47:33 -06:00
|
|
|
"path"
|
2019-01-12 22:45:25 -06:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2022-06-10 16:16:42 -05:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
2021-05-07 22:29:03 -05:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
2020-04-16 18:24:30 -05:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2021-05-05 11:42:34 -05:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
2019-01-12 22:45:25 -06:00
|
|
|
"github.com/go-ini/ini"
|
2021-05-06 08:55:23 -05:00
|
|
|
"github.com/mattn/go-isatty"
|
2019-01-12 22:45:25 -06:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-02-14 14:24:18 -06:00
|
|
|
var (
|
|
|
|
codeCommitHTTPRegex = regexp.MustCompile(`^https?://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
|
|
|
codeCommitSSHRegex = regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
|
|
|
githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`)
|
2021-11-13 08:56:31 -06:00
|
|
|
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+?)(?:.git)?$`)
|
2019-02-14 14:24:18 -06:00
|
|
|
|
|
|
|
cloneLock sync.Mutex
|
2022-06-10 16:16:42 -05:00
|
|
|
|
|
|
|
ErrShortRef = errors.New("short SHA references are not supported")
|
|
|
|
ErrNoRepo = errors.New("unable to find git repo")
|
2019-02-14 14:24:18 -06:00
|
|
|
)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2022-06-10 16:16:42 -05:00
|
|
|
type Error struct {
|
|
|
|
err error
|
|
|
|
commit string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Unwrap() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Commit() string {
|
|
|
|
return e.commit
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
// FindGitRevision get the current git revision
|
2022-06-17 10:55:21 -05:00
|
|
|
func FindGitRevision(ctx context.Context, file string) (shortSha string, sha string, err error) {
|
|
|
|
logger := common.Logger(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2022-10-29 12:15:38 -05:00
|
|
|
bts, err := os.ReadFile(filepath.Join(gitDir, "HEAD"))
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2019-01-17 17:53:03 -06:00
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
var ref = strings.TrimSpace(strings.TrimPrefix(string(bts), "ref:"))
|
2019-01-17 17:53:03 -06:00
|
|
|
var refBuf []byte
|
2019-01-18 15:28:53 -06:00
|
|
|
if strings.HasPrefix(ref, "refs/") {
|
2019-01-17 17:53:03 -06:00
|
|
|
// load commitid ref
|
2022-10-29 12:15:38 -05:00
|
|
|
refBuf, err = os.ReadFile(filepath.Join(gitDir, ref))
|
2019-01-17 17:53:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-18 15:28:53 -06:00
|
|
|
refBuf = []byte(ref)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-17 17:53:03 -06:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("Found revision: %s", refBuf)
|
2019-02-12 22:02:24 -06:00
|
|
|
return string(refBuf[:7]), strings.TrimSpace(string(refBuf)), nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-18 15:28:53 -06:00
|
|
|
// FindGitRef get the current git ref
|
2022-06-17 10:55:21 -05:00
|
|
|
func FindGitRef(ctx context.Context, file string) (string, error) {
|
|
|
|
logger := common.Logger(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("Loading revision from git directory '%s'", gitDir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
_, ref, err := FindGitRevision(ctx, file)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("HEAD points to '%s'", ref)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2021-05-03 09:32:00 -05:00
|
|
|
// Prefer the git library to iterate over the references and find a matching tag or branch.
|
|
|
|
var refTag = ""
|
|
|
|
var refBranch = ""
|
|
|
|
r, err := git.PlainOpen(filepath.Join(gitDir, ".."))
|
|
|
|
if err == nil {
|
|
|
|
iter, err := r.References()
|
|
|
|
if err == nil {
|
|
|
|
for {
|
|
|
|
r, err := iter.Next()
|
|
|
|
if r == nil || err != nil {
|
|
|
|
break
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
// logger.Debugf("Reference: name=%s sha=%s", r.Name().String(), r.Hash().String())
|
2021-05-03 09:32:00 -05:00
|
|
|
if r.Hash().String() == ref {
|
|
|
|
if r.Name().IsTag() {
|
|
|
|
refTag = r.Name().String()
|
|
|
|
}
|
|
|
|
if r.Name().IsBranch() {
|
|
|
|
refBranch = r.Name().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if refTag != "" {
|
|
|
|
return refTag, nil
|
|
|
|
}
|
|
|
|
if refBranch != "" {
|
|
|
|
return refBranch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the above doesn't work, fall back to the old way
|
|
|
|
|
2019-05-22 22:05:14 -05:00
|
|
|
// try tags first
|
2022-06-17 10:55:21 -05:00
|
|
|
tag, err := findGitPrettyRef(ctx, ref, gitDir, "refs/tags")
|
2019-05-22 22:05:14 -05:00
|
|
|
if err != nil || tag != "" {
|
|
|
|
return tag, err
|
|
|
|
}
|
|
|
|
// and then branches
|
2022-06-17 10:55:21 -05:00
|
|
|
return findGitPrettyRef(ctx, ref, gitDir, "refs/heads")
|
2019-05-22 22:05:14 -05:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func findGitPrettyRef(ctx context.Context, head, root, sub string) (string, error) {
|
2019-05-22 22:05:14 -05:00
|
|
|
var name string
|
|
|
|
var err = filepath.Walk(filepath.Join(root, sub), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2021-05-07 22:29:03 -05:00
|
|
|
return err
|
2019-05-22 22:05:14 -05:00
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
if name != "" || info.IsDir() {
|
2019-05-22 22:05:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
var bts []byte
|
2022-10-29 12:15:38 -05:00
|
|
|
if bts, err = os.ReadFile(path); err != nil {
|
2019-05-22 22:05:14 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pointsTo = strings.TrimSpace(string(bts))
|
|
|
|
if head == pointsTo {
|
2021-03-12 18:23:03 -06:00
|
|
|
// On Windows paths are separated with backslash character so they should be replaced to provide proper git refs format
|
|
|
|
name = strings.TrimPrefix(strings.ReplaceAll(strings.Replace(path, root, "", 1), `\`, `/`), "/")
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Debugf("HEAD matches %s", name)
|
2019-05-22 22:05:14 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return name, err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindGithubRepo get the repo
|
2022-06-17 10:55:21 -05:00
|
|
|
func FindGithubRepo(ctx context.Context, file, githubInstance, remoteName string) (string, error) {
|
2022-04-04 12:53:08 -05:00
|
|
|
if remoteName == "" {
|
|
|
|
remoteName = "origin"
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
url, err := findGitRemoteURL(ctx, file, remoteName)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-05 11:42:34 -05:00
|
|
|
_, slug, err := findGitSlug(url, githubInstance)
|
2019-01-12 22:45:25 -06:00
|
|
|
return slug, err
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func findGitRemoteURL(ctx context.Context, file, remoteName string) (string, error) {
|
2019-01-12 22:45:25 -06:00
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Debugf("Loading slug from git directory '%s'", gitDir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
gitconfig, err := ini.InsensitiveLoad(fmt.Sprintf("%s/config", gitDir))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-04-04 12:53:08 -05:00
|
|
|
remote, err := gitconfig.GetSection(fmt.Sprintf(`remote "%s"`, remoteName))
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
urlKey, err := remote.GetKey("url")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
url := urlKey.String()
|
|
|
|
return url, nil
|
|
|
|
}
|
|
|
|
|
2021-05-05 11:42:34 -05:00
|
|
|
func findGitSlug(url string, githubInstance string) (string, string, error) {
|
2019-01-12 22:45:25 -06:00
|
|
|
if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil {
|
2019-02-14 14:24:18 -06:00
|
|
|
return "CodeCommit", matches[2], nil
|
2019-01-12 22:45:25 -06:00
|
|
|
} else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "CodeCommit", matches[2], nil
|
2019-02-14 14:24:18 -06:00
|
|
|
} else if matches := githubHTTPRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
|
|
|
|
} else if matches := githubSSHRegex.FindStringSubmatch(url); matches != nil {
|
2019-01-12 22:45:25 -06:00
|
|
|
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
|
2021-05-05 11:42:34 -05:00
|
|
|
} else if githubInstance != "github.com" {
|
|
|
|
gheHTTPRegex := regexp.MustCompile(fmt.Sprintf(`^https?://%s/(.+)/(.+?)(?:.git)?$`, githubInstance))
|
2021-11-13 08:56:31 -06:00
|
|
|
gheSSHRegex := regexp.MustCompile(fmt.Sprintf(`%s[:/](.+)/(.+?)(?:.git)?$`, githubInstance))
|
2021-05-05 11:42:34 -05:00
|
|
|
if matches := gheHTTPRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
|
|
|
|
} else if matches := gheSSHRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
return "", url, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findGitDirectory(fromFile string) (string, error) {
|
|
|
|
absPath, err := filepath.Abs(fromFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir string
|
|
|
|
if fi.Mode().IsDir() {
|
|
|
|
dir = absPath
|
|
|
|
} else {
|
2019-05-22 23:41:56 -05:00
|
|
|
dir = filepath.Dir(absPath)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
gitPath := filepath.Join(dir, ".git")
|
2019-01-12 22:45:25 -06:00
|
|
|
fi, err = os.Stat(gitPath)
|
|
|
|
if err == nil && fi.Mode().IsDir() {
|
|
|
|
return gitPath, nil
|
|
|
|
} else if dir == "/" || dir == "C:\\" || dir == "c:\\" {
|
2022-06-10 16:16:42 -05:00
|
|
|
return "", &Error{err: ErrNoRepo}
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return findGitDirectory(filepath.Dir(dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
|
|
|
type NewGitCloneExecutorInput struct {
|
2021-05-05 11:42:34 -05:00
|
|
|
URL string
|
|
|
|
Ref string
|
|
|
|
Dir string
|
|
|
|
Token string
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:26:25 -05:00
|
|
|
// CloneIfRequired ...
|
2021-04-06 08:43:02 -05:00
|
|
|
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, error) {
|
2020-06-09 09:43:26 -05:00
|
|
|
r, err := git.PlainOpen(input.Dir)
|
|
|
|
if err != nil {
|
|
|
|
var progressWriter io.Writer
|
2021-05-06 08:55:23 -05:00
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
|
|
|
if entry, ok := logger.(*log.Entry); ok {
|
|
|
|
progressWriter = entry.WriterLevel(log.DebugLevel)
|
|
|
|
} else if lgr, ok := logger.(*log.Logger); ok {
|
|
|
|
progressWriter = lgr.WriterLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.Errorf("Unable to get writer from logger (type=%T)", logger)
|
|
|
|
progressWriter = os.Stdout
|
|
|
|
}
|
2020-06-09 09:43:26 -05:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:55:23 -05:00
|
|
|
cloneOptions := git.CloneOptions{
|
|
|
|
URL: input.URL,
|
|
|
|
Progress: progressWriter,
|
|
|
|
}
|
2021-05-05 11:42:34 -05:00
|
|
|
if input.Token != "" {
|
2021-05-06 08:55:23 -05:00
|
|
|
cloneOptions.Auth = &http.BasicAuth{
|
|
|
|
Username: "token",
|
|
|
|
Password: input.Token,
|
2021-05-05 11:42:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err = git.PlainCloneContext(ctx, input.Dir, false, &cloneOptions)
|
2020-06-09 09:43:26 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Unable to clone %v %s: %v", input.URL, refName, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
|
|
|
|
if err = os.Chmod(input.Dir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-09 09:43:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:16:42 -05:00
|
|
|
func gitOptions(token string) (fetchOptions git.FetchOptions, pullOptions git.PullOptions) {
|
|
|
|
fetchOptions.RefSpecs = []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"}
|
|
|
|
pullOptions.Force = true
|
|
|
|
|
|
|
|
if token != "" {
|
|
|
|
auth := &http.BasicAuth{
|
|
|
|
Username: "token",
|
|
|
|
Password: token,
|
|
|
|
}
|
|
|
|
fetchOptions.Auth = auth
|
|
|
|
pullOptions.Auth = auth
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetchOptions, pullOptions
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
// NewGitCloneExecutor creates an executor to clone git repos
|
2022-10-06 17:09:43 -05:00
|
|
|
//
|
|
|
|
//nolint:gocyclo
|
2022-06-10 16:16:42 -05:00
|
|
|
func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
2022-06-10 16:16:42 -05:00
|
|
|
logger := common.Logger(ctx)
|
2020-02-11 11:10:35 -06:00
|
|
|
logger.Infof(" \u2601 git clone '%s' # ref=%s", input.URL, input.Ref)
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
cloneLock.Lock()
|
|
|
|
defer cloneLock.Unlock()
|
|
|
|
|
2019-01-15 11:05:27 -06:00
|
|
|
refName := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", input.Ref))
|
2021-04-06 08:43:02 -05:00
|
|
|
r, err := CloneIfRequired(ctx, refName, input, logger)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
2020-06-09 09:43:26 -05:00
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2021-05-07 22:29:03 -05:00
|
|
|
// fetch latest changes
|
2022-06-10 16:16:42 -05:00
|
|
|
fetchOptions, pullOptions := gitOptions(input.Token)
|
2021-05-18 01:25:07 -05:00
|
|
|
|
|
|
|
err = r.Fetch(&fetchOptions)
|
2021-05-07 22:29:03 -05:00
|
|
|
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
2019-01-12 22:45:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 22:29:03 -05:00
|
|
|
var hash *plumbing.Hash
|
|
|
|
rev := plumbing.Revision(input.Ref)
|
|
|
|
if hash, err = r.ResolveRevision(rev); err != nil {
|
|
|
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hash.String() != input.Ref && strings.HasPrefix(hash.String(), input.Ref) {
|
2022-06-10 16:16:42 -05:00
|
|
|
return &Error{
|
|
|
|
err: ErrShortRef,
|
|
|
|
commit: hash.String(),
|
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2021-01-12 00:47:33 -06:00
|
|
|
// 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
|
2021-01-14 23:24:17 -06:00
|
|
|
//
|
|
|
|
// If err is nil, it's a tag so let's proceed with that hash like we would if
|
|
|
|
// it was a sha
|
2021-01-12 00:47:33 -06:00
|
|
|
refType := "tag"
|
2021-05-07 22:29:03 -05:00
|
|
|
rev = plumbing.Revision(path.Join("refs", "tags", input.Ref))
|
2021-01-12 00:47:33 -06:00
|
|
|
if _, err := r.Tag(input.Ref); errors.Is(err, git.ErrTagNotFound) {
|
2021-02-23 11:50:28 -06:00
|
|
|
rName := plumbing.ReferenceName(path.Join("refs", "remotes", "origin", input.Ref))
|
|
|
|
if _, err := r.Reference(rName, false); errors.Is(err, plumbing.ErrReferenceNotFound) {
|
|
|
|
refType = "sha"
|
|
|
|
rev = plumbing.Revision(input.Ref)
|
|
|
|
} else {
|
|
|
|
refType = "branch"
|
|
|
|
rev = plumbing.Revision(rName)
|
|
|
|
}
|
2021-01-12 00:47:33 -06:00
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
|
|
|
|
if hash, err = r.ResolveRevision(rev); err != nil {
|
2020-06-09 09:43:26 -05:00
|
|
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 22:29:03 -05:00
|
|
|
var w *git.Worktree
|
|
|
|
if w, err = r.Worktree(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:43:26 -05:00
|
|
|
// If the hash resolved doesn't match the ref provided in a workflow then we're
|
|
|
|
// using a branch or tag ref, not a sha
|
|
|
|
//
|
|
|
|
// Repos on disk point to commit hashes, and need to checkout input.Ref before
|
|
|
|
// we try and pull down any changes
|
2021-05-07 22:29:03 -05:00
|
|
|
if hash.String() != input.Ref && 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))
|
|
|
|
if err = w.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: sourceRef,
|
|
|
|
Force: true,
|
|
|
|
}); err != nil {
|
|
|
|
logger.Errorf("Unable to checkout %s: %v", sourceRef, err)
|
|
|
|
return err
|
2020-06-09 09:43:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:16:42 -05:00
|
|
|
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
|
2020-03-06 12:17:20 -06:00
|
|
|
logger.Debugf("Unable to pull %s: %v", refName, err)
|
2019-01-15 19:41:02 -06:00
|
|
|
}
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Debugf("Cloned %s to %s", input.URL, input.Dir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2021-09-08 11:47:12 -05:00
|
|
|
if hash.String() != input.Ref && refType == "branch" {
|
|
|
|
logger.Debugf("Provided ref is not a sha. Updating branch ref after pull")
|
|
|
|
if hash, err = r.ResolveRevision(rev); err != nil {
|
|
|
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 22:29:03 -05:00
|
|
|
if err = w.Checkout(&git.CheckoutOptions{
|
2019-01-31 02:18:52 -06:00
|
|
|
Hash: *hash,
|
|
|
|
Force: true,
|
2021-05-07 22:29:03 -05:00
|
|
|
}); err != nil {
|
2020-02-24 14:48:12 -06:00
|
|
|
logger.Errorf("Unable to checkout %s: %v", *hash, err)
|
2019-01-12 22:45:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 22:29:03 -05:00
|
|
|
if err = w.Reset(&git.ResetOptions{
|
2020-02-20 19:47:21 -06:00
|
|
|
Mode: git.HardReset,
|
|
|
|
Commit: *hash,
|
2021-05-07 22:29:03 -05:00
|
|
|
}); err != nil {
|
2020-02-20 19:47:21 -06:00
|
|
|
logger.Errorf("Unable to reset to %s: %v", hash.String(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Debugf("Checked out %s", input.Ref)
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|