forked from mattn/goveralls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitinfo.go
106 lines (99 loc) · 2.56 KB
/
gitinfo.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
package main
import (
"log"
"os"
"os/exec"
"regexp"
"strings"
)
var remotesRE = regexp.MustCompile(`^(\S+)\s+(\S+)`)
// A Head object encapsulates information about the HEAD revision of a git repo.
type Head struct {
Id string `json:"id"`
AuthorName string `json:"author_name,omitempty"`
AuthorEmail string `json:"author_email,omitempty"`
CommitterName string `json:"committer_name,omitempty"`
CommitterEmail string `json:"committer_email,omitempty"`
Message string `json:"message"`
}
// A Remote object encapsulates information about a remote of a git repo.
type Remote struct {
Name string `json:"name"`
Url string `json:"url"`
}
// A Git object encapsulates information about a git repo.
type Git struct {
Head Head `json:"head"`
Branch string `json:"branch"`
Remotes []*Remote `json:"remotes,omitempty"`
}
// collectGitInfo runs several git commands to compose a Git object.
func collectGitInfo() *Git {
gitCmds := map[string][]string{
"id": {"git", "rev-parse", "HEAD"},
"branch": {"git", "rev-parse", "--abbrev-ref", "HEAD"},
"aname": {"git", "log", "-1", "--pretty=%aN"},
"aemail": {"git", "log", "-1", "--pretty=%aE"},
"cname": {"git", "log", "-1", "--pretty=%cN"},
"cemail": {"git", "log", "-1", "--pretty=%cE"},
"message": {"git", "log", "-1", "--pretty=%s"},
"remotes": {"git", "remote", "-v"},
}
results := map[string]string{}
remotes := map[string]Remote{}
gitPath, err := exec.LookPath("git")
if err != nil {
log.Fatal(err)
}
for key, args := range gitCmds {
if key == "branch" {
if envBranch := os.Getenv("GIT_BRANCH"); envBranch != "" {
results[key] = envBranch
continue
}
}
cmd := exec.Cmd{}
cmd.Path = gitPath
cmd.Args = args
cmd.Stderr = os.Stderr
ret, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
s := string(ret)
s = strings.TrimRight(s, "\n")
results[key] = s
}
for _, line := range strings.Split(results["remotes"], "\n") {
matches := remotesRE.FindAllStringSubmatch(line, -1)
if len(matches) != 1 {
continue
}
if len(matches[0]) != 3 {
continue
}
name := matches[0][1]
url := matches[0][2]
r := Remote{
Name: name,
Url: url,
}
remotes[name] = r
}
h := Head{
Id: results["id"],
AuthorName: results["aname"],
AuthorEmail: results["aemail"],
CommitterName: results["cname"],
CommitterEmail: results["cemail"],
Message: results["message"],
}
g := &Git{
Head: h,
Branch: results["branch"],
}
for _, r := range remotes {
g.Remotes = append(g.Remotes, &r)
}
return g
}