forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dep.go
292 lines (267 loc) · 6.48 KB
/
dep.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
package main
import (
"code.google.com/p/go.tools/go/vcs"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
)
// Godeps describes what a package needs to be rebuilt reproducibly.
// It's the same information stored in file Godeps.
type Godeps struct {
ImportPath string
GoVersion string
Packages []string `json:",omitempty"` // Arguments to save, if any.
Deps []Dependency
outerRoot string
}
// A Dependency is a specific revision of a package.
type Dependency struct {
ImportPath string
Comment string `json:",omitempty"` // Description of commit, if present.
Rev string // VCS-specific commit ID.
// used by command save
pkg *Package
// used by command go
outerRoot string // dir, if present, in outer GOPATH
repoRoot *vcs.RepoRoot
vcs *VCS
}
// pkgs is the list of packages to read dependencies
func (g *Godeps) Load(pkgs []*Package) error {
var err1 error
var path, seen []string
for _, p := range pkgs {
if p.Standard {
log.Println("ignoring stdlib package:", p.ImportPath)
continue
}
if p.Error.Err != "" {
log.Println(p.Error.Err)
err1 = errors.New("error loading packages")
continue
}
seen = append(seen, p.ImportPath)
path = append(path, p.Deps...)
}
var testImports []string
for _, p := range pkgs {
testImports = append(testImports, p.TestImports...)
}
for _, p := range MustLoadPackages(testImports...) {
if p.Standard {
continue
}
if p.Error.Err != "" {
log.Println(p.Error.Err)
err1 = errors.New("error loading packages")
continue
}
path = append(path, p.ImportPath)
path = append(path, p.Deps...)
}
sort.Strings(path)
path = uniq(path)
for _, pkg := range MustLoadPackages(path...) {
if pkg.Error.Err != "" {
log.Println(pkg.Error.Err)
err1 = errors.New("error loading dependencies")
continue
}
if pkg.Standard {
continue
}
vcs, reporoot, err := VCSFromDir(pkg.Dir, pkg.Root)
if err != nil {
log.Println(err)
err1 = errors.New("error loading dependencies")
continue
}
if contains(seen, reporoot) {
continue
}
seen = append(seen, reporoot)
id, err := vcs.identify(pkg.Dir)
if err != nil {
log.Println(err)
err1 = errors.New("error loading dependencies")
continue
}
if vcs.isDirty(pkg.Dir, id) {
log.Println("dirty working tree:", pkg.Dir)
err1 = errors.New("error loading dependencies")
continue
}
comment := vcs.describe(pkg.Dir, id)
g.Deps = append(g.Deps, Dependency{
ImportPath: pkg.ImportPath,
Rev: id,
Comment: comment,
pkg: pkg,
vcs: vcs,
})
}
return err1
}
func ReadGodeps(path string) (*Godeps, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
g := new(Godeps)
err = json.NewDecoder(f).Decode(g)
if err != nil {
return nil, err
}
err = g.loadGoList()
if err != nil {
return nil, err
}
for i := range g.Deps {
d := &g.Deps[i]
d.vcs, d.repoRoot, err = VCSForImportPath(d.ImportPath)
if err != nil {
return nil, err
}
}
return g, nil
}
func (g *Godeps) loadGoList() error {
a := []string{g.ImportPath}
for _, d := range g.Deps {
a = append(a, d.ImportPath)
}
ps, err := LoadPackages(a...)
if err != nil {
return err
}
g.outerRoot = ps[0].Root
for i, p := range ps[1:] {
g.Deps[i].outerRoot = p.Root
}
return nil
}
func (g *Godeps) WriteTo(w io.Writer) (int, error) {
b, err := json.MarshalIndent(g, "", "\t")
if err != nil {
return 0, err
}
return w.Write(append(b, '\n'))
}
// Returns a path to the local copy of d's repository.
// E.g.
//
// ImportPath RepoPath
// github.com/kr/s3 $spool/github.com/kr/s3
// github.com/lib/pq/oid $spool/github.com/lib/pq
func (d Dependency) RepoPath() string {
return filepath.Join(spool, "repo", d.repoRoot.Root)
}
// Returns a URL for the remote copy of the repository.
func (d Dependency) RemoteURL() string {
return d.repoRoot.Repo
}
// Returns the url of a local disk clone of the repo, if any.
func (d Dependency) FastRemotePath() string {
if d.outerRoot != "" {
return d.outerRoot + "/src/" + d.repoRoot.Root
}
return ""
}
// Returns a path to the checked-out copy of d's commit.
func (d Dependency) Workdir() string {
return filepath.Join(d.Gopath(), "src", d.ImportPath)
}
// Returns a path to the checked-out copy of d's repo root.
func (d Dependency) WorkdirRoot() string {
return filepath.Join(d.Gopath(), "src", d.repoRoot.Root)
}
// Returns a path to a parent of Workdir such that using
// Gopath in GOPATH makes d available to the go tool.
func (d Dependency) Gopath() string {
return filepath.Join(spool, "rev", d.Rev[:2], d.Rev[2:])
}
// Creates an empty repo in d.RepoPath().
func (d Dependency) CreateRepo(fastRemote, mainRemote string) error {
if err := os.MkdirAll(d.RepoPath(), 0777); err != nil {
return err
}
if err := d.vcs.create(d.RepoPath()); err != nil {
return err
}
if err := d.link(fastRemote, d.FastRemotePath()); err != nil {
return err
}
return d.link(mainRemote, d.RemoteURL())
}
func (d Dependency) link(remote, url string) error {
return d.vcs.link(d.RepoPath(), remote, url)
}
func (d Dependency) fetchAndCheckout(remote string) error {
if err := d.fetch(remote); err != nil {
return fmt.Errorf("fetch: %s", err)
}
if err := d.checkout(); err != nil {
return fmt.Errorf("checkout: %s", err)
}
return nil
}
func (d Dependency) fetch(remote string) error {
return d.vcs.fetch(d.RepoPath(), remote)
}
func (d Dependency) checkout() error {
dir := d.WorkdirRoot()
if exists(dir) {
return nil
}
if !d.vcs.exists(d.RepoPath(), d.Rev) {
return fmt.Errorf("unknown rev %s for %s", d.Rev, d.ImportPath)
}
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
return d.vcs.checkout(dir, d.Rev, d.RepoPath())
}
func contains(a []string, s string) bool {
for _, p := range a {
if s == p {
return true
}
}
return false
}
func uniq(a []string) []string {
i := 0
s := ""
for _, t := range a {
if t != s {
a[i] = t
i++
s = t
}
}
return a[:i]
}
// mustGoVersion returns the version string of the Go compiler
// currently installed, e.g. "go1.1rc3".
func mustGoVersion() string {
// Godep might have been compiled with a different
// version, so we can't just use runtime.Version here.
cmd := exec.Command("go", "version")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
s := strings.TrimSpace(string(out))
s = strings.TrimSuffix(s, " "+runtime.GOOS+"/"+runtime.GOARCH)
s = strings.TrimPrefix(s, "go version ")
return s
}