2019-01-12 22:45:25 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"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-01-16 12:10:24 -06:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var cloneLock sync.Mutex
|
|
|
|
|
|
|
|
// 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-01-18 15:28:53 -06:00
|
|
|
ref, err := FindGitRef(file)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
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-01-18 15:28:53 -06:00
|
|
|
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", 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-01-15 19:41:02 -06:00
|
|
|
return string(refBuf[:7]), string(refBuf), nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindGitBranch get the current git branch
|
|
|
|
func FindGitBranch(file string) (string, error) {
|
2019-01-18 15:28:53 -06:00
|
|
|
ref, err := FindGitRef(file)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get branch name
|
2019-01-18 15:28:53 -06:00
|
|
|
branch := strings.TrimPrefix(ref, "refs/heads/")
|
2019-01-12 22:45:25 -06:00
|
|
|
log.Debugf("Found branch: %s", branch)
|
|
|
|
return branch, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
log.Debugf("Loading revision from git directory '%s'", gitDir)
|
|
|
|
|
|
|
|
// load HEAD ref
|
|
|
|
headFile, err := os.Open(fmt.Sprintf("%s/HEAD", gitDir))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
headFile.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
headBuffer := new(bytes.Buffer)
|
2019-01-17 17:28:52 -06:00
|
|
|
_, err = headBuffer.ReadFrom(bufio.NewReader(headFile))
|
2019-01-15 19:41:02 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-01-17 17:28:52 -06:00
|
|
|
headBytes := headBuffer.Bytes()
|
2019-01-17 16:13:15 -06:00
|
|
|
|
|
|
|
var ref string
|
2019-01-17 17:28:52 -06:00
|
|
|
head := make(map[string]string)
|
|
|
|
err = yaml.Unmarshal(headBytes, head)
|
|
|
|
if err != nil {
|
2019-01-17 17:53:03 -06:00
|
|
|
ref = strings.TrimRight(string(headBytes), "\r\n")
|
2019-01-17 16:13:15 -06:00
|
|
|
} else {
|
|
|
|
ref = head["ref"]
|
2019-01-15 19:41:02 -06:00
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-17 16:13:15 -06:00
|
|
|
log.Debugf("HEAD points to '%s'", ref)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-17 16:13:15 -06:00
|
|
|
return ref, nil
|
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) {
|
|
|
|
codeCommitHTTPRegex := regexp.MustCompile(`^http(s?)://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
|
|
|
codeCommitSSHRegex := regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
|
|
|
httpRegex := regexp.MustCompile("^http(s?)://.*github.com.*/(.+)/(.+).git$")
|
|
|
|
sshRegex := regexp.MustCompile("github.com:(.+)/(.+).git$")
|
|
|
|
|
|
|
|
if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "CodeCommit", matches[3], nil
|
|
|
|
} else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "CodeCommit", matches[2], nil
|
|
|
|
} else if matches := httpRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
return "GitHub", fmt.Sprintf("%s/%s", matches[2], matches[3]), nil
|
|
|
|
} else if matches := sshRegex.FindStringSubmatch(url); matches != nil {
|
|
|
|
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 {
|
|
|
|
dir = path.Dir(absPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitPath := path.Join(dir, ".git")
|
|
|
|
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 {
|
|
|
|
URL *url.URL
|
|
|
|
Ref string
|
|
|
|
Dir string
|
|
|
|
Logger *log.Entry
|
|
|
|
Dryrun bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitCloneExecutor creates an executor to clone git repos
|
|
|
|
func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
|
|
|
|
return func() error {
|
|
|
|
input.Logger.Infof("git clone '%s'", input.URL.String())
|
|
|
|
input.Logger.Debugf(" cloning %s to %s", input.URL.String(), input.Dir)
|
|
|
|
|
|
|
|
if input.Dryrun {
|
|
|
|
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 {
|
|
|
|
r, err = git.PlainClone(input.Dir, false, &git.CloneOptions{
|
2019-01-15 11:05:27 -06:00
|
|
|
URL: input.URL.String(),
|
|
|
|
Progress: input.Logger.WriterLevel(log.DebugLevel),
|
|
|
|
ReferenceName: refName,
|
2019-01-12 22:45:25 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := r.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:41:02 -06:00
|
|
|
err = w.Pull(&git.PullOptions{
|
2019-01-15 11:57:36 -06:00
|
|
|
ReferenceName: refName,
|
2019-01-16 12:10:24 -06:00
|
|
|
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" {
|
2019-01-15 19:41:02 -06:00
|
|
|
input.Logger.Errorf("Unable to pull %s: %v", refName, err)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
input.Logger.Debugf("Cloned %s to %s", input.URL.String(), input.Dir)
|
|
|
|
|
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
2019-01-15 11:05:27 -06:00
|
|
|
Branch: refName,
|
2019-01-16 12:10:24 -06:00
|
|
|
Force: true,
|
2019-01-12 22:45:25 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-01-15 11:05:27 -06:00
|
|
|
input.Logger.Errorf("Unable to checkout %s: %v", refName, err)
|
2019-01-12 22:45:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input.Logger.Debugf("Checked out %s", input.Ref)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|