/
main.go
176 lines (149 loc) · 4.23 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/donomii/goof"
)
var targetDir string
var detailDisplay string = "Detail Disply Inital Value"
var autoSync bool
var verbose bool
var repos [][]string
var lastSelect string
var workerChan chan string
var doneChan chan bool
var winWidth = 900
var winHeight = 900
type Option uint8
type State struct {
prop int32
opt Option
}
func worker(c chan string) {
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.`)
repos = [][]string{}
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")
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, "track")
longreasons = append(longreasons, "untracked files present")
}
if len(reasons) > 0 {
fmt.Printf("%v: %v\n", path, strings.Join(longreasons, ", "))
repos = append(repos, []string{path, shortresult, grep(diffresult), strings.Join(reasons, ", "), strings.Join(longreasons, ", ")})
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)
}
}
os.Chdir(cwd)
}
doneChan <- true
}
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 CommitPush(targetDir string) {
cwd, _ := os.Getwd()
os.Chdir(targetDir)
fmt.Printf("%v\n", []string{"git", "commit", "-a"})
goof.QCI([]string{"git", "commit", "-a"})
goof.QCI([]string{"git", "push"})
os.Chdir(cwd)
}
func Pull(targetDir string) {
cwd, _ := os.Getwd()
os.Chdir(targetDir)
goof.QCI([]string{"git", "pull"})
os.Chdir(cwd)
}
func scanRepos(c chan string) {
var git_regex = regexp.MustCompile(`\.git`)
walkHandler := func(path string, info os.FileInfo, err error) error {
if !git_regex.MatchString(path) {
c <- path
}
return nil
}
//fmt.Println("These repositories need some attention:")
filepath.Walk(".", walkHandler)
close(c)
}
func doScan() {
workerChan = make(chan string, 10)
doneChan = make(chan bool)
go worker(workerChan)
scanRepos(workerChan)
<-doneChan
close(doneChan)
log.Println("Scan complete!")
}
func main() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
flag.BoolVar(&autoSync, "auto-sync", false, "Automatically push then pull on clean repositories")
flag.BoolVar(&verbose, "verbose", false, "Print details while working")
flag.Parse()
doScan()
fmt.Println("Done!")
}