-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
148 lines (130 loc) · 2.96 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
146
147
148
package vcs
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var (
gitFormat = "--format=" + beginCommit + "%H%n%h%n%an%n%ae%n%at%n%s%n%b%n" + endMessage
)
type git struct {
}
func (g *git) Cmd() string {
return "git"
}
func (g *git) Dir() string {
return ".git"
}
func (g *git) Test(dir string) bool {
gitDir := filepath.Join(dir, g.Dir())
if st, err := os.Stat(gitDir); err == nil && st.IsDir() {
return true
}
// Check a bare git repo
cfg := filepath.Join(dir, "config")
data, err := ioutil.ReadFile(cfg)
if err == nil {
return bytes.Contains(data, []byte("bare = true"))
}
return false
}
func (g *git) Head() string {
return "HEAD"
}
func (g *git) Revision(id string) []string {
return []string{"show", id, gitFormat}
}
func (g *git) History(since string) []string {
args := []string{"log", "-p"}
if since != "" {
args = append(args, since+"..HEAD")
}
args = append(args, gitFormat)
return args
}
func (g *git) Checkout(rev string) []string {
if rev == "" {
rev = "master"
}
return []string{"checkout", "-f", rev}
}
func (g *git) Clone(src string, dst string) []string {
return []string{"clone", src, dst}
}
func (g *git) Update() []string {
// --rebase fixes the local repo if upstream
// was rebased.
return []string{"pull", "-f", "--rebase"}
}
func (g *git) ParseRevisions(_ string, data []byte) ([]*Revision, error) {
commits := strings.Split(string(data), beginCommit)
revs := make([]*Revision, 0, len(commits))
for _, v := range commits {
if v == "" {
continue
}
lines := strings.Split(v, "\n")
ts, err := timestamp(lines[4])
if err != nil {
return nil, err
}
var msg []string
jj := 6
for ; jj < len(lines); jj++ {
if lines[jj] == endMessage {
break
}
msg = append(msg, lines[jj])
}
revs = append(revs, &Revision{
Identifier: lines[0],
ShortIdentifier: lines[1],
Subject: lines[5],
Message: strings.TrimSpace(strings.Join(msg, "\n")),
Author: lines[2],
Email: lines[3],
Diff: strings.TrimSpace(strings.Join(lines[jj+1:], "\n")),
Timestamp: ts,
})
}
return revs, nil
}
func (g *git) Branches() []string {
return []string{"show-ref", "--heads"}
}
func (g *git) Tags() []string {
return []string{"show-ref", "--tags"}
}
func (g *git) ParseBranches(data []byte) ([]*Branch, error) {
lines, err := splitTwoValuesLines(data)
if err != nil {
return nil, err
}
branches := make([]*Branch, len(lines))
for ii, v := range lines {
branches[ii] = &Branch{
Name: strings.TrimPrefix(v.second, "refs/heads/"),
Revision: v.first,
}
}
return branches, nil
}
func (g *git) ParseTags(data []byte) ([]*Tag, error) {
lines, err := splitTwoValuesLines(data)
if err != nil {
return nil, err
}
tags := make([]*Tag, len(lines))
for ii, v := range lines {
tags[ii] = &Tag{
Name: strings.TrimPrefix(v.second, "refs/tags/"),
Revision: v.first,
}
}
return tags, nil
}
func init() {
Register(&git{})
}