-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.go
178 lines (155 loc) · 4.74 KB
/
backend.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
package gitremind
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/donomii/goof"
)
var workerChan chan string
var wg sync.WaitGroup
var Repos map[string][]string
func DoScan(scanDir string, verbose bool, autoSync bool) {
log.Println("Starting scan:", scanDir)
workerChan = make(chan string, 10)
// doneChan = make(chan bool)
go worker(workerChan, verbose, autoSync)
scanRepos(scanDir, workerChan)
wg.Wait()
//<-doneChan
//close(doneChan)
log.Println("Scan complete!")
}
func scanRepos(scanDir string, c chan string) {
var git_regex = regexp.MustCompile(`\.git`)
walkHandler := func(path string, info os.FileInfo, err error) error {
//fmt.Println(path)
if !git_regex.MatchString(path) {
wg.Add(1)
c <- path
}
return nil
}
//fmt.Println("These repositories need some attention:")
filepath.Walk(scanDir, walkHandler)
//close(c)
}
func grep(str string) string {
var out string
strs := strings.Split(str, "\n")
for _, v := range strs {
if strings.Index(v, "+") == 0 || strings.Index(v, "-") == 0 {
out = out + v + "\n"
}
}
return out
}
func worker(c chan string, verbose bool, autoSync bool) {
var ahead_regex = regexp.MustCompile(`Your branch is ahead of`)
var not_staged_regex = regexp.MustCompile(`Changes not staged for commit:`)
var staged_not_committed_regex = regexp.MustCompile(`Changes to be committed`)
var modified_regex = regexp.MustCompile(`modified:`)
var untracked_regex = regexp.MustCompile(`Untracked files:`)
var behind_regex = regexp.MustCompile(`Your branch is behind`)
var both_regex = regexp.MustCompile(`different commits each, respectively.`)
cwd, _ := os.Getwd()
for path := range c {
os.Chdir(cwd)
gitpath := fmt.Sprintf("%v/%v", path, ".git")
if goof.IsDir(gitpath) {
if verbose {
log.Println(gitpath)
}
os.Chdir(path)
cmd := exec.Command("git", "fetch")
goof.QuickCommand(cmd)
cmd = exec.Command("git", "status")
result, _ := goof.QuickCommand(cmd)
cmd = exec.Command("git", "status", "--porcelain")
shortresult, _ := goof.QuickCommand(cmd)
cmd = exec.Command("git", "diff", "--ignore-blank-lines")
diffresult, _ := goof.QuickCommand(cmd)
reasons := []string{}
longreasons := []string{}
if ahead_regex.MatchString(result) {
reasons = append(reasons, "push")
longreasons = append(longreasons, "local commits not pushed")
}
if behind_regex.MatchString(result) {
reasons = append(reasons, "pull")
longreasons = append(longreasons, "remote branch changed")
}
if both_regex.MatchString(result) {
reasons = append(reasons, "diverge")
longreasons = append(longreasons, "remote branch and local branch changed")
}
if modified_regex.MatchString(result) || not_staged_regex.MatchString(result) || staged_not_committed_regex.MatchString(result) {
reasons = append(reasons, "commit")
longreasons = append(longreasons, "changes not committed")
}
if untracked_regex.MatchString(result) {
reasons = append(reasons, "untracked")
longreasons = append(longreasons, "untracked files present")
}
if len(reasons) > 0 {
fmt.Printf("%v: %v\n", path, strings.Join(longreasons, ", "))
//fullPath := fmt.Sprintf("%v/%v", cwd, path)
if Repos == nil {
Repos = map[string][]string{}
}
Repos[path] = []string{path, shortresult, grep(diffresult), strings.Join(reasons, ", "), strings.Join(longreasons, ", "), result}
if verbose {
fmt.Println(result)
fmt.Printf("\n\n\n\n\n")
}
}
if autoSync {
fmt.Println("Syncing " + path)
cmd := exec.Command("git", "push")
goof.QuickCommand(cmd)
cmd = exec.Command("git", "pull")
goof.QuickCommand(cmd)
cmd = exec.Command("git", "push")
goof.QuickCommand(cmd)
}
}
os.Chdir(cwd)
wg.Done()
}
}
func CommitWithMessagePush(targetDir, message string) {
cwd, _ := os.Getwd()
fmt.Println("Current directory", cwd)
fmt.Println("Target directory", targetDir)
message = strings.Replace(message, "\r", "", -1) //Remove windows line endings
os.Chdir(targetDir)
messageLines := strings.Split(message, "\n")
//Searches a list of strings, return any that match search. Case insensitive
var compactMessages = []string{}
for _, v := range messageLines {
if len(v) > 0 && v[0] != '#' {
compactMessages = append(compactMessages, v)
}
}
if len(compactMessages) > 0 {
mess := strings.Join(compactMessages, "\n")
fmt.Printf("%v\n", []string{"git", "commit", "-a"})
goof.QCI([]string{"git", "commit", "-a", "-m", mess})
goof.QCI([]string{"git", "push"})
fmt.Println("Commit message:", mess)
} else {
fmt.Println("Not committing, due to empty message")
}
os.Chdir(cwd)
}
func RemoveRepo(repo string) {
delete(Repos, repo)
}
func ScanRepo(path string) {
wg.Add(1)
workerChan <- path
}