-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
207 lines (178 loc) · 4.01 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
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
// Copyright 2017 David Lazar. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
var ignored = flag.Bool("ignored", false, "show ignored files")
type Walker struct {
Root string
gitRepo map[string]bool
skeleton map[string]bool
untracked map[string]bool
}
func main() {
flag.Parse()
if flag.NArg() < 1 {
fmt.Println("missing required argument (root path)")
return
}
walker := &Walker{
Root: flag.Arg(0),
gitRepo: make(map[string]bool),
skeleton: make(map[string]bool),
untracked: make(map[string]bool),
}
walker.findGitRepos()
walker.findUntracked()
walker.printStatus()
}
type LocalStatus int
const (
LocalClean LocalStatus = iota
LocalDirty
LocalUntracked
)
func (ls LocalStatus) icon() rune {
switch ls {
case LocalClean:
return ' '
case LocalDirty:
return '?'
case LocalUntracked:
return '!'
}
return 'X'
}
type Status struct {
LocalStatus LocalStatus
WorkingTreeStatus []byte
}
func (w *Walker) printStatus() {
statuses := make(map[string]Status)
for repo := range w.gitRepo {
out, err := gitStatus(repo)
if err != nil {
log.Fatalf("git status error: %s", err)
}
st := Status{
WorkingTreeStatus: out,
}
if len(out) == 0 {
st.LocalStatus = LocalClean
} else {
st.LocalStatus = LocalDirty
}
statuses[repo] = st
}
for path := range w.untracked {
statuses[path] = Status{
LocalStatus: LocalUntracked,
}
}
paths := make([]string, len(statuses))
i := 0
for path := range statuses {
paths[i] = path
i++
}
sort.Strings(paths)
for _, path := range paths {
st := statuses[path]
if st.LocalStatus == LocalClean {
continue
}
relpath, _ := filepath.Rel(w.Root, path)
fmt.Printf("%c %s\n", st.LocalStatus.icon(), relpath)
if len(st.WorkingTreeStatus) > 0 {
s := string(st.WorkingTreeStatus)
lines := strings.Split(s, "\n")
for i, line := range lines {
if line == "" {
continue
}
fmt.Printf(" %s\n", line)
if i >= 10 {
fmt.Printf(" (%d more lines)\n", len(lines)-i)
break
}
}
fmt.Println("")
}
}
}
func (w *Walker) findGitRepos() error {
return filepath.Walk(w.Root, w.walkGit)
}
// findUntracked should be called after findGitRepos.
func (w *Walker) findUntracked() error {
return filepath.Walk(w.Root, w.walkUntracked)
}
func (w *Walker) walkGit(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == ".git" {
repo := filepath.Dir(path) // drop .git suffix
w.gitRepo[repo] = true
// The repo's ancestors are skeleton directories: they provide structure
// that organizes git repos, and should not be considered untracked.
ancestors := repoAncestors(w.Root, repo)
for _, dir := range ancestors {
w.skeleton[dir] = true
}
// Skip the .git directory.
return filepath.SkipDir
}
return nil
}
func repoAncestors(root string, repoDir string) []string {
ancestors := make([]string, 0, 3)
curr := repoDir
for {
dir := filepath.Dir(curr)
ancestors = append(ancestors, dir)
if dir == "." || dir == root || dir == curr {
break
}
curr = dir
}
return ancestors
}
// walkUntracked finds files and directories that are not part of a git repository.
func (w *Walker) walkUntracked(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if w.gitRepo[path] {
return filepath.SkipDir
}
if w.skeleton[path] {
// Skeleton directories contain a git repo, so we should not mark
// the whole directory untracked. Instead, we recurse into the directory
// to find untracked paths.
return nil
}
w.untracked[path] = true
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
func gitStatus(repo string) ([]byte, error) {
args := []string{"-C", repo, "status", "--short", "--porcelain"}
if *ignored {
args = append(args, "--ignored")
}
cmd := exec.Command("git", args...)
return cmd.Output()
}