-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
446 lines (345 loc) · 9.82 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
package main
/**
* Git Repo Information
*/
import (
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
ui "github.com/gizak/termui"
walk "github.com/karrick/godirwalk"
)
////////////////////////////////////////////
// Utility: Git Repo Info
////////////////////////////////////////////
const GitRepoStatusUpdateInterval = 10 * time.Second
type RepoStatusField struct {
OutputCharacter rune
OutputColorString string
}
// Key is the git status rune (what shows up in `git status -sb`)
var RepoStatusFieldDefinitionsOrderedKeys = []rune{'M', 'A', 'D', 'R', 'C', 'U', '?', '!'}
var RepoStatusFieldDefinitions = map[rune]RepoStatusField{
// modified
'M': RepoStatusField{OutputCharacter: 'M', OutputColorString: "fg-green"},
// added
'A': RepoStatusField{OutputCharacter: '+', OutputColorString: "fg-green,fg-bold"},
// deleted
'D': RepoStatusField{OutputCharacter: '-', OutputColorString: "fg-red,fg-bold"},
// renamed
'R': RepoStatusField{OutputCharacter: 'R', OutputColorString: "fg-yellow,fg-bold"},
// copied
'C': RepoStatusField{OutputCharacter: 'C', OutputColorString: "fg-blue,fg-bold"},
// updated
'U': RepoStatusField{OutputCharacter: 'U', OutputColorString: "fg-magenta,fg-bold"},
// untracked
'?': RepoStatusField{OutputCharacter: '?', OutputColorString: "fg-red"},
// ignored
'!': RepoStatusField{OutputCharacter: '!', OutputColorString: "fg-cyan"},
}
type RepoInfo struct {
Name string
FullPath string
HomePath string
BranchStatus string
Status string
lastUpdated *time.Time
}
func NewRepoInfo(fullPath string) RepoInfo {
if strings.HasSuffix(fullPath, ".git") || strings.HasSuffix(fullPath, ".git/") {
// This is the path to the .git folder, so go up a level
fullPath = normalizePath(filepath.Join(fullPath, ".."))
}
// Repo name
name := filepath.Base(fullPath)
// Normalize path with home directory (if possible)
homePath := fullPath
if strings.HasPrefix(fullPath, HOME) {
relative, relErr := filepath.Rel(HOME, fullPath)
if relErr == nil {
homePath = filepath.Join("~", relative)
} else {
log.Printf("Error getting relative: %v", relErr)
}
} else if strings.HasPrefix(fullPath, CANONHOME) {
relative, relErr := filepath.Rel(CANONHOME, fullPath)
if relErr == nil {
homePath = filepath.Join("~", relative)
} else {
log.Printf("Error getting relative: %v", relErr)
}
}
// Load repo status
branches := "my branches"
status := "my status"
// Build it
r := RepoInfo{
Name: name,
FullPath: fullPath,
HomePath: homePath,
BranchStatus: branches,
Status: status,
}
r.update()
return r
}
func (w *RepoInfo) update() {
if shouldUpdate(w) {
// TODO: Make this not run a command to get this data
// Go do a git status in that folder
output, exitCode, err := execAndGetOutput("git", &w.FullPath, "-c", "color.status=never", "-c", "color.ui=never", "status", "-sb")
if err != nil {
log.Printf("Failed to get git output for repo %v (%v): %v", w.Name, w.FullPath, err)
} else if exitCode != 0 {
log.Printf("Bad exit code getting git output for repo %v (%v): %v", w.Name, w.FullPath, exitCode)
} else {
// Parse out the output
lines := strings.Split(output, "\n")
// Branch is first line
branchLine := lines[0][3:]
branchName := strings.Split(branchLine, " ")[0]
if strings.Contains(branchName, "...") {
branchName = strings.Split(branchName, "...")[0]
}
branchState := ""
if strings.Contains(branchLine, "[") {
branchState = "[" + strings.Split(branchLine, "[")[1]
}
nameColor := "fg-cyan"
if branchName == "master" || branchName == "mainline" {
nameColor = "fg-green"
}
w.BranchStatus = fmt.Sprintf("[%v](%s)", branchName, nameColor)
if len(branchState) > 0 {
w.BranchStatus += fmt.Sprintf(" [%v](fg-magenta)", branchState)
}
// Status for files follows, let's aggregate
status := make(map[rune]int, len(RepoStatusFieldDefinitions))
for field, _ := range RepoStatusFieldDefinitions {
status[field] = 0
}
for _, l := range lines[1:] {
l = strings.TrimSpace(l)
if len(l) < 2 {
continue
}
// Grab first two characters
statchars := l[:2]
for key := range status {
if strings.ContainsRune(statchars, key) {
status[key]++
}
}
}
w.Status = buildColoredStatusStringFromMap(status)
}
}
}
func (w *RepoInfo) getUpdateInterval() time.Duration {
return GitRepoStatusUpdateInterval
}
func (w *RepoInfo) getLastUpdated() *time.Time {
return w.lastUpdated
}
func (w *RepoInfo) setLastUpdated(t time.Time) {
w.lastUpdated = &t
}
type BySortOrder []*ui.Gauge
func (a BySortOrder) Len() int { return len(a) }
func (a BySortOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BySortOrder) Less(i, j int) bool { return a[i].BorderLabel < a[j].BorderLabel }
func buildColoredStatusStringFromMap(status map[rune]int) string {
retval := ""
for _, key := range RepoStatusFieldDefinitionsOrderedKeys {
count := status[key]
if count > 0 {
if retval != "" {
retval += " "
}
retval += fmt.Sprintf("[%c:%d](%s)", RepoStatusFieldDefinitions[key].OutputCharacter, count, RepoStatusFieldDefinitions[key].OutputColorString)
}
}
return retval
}
////////////////////////////////////////////
// Utility: Git Repo List
////////////////////////////////////////////
const GitRepoListUpdateInterval = 30 * time.Second
var HOME = os.ExpandEnv("$HOME")
var CANONHOME = normalizePath(HOME)
type CachedGitRepoList struct {
repoSearch map[string]int
Repos []RepoInfo
lastUpdated *time.Time
}
func (w *CachedGitRepoList) getUpdateInterval() time.Duration {
return GitRepoListUpdateInterval
}
func (w *CachedGitRepoList) getLastUpdated() *time.Time {
return w.lastUpdated
}
func (w *CachedGitRepoList) setLastUpdated(t time.Time) {
w.lastUpdated = &t
}
func (w *CachedGitRepoList) update() {
if shouldUpdate(w) {
repoPaths := getGitRepositories(w.repoSearch)
repos := make([]RepoInfo, 0)
for _, repo := range repoPaths {
repoInfo := NewRepoInfo(repo)
repos = append(repos, repoInfo)
}
w.Repos = repos
}
// Update status for all the repos as well
for _, r := range w.Repos {
r.update()
}
}
func NewCachedGitRepoList(search map[string]int) *CachedGitRepoList {
// Build it
w := &CachedGitRepoList{
repoSearch: search,
Repos: make([]RepoInfo, 0),
}
w.update()
return w
}
var cachedGitRepos = NewCachedGitRepoList(GetGitRepoSearchPaths())
// Walks the search directories to look for git folders
// search is a map of directory roots to depths
func getGitRepositories(search map[string]int) []string {
var retval = make([]string, 0)
for path, depth := range search {
gitRepos := getGitRepositoriesForPath(path, depth)
retval = append(retval, gitRepos...)
}
// Sort
sort.Strings(retval)
// Uniquify
// w is where non-matching elements should be written
// last is the last element we wrote
// r is the current read pointer
w := 1
last := 0
for r := 1; r < len(retval); r++ {
// If they're the same, skip it
if retval[r] == retval[last] {
continue
}
// They're different, write it to the array
retval[w] = retval[r]
// Save last pointer
last = w
// Advance
w++
}
retval = retval[:w] // slice it to just what we wrote
return retval
}
func getGitRepositoriesForPath(root string, maxDepth int) []string {
var retval = walkTreeLookingForGit(root, nil, 0, maxDepth)
return retval
}
func walkTreeLookingForGit(path string, de *walk.Dirent, curDepth int, maxDepth int) []string {
// Do we keep going?
if curDepth <= maxDepth {
// de is nil the first time through
if de != nil {
gitPath := checkAndResolveGitFolder(path, de)
if gitPath != nil {
// Got it!
return []string{*gitPath}
}
}
// Get children
retval := make([]string, 0)
kids, err := walk.ReadDirents(path, nil)
if err != nil {
log.Printf("Failed to traverse into children of '%v': %v", path, err)
} else {
for _, kidDE := range kids {
if kidDE.IsDir() {
results := walkTreeLookingForGit(filepath.Join(path, kidDE.Name()), kidDE, curDepth+1, maxDepth)
retval = append(retval, results...)
}
}
}
return retval
} else {
return []string{}
}
}
// Returns nil if not a git folder
// Returns a resolved pathname if is a git folder
func checkAndResolveGitFolder(osPathname string, de *walk.Dirent) *string {
// check name
if !de.IsDir() {
return nil
}
if de.Name() != ".git" {
return nil
}
path := normalizePath(osPathname)
return &path
}
////////////////////////////////////////////
// Widget: Git Repos
////////////////////////////////////////////
const MinimumRepoNameWidth = 26
const MinimumRepoBranchesWidth = 37
type GitRepoWidget struct {
widget *ui.Table
lastUpdated *time.Time
}
func NewGitRepoWidget() *GitRepoWidget {
// Create base element
e := ui.NewTable()
e.Border = true
e.BorderLabel = "Git Repos"
e.Separator = false
// Create widget
w := &GitRepoWidget{
widget: e,
}
w.update()
w.resize()
return w
}
func (w *GitRepoWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *GitRepoWidget) update() {
rows := [][]string{}
height := 2
// Load repos
cachedGitRepos.update()
maxRepoWidth := 0
for _, repo := range cachedGitRepos.Repos {
// Figure out max length
if len(repo.HomePath) > maxRepoWidth {
maxRepoWidth = len(repo.HomePath)
}
}
if maxRepoWidth < MinimumRepoNameWidth {
maxRepoWidth = MinimumRepoNameWidth
}
for _, repo := range cachedGitRepos.Repos {
// Make the name all fancy
pathPad := maxRepoWidth - len(repo.Name)
path := filepath.Dir(repo.HomePath)
name := fmt.Sprintf("[%*v%c](fg-cyan)[%v](fg-cyan,fg-bold)", pathPad, path, os.PathSeparator, repo.Name)
line := []string{name, repo.BranchStatus, repo.Status}
rows = append(rows, line)
height++
}
w.widget.Rows = rows
w.widget.Height = height
}
func (w *GitRepoWidget) resize() {
// Do nothing
}