-
Notifications
You must be signed in to change notification settings - Fork 8
/
git.go
224 lines (186 loc) · 4.6 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"time"
)
const (
Error State = "error"
Dirty State = "dirty"
Ahead State = "ahead"
OutOfSync State = "out-of-sync"
Sync State = "sync"
)
type State string
type Git interface {
IsDirty(path string) (bool, error)
GetState(path string) (State, error)
Sync(path string) error
Update(path string) error
}
type GitCmd struct {
}
func (g *GitCmd) Sync(path string) error {
state, err := g.GetState(path)
log.Printf("Starting state: %s", state)
if err != nil {
return fmt.Errorf("performing GetState() failed. Err: %v", err)
}
for {
if state == Sync {
return nil
}
err = g.Update(path)
if err != nil {
return fmt.Errorf("performing Update() failed. Err: %v", err)
}
nextState, err := g.GetState(path)
if err != nil {
return fmt.Errorf("performing GetState() failed. Err: %v", err)
}
log.Printf("Next state: %s", nextState)
if state == nextState {
return fmt.Errorf("state doesn't change. Something is wrong")
}
state = nextState
}
}
func runCmd(path string, command string, args... string) (string, error) {
cmd := exec.Command(command, args...)
cmd.Dir = path
out, err := cmd.CombinedOutput()
return string(out), err
}
func (g *GitCmd) IsDirty(path string) (bool, error) {
out, err := runCmd(path, "git", "status", "--porcelain")
if err != nil {
return false, fmt.Errorf("unable to get status. Error: %v", err)
}
dirty := strings.TrimSpace(string(out)) != ""
return dirty, nil
}
func (g *GitCmd) GetState(path string) (State, error) {
log.Printf("Computing the state of %s", path)
dirty, err := g.IsDirty(path)
if err != nil {
return Error, fmt.Errorf("unable to get status. Error: %v", err)
}
if dirty {
return Dirty, nil
} else {
state, err := GetStateAgainstRemote(path)
if err != nil {
return Error, err
}
return state, nil
}
}
func ParseStatusBranch(status string) (State, error) {
// 5 variants of status branch
// ## master
// ## master...origin/master
// ## master...origin/master [ahead 1]
// ## master...origin/master [behind 1]
// ## master...origin/master [ahead 1, behind 1]
reg := regexp.MustCompile("## master(\\.\\.\\.origin\\/master *(\\[(ahead|behind) *[0-9]+ *(, *behind *[0-9]+)? *])?)?")
matches := reg.FindAllStringSubmatch(status, -1)
if len(matches) == 0 {
return Error, fmt.Errorf("unable to parse status: %v", status)
}
groups := matches[0]
if groups[0] == "" {
return Error, fmt.Errorf("unable to parse status: %v", status)
}
// ## master
if groups[1] == "" {
return Ahead, nil
}
// ## master...origin/master
if groups[2] == "" {
return Sync, nil
}
// ## master...origin/master
if groups[3] == "behind" {
return OutOfSync, nil
}
// ## master...origin/master
if groups[3] == "ahead" {
if groups[4] == "" {
return Ahead, nil
} else {
return OutOfSync, nil
}
}
return Error, fmt.Errorf("unable to parse status: %v", status)
}
func GetStateAgainstRemote(path string) (State, error) {
_, err := runCmd(path, "git", "fetch")
if err != nil {
return Error, fmt.Errorf("unable to fetch. Error: %v", err)
}
status, err := runCmd(path, "git", "status", "--branch", "--porcelain")
if err != nil {
return Error, fmt.Errorf("unable to fetch. Error: %v", err)
}
return ParseStatusBranch(status)
}
func (g *GitCmd) Update(path string) error {
state, err := g.GetState(path)
if err != nil {
return err
}
switch state {
case Error:
case Dirty:
err = AddAndCommit(path)
case Ahead:
err = Push(path)
case OutOfSync:
err = Merge(path)
case Sync:
}
return err
}
func AddAndCommit(path string) error {
err := Add(path)
if err != nil {
return err
}
return Commit(path)
}
func Merge(path string) error {
cmd := exec.Command("git", "merge", "origin/master", "--allow-unrelated-histories", "--no-commit")
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run() // Merge fails if there's conflict. So, we ignore the failure.
return nil
}
func Push(path string) error {
cmd := exec.Command("git", "push", "origin", "master", "-u")
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func Add(path string) error {
cmd := exec.Command("git", "add", "--all")
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func Commit(path string) error {
cmd := exec.Command("git", "-c", "user.name='Git notes'", "-c", "user.email='git-notes@noemail.com'", "commit", "-m", fmt.Sprintf("Commited at %v", time.Now()))
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func NewGoGit() GitCmd {
return GitCmd{}
}