-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver_gitbinary.go
217 lines (178 loc) · 5.03 KB
/
driver_gitbinary.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
package gitdb
import (
"errors"
"os/exec"
"strings"
"time"
"github.com/bouggo/log"
)
type gitBinaryDriver struct {
config Config
absDBPath string
}
func (d *gitBinaryDriver) name() string {
return "gitBinary"
}
func (d *gitBinaryDriver) setup(db *gitdb) error {
d.config = db.config
d.absDBPath = db.dbDir()
return nil
}
func (d *gitBinaryDriver) init() error {
cmd := exec.Command("git", "-C", d.absDBPath, "init")
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Info(string(out))
return err
}
return nil
}
func (d *gitBinaryDriver) clone() error {
cmd := exec.Command("git", "clone", "--depth", "10", d.config.OnlineRemote, d.absDBPath)
if out, err := cmd.CombinedOutput(); err != nil {
return errors.New(string(out))
}
return nil
}
func (d *gitBinaryDriver) addRemote() error {
// check to see if we have origin / online remotes
cmd := exec.Command("git", "-C", d.absDBPath, "remote")
out, err := cmd.CombinedOutput()
if err != nil {
// log(string(out))
return err
}
remoteStr := string(out)
hasOriginRemote := strings.Contains(remoteStr, "origin")
hasOnlineRemote := strings.Contains(remoteStr, "online")
if hasOriginRemote {
cmd := exec.Command("git", "-C", d.absDBPath, "remote", "rm", "origin")
if out, err := cmd.CombinedOutput(); err != nil {
log.Info(string(out))
}
}
if !hasOnlineRemote {
cmd = exec.Command("git", "-C", d.absDBPath, "remote", "add", "online", d.config.OnlineRemote)
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
return errors.New(string(out))
}
}
return nil
}
func (d *gitBinaryDriver) sync() error {
if err := d.pull(); err != nil {
return err
}
if err := d.push(); err != nil {
return err
}
return nil
}
func (d *gitBinaryDriver) pull() error {
cmd := exec.Command("git", "-C", d.absDBPath, "pull", "online", "master")
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return errors.New("failed to pull data from online remote")
}
return nil
}
func (d *gitBinaryDriver) push() error {
cmd := exec.Command("git", "-C", d.absDBPath, "push", "online", "master")
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return errors.New("failed to push data to online remotes")
}
return nil
}
func (d *gitBinaryDriver) commit(filePath string, msg string, user *User) error {
cmd := exec.Command("git", "-C", d.absDBPath, "config", "user.email", user.Email)
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
cmd = exec.Command("git", "-C", d.absDBPath, "config", "user.name", user.Name)
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
cmd = exec.Command("git", "-C", d.absDBPath, "add", filePath)
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
cmd = exec.Command("git", "-C", d.absDBPath, "commit", "-am", msg)
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
log.Info("new changes committed")
return nil
}
func (d *gitBinaryDriver) undo() error {
cmd := exec.Command("git", "-C", d.absDBPath, "checkout", ".")
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
cmd = exec.Command("git", "-C", d.absDBPath, "clean", "-fd")
// log(utils.CmdToString(cmd))
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return err
}
log.Info("changes reverted")
return nil
}
func (d *gitBinaryDriver) changedFiles() []string {
var files []string
if len(d.config.OnlineRemote) > 0 {
log.Test("getting list of changed files...")
// git fetch
cmd := exec.Command("git", "-C", d.absDBPath, "fetch", "online", "master")
if out, err := cmd.CombinedOutput(); err != nil {
log.Error(string(out))
return files
}
// git diff --name-only ..online/master
cmd = exec.Command("git", "-C", d.absDBPath, "diff", "--name-only", "..online/master")
out, err := cmd.CombinedOutput()
if err != nil {
log.Error(string(out))
return files
}
output := string(out)
if len(output) > 0 {
// strip out lock files
for _, file := range strings.Split(output, "\n") {
if strings.HasSuffix(file, ".json") {
files = append(files, file)
}
}
return files
}
}
return files
}
func (d *gitBinaryDriver) lastCommitTime() (time.Time, error) {
var t time.Time
cmd := exec.Command("git", "-C", d.absDBPath, "log", "-1", "--remotes=online", "--format=%cd", "--date=iso")
// log.PutInfo(utils.CmdToString(cmd))
out, err := cmd.CombinedOutput()
if err != nil {
// log.PutError("gitLastCommit Failed")
return t, err
}
timeString := string(out)
if len(timeString) >= 25 {
return time.Parse("2006-01-02 15:04:05 -0700", timeString[:25])
}
return t, errors.New("no commit history in repo")
}