2019-01-12 22:45:25 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-02-10 17:27:05 -06:00
|
|
|
"io"
|
2019-01-12 22:45:25 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-ini/ini"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
git "gopkg.in/src-d/go-git.v4"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
)
|
|
|
|
|
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)?$`)
|
|
|
|
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+).git$`)
|
|
|
|
|
|
|
|
cloneLock sync.Mutex
|
|
|
|
)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
// FindGitRevision get the current git revision
|
|
|
|
func FindGitRevision(file string) (shortSha string, sha string, err error) {
|
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
bts, err := ioutil.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
|
2019-05-22 23:39:57 -05:00
|
|
|
refBuf, err = ioutil.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
|
|
|
|
|
|
|
log.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
|
|
|
|
func FindGitRef(file string) (string, error) {
|
2019-01-12 22:45:25 -06:00
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-05-22 23:39:57 -05:00
|
|
|
log.Debugf("Loading revision from git directory '%s'", gitDir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
_, ref, err := FindGitRevision(file)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:13:15 -06:00
|
|
|
log.Debugf("HEAD points to '%s'", ref)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-05-22 22:05:14 -05:00
|
|
|
// try tags first
|
2019-05-22 23:45:50 -05:00
|
|
|
tag, err := findGitPrettyRef(ref, gitDir, "refs/tags")
|
2019-05-22 22:05:14 -05:00
|
|
|
if err != nil || tag != "" {
|
|
|
|
return tag, err
|
|
|
|
}
|
|
|
|
// and then branches
|
2019-05-22 23:45:50 -05:00
|
|
|
return findGitPrettyRef(ref, gitDir, "refs/heads")
|
2019-05-22 22:05:14 -05:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:45:50 -05:00
|
|
|
func findGitPrettyRef(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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if name != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bts, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pointsTo = strings.TrimSpace(string(bts))
|
|
|
|
if head == pointsTo {
|
|
|
|
name = strings.TrimPrefix(strings.Replace(path, root, "", 1), "/")
|
|
|
|
log.Debugf("HEAD matches %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return name, err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindGithubRepo get the repo
|
|
|
|
func FindGithubRepo(file string) (string, error) {
|
|
|
|
url, err := findGitRemoteURL(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, slug, err := findGitSlug(url)
|
|
|
|
return slug, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func findGitRemoteURL(file string) (string, error) {
|
|
|
|
gitDir, err := findGitDirectory(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Debugf("Loading slug from git directory '%s'", gitDir)
|
|
|
|
|
|
|
|
gitconfig, err := ini.InsensitiveLoad(fmt.Sprintf("%s/config", gitDir))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
remote, err := gitconfig.GetSection("remote \"origin\"")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
urlKey, err := remote.GetKey("url")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
url := urlKey.String()
|
|
|
|
return url, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findGitSlug(url string) (string, string, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return "", url, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findGitDirectory(fromFile string) (string, error) {
|
|
|
|
absPath, err := filepath.Abs(fromFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Searching for git directory in %s", absPath)
|
|
|
|
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:\\" {
|
|
|
|
return "", errors.New("unable to find git repo")
|
|
|
|
}
|
|
|
|
|
|
|
|
return findGitDirectory(filepath.Dir(dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
|
|
|
type NewGitCloneExecutorInput struct {
|
2020-02-10 17:27:05 -06:00
|
|
|
URL string
|
|
|
|
Ref string
|
|
|
|
Dir string
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitCloneExecutor creates an executor to clone git repos
|
|
|
|
func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
2020-02-10 17:27:05 -06:00
|
|
|
logger := Logger(ctx)
|
|
|
|
logger.Infof("git clone '%s' # ref=%s", input.URL, input.Ref)
|
|
|
|
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-10 17:27:05 -06:00
|
|
|
if Dryrun(ctx) {
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneLock.Lock()
|
|
|
|
defer cloneLock.Unlock()
|
|
|
|
|
2019-01-15 11:05:27 -06:00
|
|
|
refName := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", input.Ref))
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
r, err := git.PlainOpen(input.Dir)
|
|
|
|
if err != nil {
|
2020-02-10 17:27:05 -06:00
|
|
|
var progressWriter io.Writer
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
r, err = git.PlainClone(input.Dir, false, &git.CloneOptions{
|
2019-01-31 02:18:52 -06:00
|
|
|
URL: input.URL,
|
2020-02-10 17:27:05 -06:00
|
|
|
Progress: progressWriter,
|
2019-01-31 02:18:52 -06:00
|
|
|
//ReferenceName: refName,
|
2019-01-12 22:45:25 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Errorf("Unable to clone %v %s: %v", input.URL, refName, err)
|
2019-01-12 22:45:25 -06:00
|
|
|
return err
|
|
|
|
}
|
2020-02-10 18:53:14 -06:00
|
|
|
_ = os.Chmod(input.Dir, 0755)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
w, err := r.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:41:02 -06:00
|
|
|
err = w.Pull(&git.PullOptions{
|
2019-01-31 02:18:52 -06:00
|
|
|
//ReferenceName: refName,
|
|
|
|
Force: true,
|
2019-01-15 11:57:36 -06:00
|
|
|
})
|
2019-01-16 12:10:24 -06:00
|
|
|
if err != nil && err.Error() != "already up-to-date" {
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Errorf("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
|
|
|
|
2019-01-31 02:18:52 -06:00
|
|
|
hash, err := r.ResolveRevision(plumbing.Revision(input.Ref))
|
|
|
|
if err != nil {
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
2019-01-31 02:18:52 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
2019-01-31 02:18:52 -06:00
|
|
|
//Branch: refName,
|
|
|
|
Hash: *hash,
|
|
|
|
Force: true,
|
2019-01-12 22:45:25 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-10 17:27:05 -06:00
|
|
|
logger.Errorf("Unable to checkout %s: %v", refName, err)
|
2019-01-12 22:45:25 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|