-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
542 lines (480 loc) · 15 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package main
import (
"bytes"
"errors"
"fmt"
"html/template"
"os"
"os/exec"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// CUSTOM GIT WRAPPERS
// Construct an *exec.Cmd for `git {args}` with a workingDirectory
func (env *wikiEnv) gitCommand(args ...string) *exec.Cmd {
c := exec.Command(env.cfg.GitPath, args...)
c.Env = os.Environ()
c.Env = append(c.Env, `GIT_COMMITTER_NAME="`+env.cfg.GitCommitName+`"`, `GIT_COMMITTER_EMAIL="`+env.cfg.GitCommitEmail+`"`)
c.Dir = env.cfg.WikiDir
return c
}
// Execute `git init {directory}` in the current workingDirectory
func (env *wikiEnv) gitInit() error {
//wd, err := os.Getwd()
//if err != nil {
// return err
//}
return env.gitCommand("init").Run()
}
/*
// Execute `git clone [repo]` in the current workingDirectory
func gitClone(repo string) error {
//wd, err := os.Getwd()
//if err != nil {
// return err
//}
o, err := gitCommand("clone", repo, ".").CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git clone`: %s\n%s", err.Error(), string(o))
}
return nil
}
*/
// Execute `git status -s` in directory
// If there is output, the directory has is dirty
func (env *wikiEnv) gitIsClean() error {
gitBehind := []byte("Your branch is behind")
gitAhead := []byte("Your branch is ahead")
gitDiverged := []byte("have diverged")
// Check for untracked files first
u := env.gitCommand("ls-files", "--exclude-standard", "--others")
uo, err := u.Output()
if len(uo) != 0 {
return errors.New(string(uo))
}
// Fetch changes from remote
// Commented out for now; adds a solid second to load times!
/*
err = gitCommand("fetch").Run()
if err != nil {
return err
}
*/
// Now check the status, minus untracked files
c := env.gitCommand("status", "-uno")
o, err := c.Output()
if err != nil {
return err
}
if bytes.Contains(o, gitBehind) {
//return gitPull()
//return errors.New(string(o))
return errGitBehind
}
if bytes.Contains(o, gitAhead) {
//return gitPush()
//return errors.New(string(o))
return errGitAhead
}
if bytes.Contains(o, gitDiverged) {
//return errors.New(string(o))
return errGitDiverged
}
return nil
}
func (env *wikiEnv) gitIsCleanStartup() error {
gitBehind := []byte("Your branch is behind")
gitAhead := []byte("Your branch is ahead")
gitDiverged := []byte("have diverged")
// Check for untracked files first
u := env.gitCommand("ls-files", "--exclude-standard", "--others")
uo, err := u.Output()
if len(uo) != 0 {
return errors.New("Untracked files: " + string(uo))
}
if env.cfg.RemoteGitRepo != "" {
// Fetch changes from remote
err = env.gitCommand("fetch").Run()
if err != nil {
return err
}
}
// Now check the status, minus untracked files
c := env.gitCommand("status", "-uno")
o, err := c.Output()
if err != nil {
return err
}
if bytes.Contains(o, gitBehind) {
log.Debugln("gitIsCleanStartup: Pulling git repo...")
return env.gitPull()
//return errors.New(string(o))
//return ErrGitBehind
}
if bytes.Contains(o, gitAhead) {
if env.cfg.PushOnSave {
log.Debugln("gitIsCleanStartup: Pushing git repo...")
return env.gitPush()
}
return nil
//return errors.New(string(o))
//return ErrGitAhead
}
if bytes.Contains(o, gitDiverged) {
return errors.New(string(o))
//return ErrGitDiverged
}
return nil
}
// Execute `git add {filepath}` in workingDirectory
func (env *wikiEnv) gitAddFilepath(filepath string) error {
o, err := env.gitCommand("add", filepath).CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git add`: %s\n%s", err.Error(), string(o))
}
return nil
}
// Execute `git rm {filepath}` in workingDirectory
func (env *wikiEnv) gitRmFilepath(filepath string) error {
o, err := env.gitCommand("rm", filepath).CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git rm`: %s\n%s", err.Error(), string(o))
}
return nil
}
// Execute `git commit -m {msg}` in workingDirectory
func (env *wikiEnv) gitCommitWithMessage(msg string) error {
o, err := env.gitCommand("commit", "--author", "'Golang Wiki <golangwiki@jba.io>'", "-m", msg).CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git commit`: %s\n%s", err.Error(), string(o))
}
return nil
}
// Execute `git commit -m "commit from GoWiki"` in workingDirectory
func (env *wikiEnv) gitCommitEmpty() error {
o, err := env.gitCommand("commit", "--author", "'Golang Wiki <golangwiki@jba.io>'", "-m", "commit from GoWiki").CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git commit`: %s\n%s", err.Error(), string(o))
}
return nil
}
// Execute `git push` in workingDirectory
func (env *wikiEnv) gitPush() error {
o, err := env.gitCommand("push", "-u", "origin", "master").CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git push`: %s\n%s", err.Error(), string(o))
}
return nil
}
// Execute `git push` in workingDirectory
func (env *wikiEnv) gitPull() error {
o, err := env.gitCommand("pull").CombinedOutput()
if err != nil {
return fmt.Errorf("error during `git pull`: %s\n%s", err.Error(), string(o))
}
return nil
}
// File creation time, output to UNIX time
// git log --diff-filter=A --follow --format=%at -1 -- [filename]
func (env *wikiEnv) gitGetCtime(filename string) (int64, error) {
//var ctime int64
o, err := env.gitCommand("log", "--diff-filter=A", "--follow", "--format=%at", "-1", "--", filename).Output()
if err != nil {
return 0, fmt.Errorf("error during `git log --diff-filter=A --follow --format=at -1 --`: %s\n%s", err.Error(), string(o))
}
ostring := strings.TrimSpace(string(o))
// If output is blank, no point in wasting CPU doing the rest
if ostring == "" {
log.Println(filename + " is not checked into Git")
return 0, errNotInGit
}
ctime, err := strconv.ParseInt(ostring, 10, 64)
if err != nil {
panic(err)
}
return ctime, err
}
// File modification time, output to UNIX time
// git log -1 --format=%at -- [filename]
func (env *wikiEnv) gitGetMtime(filename string) (int64, error) {
//var mtime int64
o, err := env.gitCommand("log", "--format=%at", "-1", "--", filename).Output()
if err != nil {
return 0, fmt.Errorf("error during `git log -1 --format=at --`: %s\n%s", err.Error(), string(o))
}
ostring := strings.TrimSpace(string(o))
// If output is blank, no point in wasting CPU doing the rest
if ostring == "" {
log.Println(filename + " is not checked into Git")
return 0, nil
}
mtime, err := strconv.ParseInt(ostring, 10, 64)
if err != nil {
panic(err)
}
return mtime, err
}
type commitLog struct {
Filename string
Commit string
Date int64
Message string
}
// File history
// git log --pretty=format:"commit:%H date:%at message:%s" [filename]
// git log --pretty=format:"%H,%at,%s" [filename]
func (env *wikiEnv) gitGetFileLog(filename string) ([]commitLog, error) {
o, err := env.gitCommand("log", "--pretty=format:%H,%at,%s", filename).Output()
if err != nil {
return nil, fmt.Errorf("error during `git log`: %s\n%s", err.Error(), string(o))
}
// split each commit onto it's own line
logsplit := strings.Split(string(o), "\n")
// now split each commit-line into it's slice
// format should be: [sha1],[date],[message]
var commits []commitLog
for _, v := range logsplit {
//var theCommit *commitLog
var vs = strings.SplitN(v, ",", 3)
// Convert date to int64
var mtime, err = strconv.ParseInt(vs[1], 10, 64)
if err != nil {
panic(err)
}
// Now shortening the SHA1 to 7 digits, supposed to be the default git short sha output
shortsha := vs[0][0:7]
// vs[0] = commit, vs[1] = date, vs[2] = message
theCommit := commitLog{
Filename: filename,
Commit: shortsha,
Date: mtime,
Message: vs[2],
}
commits = append(commits, theCommit)
}
return commits, nil
}
// Get file as it existed at specific commit
// git show [commit sha1]:[filename]
func (env *wikiEnv) gitGetFileCommit(filename, commit string) ([]byte, error) {
// Combine these into one
fullcommit := commit + ":" + filename
o, err := env.gitCommand("show", fullcommit).CombinedOutput()
if err != nil {
return []byte{}, fmt.Errorf("error during `git show`: %s\n%s", err.Error(), string(o))
}
return o, nil
}
// Get diff for entire commit
// git show [commit sha1]
func (env *wikiEnv) gitGetFileCommitDiff(filename, commit string) ([]byte, error) {
o, err := env.gitCommand("show", commit).CombinedOutput()
if err != nil {
return []byte{}, fmt.Errorf("error during `git show`: %s\n%s", err.Error(), string(o))
}
return o, nil
}
// File modification time for specific commit, output to UNIX time
// git log -1 --format=%at [commit sha1]
func (env *wikiEnv) gitGetFileCommitMtime(commit string) (int64, error) {
//var mtime int64
o, err := env.gitCommand("log", "--format=%at", "-1", commit).Output()
if err != nil {
return 0, fmt.Errorf("error during `git log -1 --format=at --`: %s\n%s", err.Error(), string(o))
}
ostring := strings.TrimSpace(string(o))
mtime, err := strconv.ParseInt(ostring, 10, 64)
if err != nil {
panic(err)
}
return mtime, err
}
// git ls-files
func (env *wikiEnv) gitLs() ([]string, error) {
o, err := env.gitCommand("ls-files", "-z").Output()
if err != nil {
return nil, fmt.Errorf("error during `git ls-files`: %s\n%s", err.Error(), string(o))
}
nul := bytes.Replace(o, []byte("\x00"), []byte("\n"), -1)
// split each commit onto it's own line
lssplit := strings.Split(string(nul), "\n")
return lssplit, nil
}
// git ls-tree -r -t HEAD
func (env *wikiEnv) gitLsTree() ([]*gitDirList, error) {
o, err := env.gitCommand("ls-tree", "-r", "-t", "-z", "HEAD").Output()
if err != nil {
return nil, fmt.Errorf("error during `git ls-files`: %s\n%s", err.Error(), string(o))
}
if len(o) == 0 {
return []*gitDirList{}, nil
}
nul := bytes.Replace(o, []byte("\x00"), []byte("\n"), -1)
// split each commit onto it's own line
ostring := strings.TrimSpace(string(nul))
lssplit := strings.Split(ostring, "\n")
var dirList []*gitDirList
for _, v := range lssplit {
var vs = strings.SplitN(v, " ", 3)
var vs2 = strings.FieldsFunc(vs[2], func(c rune) bool { return c == '\t' })
theGitDirListing := &gitDirList{
Type: vs[1],
Filename: vs2[1],
}
dirList = append(dirList, theGitDirListing)
}
return dirList, nil
}
// git ls-tree HEAD:[dirname]
func (env *wikiEnv) gitLsTreeDir(dir string) ([]*gitDirList, error) {
o, err := env.gitCommand("ls-tree", "-z", "HEAD:"+dir).Output()
if err != nil {
return nil, fmt.Errorf("error during `git ls-files`: %s\n%s", err.Error(), string(o))
}
nul := bytes.Replace(o, []byte("\x00"), []byte("\n"), -1)
// split each commit onto it's own line
ostring := strings.TrimSpace(string(nul))
lssplit := strings.Split(ostring, "\n")
var dirList []*gitDirList
for _, v := range lssplit {
var vs = strings.Fields(v)
theGitDirListing := &gitDirList{
Type: vs[1],
Filename: vs[3],
}
dirList = append(dirList, theGitDirListing)
}
return dirList, nil
}
// git log --name-only --pretty=format:"%at %H" -z HEAD
func (env *wikiEnv) gitHistory() ([]string, error) {
o, err := env.gitCommand("log", "--name-only", "--pretty=format:_END %at %H", "-z", "HEAD").Output()
if err != nil {
return nil, fmt.Errorf("error during `git history`: %s\n%s", err.Error(), string(o))
}
// Get rid of first _END
o = bytes.Replace(o, []byte("_END"), []byte(""), 1)
o = bytes.Replace(o, []byte("\x00"), []byte("\n"), -1)
// Now remove all _END tags
b := bytes.SplitAfter(o, []byte("_END"))
var s []string
//var s *[]recent
for _, v := range b {
v = bytes.Replace(v, []byte("_END"), []byte(""), -1)
s = append(s, string(v))
}
return s, nil
}
// File creation time, output to UNIX time
// git log --diff-filter=A --follow --format=%at -1 -- [filename]
// File modification time, output to UNIX time
// git log -1 --format=%at -- [filename]
func (env *wikiEnv) gitGetTimes(filename string, ctime, mtime chan<- int64) {
go func() {
co, err := env.gitCommand("log", "--diff-filter=A", "--follow", "--format=%at", "-1", "--", filename).Output()
if err != nil {
log.Println(filename, err)
ctime <- 0
return
}
costring := strings.TrimSpace(string(co))
// If output is blank, no point in wasting CPU doing the rest
if costring == "" {
log.Println(filename + " is not checked into Git")
ctime <- 0
return
}
ctimeI, err := strconv.ParseInt(costring, 10, 64)
if err != nil {
ctime <- 0
return
}
ctime <- ctimeI
}()
go func() {
mo, err := env.gitCommand("log", "--format=%at", "-1", "--", filename).Output()
if err != nil {
log.Println(filename, err)
mtime <- 0
return
}
mostring := strings.TrimSpace(string(mo))
// If output is blank, no point in wasting CPU doing the rest
if mostring == "" {
log.Println(filename + " is not checked into Git")
mtime <- 0
return
}
mtimeI, err := strconv.ParseInt(mostring, 10, 64)
if err != nil {
mtime <- 0
return
}
mtime <- mtimeI
}()
}
func (env *wikiEnv) gitIsEmpty() bool {
// Run git rev-parse HEAD on the repo
// If it errors out, should mean it's empty
err := env.gitCommand("rev-parse", "HEAD").Run()
if err != nil {
return true
}
return false
}
// Search results, via git
// git grep --break 'searchTerm'
func (env *wikiEnv) gitSearch(searchTerm, fileSpec string) []result {
var results []result
quotedSearchTerm := `'` + searchTerm + `'`
cmd := exec.Command("/bin/sh", "-c", env.cfg.GitPath+" grep -i "+quotedSearchTerm+" -- "+fileSpec)
//o := gitCommand("grep", "omg -- 'index'")
cmd.Dir = env.cfg.WikiDir
o, err := cmd.CombinedOutput()
if err != nil {
return nil
}
// split each result onto it's own line
logsplit := strings.Split(string(o), "\n")
// now split each search result line into it's slice
// format should be: filename:searchresult
for _, v := range logsplit {
//var theCommit *commitLog
var vs = strings.SplitN(v, ":", 2)
//log.Println(len(vs))
// vs[0] = commit, vs[1] = date, vs[2] = message
if len(vs) == 2 {
theResult := result{
Name: vs[0],
Result: vs[1],
}
results = append(results, theResult)
}
}
// Check for matching filenames
listOfFiles := strings.Fields(fileSpec)
for _, v := range listOfFiles {
if strings.Contains(v, searchTerm) {
cleanV := strings.TrimPrefix(v, "\"")
cleanV = strings.TrimSuffix(cleanV, "\"")
theResult := result{
Name: cleanV,
}
results = append(results, theResult)
}
}
return results
}
func (env *wikiEnv) gitIsCleanURLs(token string) template.HTML {
switch env.gitIsClean() {
case errGitAhead:
return template.HTML(`<form method="post" action="/admin/git/push" id="git_push"><input type="hidden" name="csrf_token" value="` + token + `"><i class="fa fa-cloud-upload" aria-hidden="true"></i><button type="submit" class="button">Push git</button></form>`)
case errGitBehind:
return template.HTML(`<form method="post" action="/admin/git/pull" id="git_pull"><input type="hidden" name="csrf_token" value="` + token + `"><i class="fa fa-cloud-download" aria-hidden="true"></i><button type="submit" class="button">Pull git</button></form>`)
case errGitDiverged:
return template.HTML(`<a href="/admin/git"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i>Issue with git:wiki!</a>`)
default:
return template.HTML(`Git repo is clean.`)
}
}