-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
57 lines (52 loc) · 1.42 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package util
import (
"fmt"
"log"
"os"
"regexp"
"strings"
)
func RepoFromRef(ref string) (url string, branch string) {
// given reference, for example, https://github.com/user/repo@branch-or-tag
// return repo url: https://github.com/user/repo, branch: branch-or-tag
githubPattern := regexp.MustCompile(`(?P<url>https?://[\w.-]+/[\w.-]+/[\w.-]+)(@(?P<branch>[^@]+))?$`)
match := githubPattern.FindStringSubmatch(ref)
if match == nil {
return
}
m := map[string]string{}
for i, n := range githubPattern.SubexpNames() {
if n != "" {
m[n] = match[i]
}
}
url = m["url"]
branch = m["branch"]
return
}
// CloneGitRepo(d.Repo, d.getRepoLocalPath())
func CloneGitRepo(ref string, path string) {
// split repo url from ref
log.Printf("Checking out repo %s...", ref)
url, branch := RepoFromRef(ref)
if url == "" {
log.Panicf("Invalid repo %s", ref)
}
log.Printf("Check if local repo exist by: %s", path)
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Printf("Clonning the repo %s...", url)
gitClone := fmt.Sprintf("git clone %s %s", url, path)
if branch != "" {
gitClone = fmt.Sprintf("git clone %s -b %s %s", url, branch, path)
}
Shell(gitClone)
} else {
log.Printf("Local git repo for %s already exists, skipping...", url)
}
}
func GetRepoHead(localPath string) string {
commit, _ := ShellOutput(
fmt.Sprintf("cd %v & git rev-parse --short HEAD", localPath),
)
return strings.TrimSpace(commit)
}