-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
57 lines (48 loc) · 1.04 KB
/
search.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
package main
import (
"bytes"
"io/fs"
"os/exec"
"path/filepath"
"regexp"
)
type repo struct {
path string
status string
}
var excludeDirs = regexp.MustCompile(`.+/(\..+|node_modules)`) // Skip hidden directories (incl. .git) and node_modules
func collectDirtyRepos(path string, sub chan repo) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}
err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && excludeDirs.MatchString(path) {
if filepath.Base(path) == ".git" {
repopath := filepath.Dir(path)
var buf = new(bytes.Buffer)
cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch")
cmd.Stdout = buf
cmd.Dir = repopath
err = cmd.Run()
if err != nil {
return err
}
gi := GitInfo{}
gi.Parse(buf)
if !gi.IsClean() {
sub <- repo{path: repopath, status: gi.Summary()}
}
}
return fs.SkipDir
}
return nil
})
if err != nil {
return err
}
return nil
}