/
git.go
145 lines (124 loc) · 3.4 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package common
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/go-ini/ini"
"gopkg.in/yaml.v2"
)
func findGitRevision(file string) (string, error) {
gitDir, err := findGitDirectory(file)
if err != nil {
return "", err
}
head, err := findGitHead(file)
if err != nil {
return "", err
}
// load commitid ref
refBuf, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, head))
if err != nil {
return "", err
}
return string(string(refBuf)[:7]), nil
}
func findGitBranch(file string) (string, error) {
head, err := findGitHead(file)
if err != nil {
return "", err
}
// get branch name
branch := strings.TrimPrefix(head, "refs/heads/")
log.Debugf("Found branch: %s", branch)
return branch, nil
}
func findGitHead(file string) (string, error) {
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)
headBuffer.ReadFrom(bufio.NewReader(headFile))
head := make(map[string]string)
yaml.Unmarshal(headBuffer.Bytes(), head)
log.Debugf("HEAD points to '%s'", head["ref"])
return head["ref"], nil
}
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))
}