/
git.go
242 lines (214 loc) · 5.61 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Package git provides methods to execute raw git commands
// in the current folder.
package git
import (
"errors"
"fmt"
"github.com/tbruyelle/qexec"
"os"
"regexp"
"strings"
"sync"
)
var mutex sync.Mutex
// Repository exposes the same functions than the package but removes the
// constraint of having the CWD in the repository path.
// Each commands will change the CWD and lock a mutex until the command is done.
// This should make the thing works in goroutines.
type Repository struct {
Path string
}
func (r Repository) AddRemote(name, url string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return AddRemote(name, url)
}
func (r Repository) Remote(name string) (string, error) {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Remote(name)
}
func (r Repository) Branch() (string, error) {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Branch()
}
func (r Repository) Pull(remote string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Pull(remote)
}
func (r Repository) Fetch(remote string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Fetch(remote)
}
func (r Repository) Checkout(ref, remote string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Checkout(ref, remote)
}
func (r Repository) Merge(ref string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return Merge(ref)
}
func (r Repository) ResetHard(ref string) error {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return ResetHard(ref)
}
func (r Repository) HasLocalDiff() (bool, error) {
mutex.Lock()
defer mutex.Unlock()
os.Chdir(r.Path)
return HasLocalDiff()
}
// AddRemote add a new remote to the repository.
func AddRemote(name, url string) error {
_, err := qexec.Run("git", "remote", "add", name, url)
return err
}
// Pull executes the git pull command
func Pull(remote string) error {
_, err := qexec.Run("git", "pull", remote)
return err
}
// Remote returns the requested remote url
func Remote(name string) (string, error) {
remote, err := qexec.Run("git", "config", "--get", fmt.Sprintf("remote.%s.url", name))
if err != nil {
return "", err
}
return strings.TrimSpace(remote), err
}
// Branch returns the current branch.
func Branch() (string, error) {
branch, err := qexec.Run("git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return "", err
}
return strings.TrimSpace(branch), nil
}
// RevParse executes the git rev-parse command.
func RevParse(arg string) (string, error) {
ref, err := qexec.Run("git", "rev-parse", "-q", arg)
if err != nil {
return "", nil
}
return strings.TrimSpace(ref), nil
}
// Checkout executes the git checkout command.
// If remote is specified, first delete the local branch if any
// and recreate according to that remote.
func Checkout(ref, remote string) (err error) {
if remote == "" {
_, err = qexec.Run("git", "checkout", ref)
return
}
qexec.Run("git", "branch", "-D", ref)
_, err = qexec.Run("git", "checkout", "-b", ref, remote)
return
}
// Merge executes the git merge command.
func Merge(ref string) error {
_, err := qexec.Run("git", "merge", ref)
return err
}
// Fetch executes the git fetch command.
func Fetch(remote string) error {
_, err := qexec.Run("git", "fetch", remote)
return err
}
// HasLocalDiff returns true if repo has local modifications.
func HasLocalDiff() (bool, error) {
_, err := qexec.Run("git", "diff", "--quiet", "HEAD")
status, err := qexec.ExitStatus(err)
if err != nil {
return false, err
}
return status != 0, nil
}
// RefExists checks if the ref exists in the repository.
func RefExists(ref string) (bool, error) {
_, err := qexec.Run("git", "rev-parse", "--quiet", "--verify", ref)
if err != nil {
status, err := qexec.ExitStatus(err)
if err != nil || status != 1 {
return false, err
}
return false, nil
}
return true, nil
}
// ResetHard executes the git reset --hard command.
func ResetHard(ref string) error {
_, err := qexec.Run("git", "reset", "--hard", ref)
return err
}
type Commit struct {
Ref string
Message string
}
// Log returns the list of commits from the requested range.
func Log(start, end string) ([]Commit, error) {
output, err := qexec.Run("git", "log", start+".."+end, "--oneline")
if err != nil {
return nil, err
}
lines := strings.Split(strings.TrimSpace(output), "\n")
commits := make([]Commit, 0)
for _, line := range lines {
tokens := strings.SplitN(line, " ", 2)
if len(tokens) != 2 {
continue
}
commits = append(commits, Commit{tokens[0], tokens[1]})
}
return commits, nil
}
// RemoteOrigin returns structured data about the origin remote.
func RemoteOrigin() (*RemoteInfo, error) {
remote, err := Remote("origin")
if err != nil {
return nil, err
}
return parseRemote(remote)
}
type RemoteInfo struct {
Owner, Name string
}
var remoteGitUrlSsh = regexp.MustCompile("git@\\S+:(\\w+)/(\\w+)(\\.git)?")
var remoteGitUrlHttp = regexp.MustCompile("https?://\\S+/(\\w+)/(\\w+)(\\.git)?")
func parseRemote(remote string) (*RemoteInfo, error) {
if strings.Index(remote, "http") != -1 {
res := remoteGitUrlHttp.FindAllStringSubmatch(remote, -1)
if len(res) == 0 || len(res[0]) < 3 {
return nil, errors.New("Unable to parse remote " + remote)
}
r := &RemoteInfo{
Owner: res[0][1],
Name: res[0][2],
}
return r, nil
} else if strings.Index(remote, "git@") != -1 {
res := remoteGitUrlSsh.FindAllStringSubmatch(remote, -1)
if len(res) == 0 || len(res[0]) < 3 {
return nil, errors.New("Unable to parse remote " + remote)
}
repo := &RemoteInfo{
Owner: res[0][1],
Name: res[0][2],
}
return repo, nil
}
return nil, errors.New("Unhandled remote " + remote)
}