forked from shurcooL/gostatus
/
workspace.go
196 lines (176 loc) · 4.51 KB
/
workspace.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
package main
import (
"fmt"
"go/build"
"sync"
"github.com/bradfitz/iter"
"github.com/shurcooL/vcsstate"
"golang.org/x/tools/go/vcs"
)
// workspace is a Go workspace environment; each repo has local and remote components.
type workspace struct {
ImportPaths chan string // ImportPaths is the input for Go packages to be processed.
unique chan *Repo // Unique repos.
processedFiltered chan *Repo // Processed repos, populated with local and remote state, filtered with shouldShow.
Statuses chan string // Statuses has results of running presenter on processed repos.
Errors chan error // Errors contains errors that were encountered during processing of repos.
shouldShow RepoFilter
presenter RepoPresenter
reposMu sync.Mutex
repos map[string]*Repo // Map key is the import path corresponding to the root of the repository or Go package.
}
func NewWorkspace(shouldShow RepoFilter, presenter RepoPresenter) *workspace {
w := &workspace{
ImportPaths: make(chan string, 64),
unique: make(chan *Repo, 64),
processedFiltered: make(chan *Repo, 64),
Statuses: make(chan string, 64),
Errors: make(chan error, 64),
shouldShow: shouldShow,
presenter: presenter,
repos: make(map[string]*Repo),
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.uniqueWorker(&wg)
}
go func() {
wg.Wait()
close(w.unique)
}()
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.processFilterWorker(&wg)
}
go func() {
wg.Wait()
close(w.processedFiltered)
}()
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.presenterWorker(&wg)
}
go func() {
wg.Wait()
close(w.Statuses)
close(w.Errors)
}()
}
return w
}
// uniqueWorker finds unique repos out of all input Go packages.
func (w *workspace) uniqueWorker(wg *sync.WaitGroup) {
defer wg.Done()
for importPath := range w.ImportPaths {
// Determine repo root.
// This is potentially somewhat slow.
bpkg, err := build.Import(importPath, wd, build.FindOnly)
if err != nil {
w.Errors <- err
continue
}
if bpkg.Goroot {
// gostatus has no support for printing status of packages in GOROOT, so skip those.
continue
}
vcsCmd, root, err := vcs.FromDir(bpkg.Dir, bpkg.SrcRoot)
if err != nil {
// Go package not under VCS.
var pkg *Repo
w.reposMu.Lock()
if _, ok := w.repos[bpkg.ImportPath]; !ok {
pkg = &Repo{
Path: bpkg.Dir,
Root: bpkg.ImportPath,
vcs: nil,
}
w.repos[bpkg.ImportPath] = pkg
}
w.reposMu.Unlock()
// If new package, send off to next stage.
if pkg != nil {
w.unique <- pkg
}
continue
}
vcs, err := vcsstate.NewVCS(vcsCmd)
if err != nil {
w.Errors <- fmt.Errorf("repo %v not supported by vcsstate: %v", root, err)
continue
}
var repo *Repo
w.reposMu.Lock()
if _, ok := w.repos[root]; !ok {
repo = &Repo{
Path: bpkg.Dir,
Root: root,
vcs: vcs,
}
w.repos[root] = repo
}
w.reposMu.Unlock()
// If new repo, send off to next stage.
if repo != nil {
w.unique <- repo
}
}
}
// processFilterWorker computes repository local and remote state, and filters with shouldShow.
func (w *workspace) processFilterWorker(wg *sync.WaitGroup) {
defer wg.Done()
for repo := range w.unique {
w.computeVCSState(repo)
if !w.shouldShow(repo) {
continue
}
w.processedFiltered <- repo
}
}
func (*workspace) computeVCSState(r *Repo) {
if r.vcs == nil {
// Go package not under VCS.
return
}
r.DefaultBranch = r.vcs.DefaultBranch()
if s, err := r.vcs.Status(r.Path); err == nil {
r.Local.Status = s
}
if b, err := r.vcs.Branch(r.Path); err == nil {
r.Local.Branch = b
}
if rev, err := r.vcs.LocalRevision(r.Path); err == nil {
r.Local.Revision = rev
}
if s, err := r.vcs.Stash(r.Path); err == nil {
r.Local.Stash = s
}
if remote, err := r.vcs.RemoteURL(r.Path); err == nil {
r.Local.RemoteURL = remote
}
if rev, err := r.vcs.RemoteRevision(r.Path); err == nil {
r.Remote.Revision = rev
}
if r.Remote.Revision != "" {
if c, err := r.vcs.Contains(r.Path, r.Remote.Revision); err == nil {
r.LocalContainsRemoteRevision = c
}
}
if rr, err := vcs.RepoRootForImportPath(r.Root, false); err == nil {
r.Remote.RepoURL = rr.Repo
}
}
// presenterWorker runs presenter on processed and filtered repos.
func (w *workspace) presenterWorker(wg *sync.WaitGroup) {
defer wg.Done()
for repo := range w.processedFiltered {
w.Statuses <- w.presenter(repo)
}
}