-
Notifications
You must be signed in to change notification settings - Fork 10
/
workspace.go
234 lines (213 loc) · 5.96 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
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
package main
import (
"fmt"
"go/build"
"log"
"strings"
"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|build.IgnoreVendor)
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,
}
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 {
// Repository not supported by vcsstate.
var pkg *Repo
w.reposMu.Lock()
if _, ok := w.repos[root]; !ok {
pkg = &Repo{
Path: bpkg.Dir,
Root: root,
vcsError: fmt.Errorf("%v not supported by vcsstate: %v", vcsCmd.Name, err),
}
w.repos[root] = pkg
}
w.reposMu.Unlock()
// If new package, display an error and send off to next stage.
if pkg != nil {
w.unique <- pkg
}
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
}
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 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 b, rev, remoteError := r.vcs.RemoteBranchAndRevision(r.Path); remoteError == nil {
r.Remote.Branch = b
r.Remote.Revision = rev
} else if remoteError == vcsstate.ErrNoRemote {
r.Remote.Branch = r.vcs.NoRemoteDefaultBranch()
} else if notFoundError, ok := remoteError.(vcsstate.NotFoundError); ok {
r.Remote.NotFound = notFoundError
r.Remote.Branch = r.vcs.NoRemoteDefaultBranch()
} else if remoteError != nil {
if b, err := r.vcs.CachedRemoteDefaultBranch(); err == nil {
r.Remote.Branch = b
} else {
log.Printf("%v: %v\n", r.Root, remoteError)
r.Remote.Branch = r.vcs.NoRemoteDefaultBranch() // It's a better fallback than empty string.
}
}
if rev, err := r.vcs.LocalRevision(r.Path, r.Remote.Branch); err == nil {
r.Local.Revision = rev
}
if r.Remote.Revision != "" {
if c, err := r.vcs.Contains(r.Path, r.Remote.Revision, r.Remote.Branch); err == nil {
r.Local.ContainsRemoteRevision = c
}
}
if r.Local.Revision != "" {
if c, err := r.vcs.RemoteContains(r.Path, r.Local.Revision, r.Remote.Branch); err == nil {
r.Remote.ContainsLocalRevision = c
} else if strings.Contains(err.Error(), "not implemented") && r.Local.Revision != r.Remote.Revision && r.Remote.Revision != "" {
// Fall back to using r.Local.ContainsRemoteRevision to deduct information.
// Assume that if local contains remote revision, then remote doesn't, and vice versa.
r.Remote.ContainsLocalRevision = !r.Local.ContainsRemoteRevision
}
}
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)
}
}