forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
278 lines (240 loc) · 8.36 KB
/
context.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/vcs"
"github.com/golang/dep/internal/fs"
"github.com/golang/dep/internal/gps"
"github.com/pkg/errors"
)
// Ctx defines the supporting context of dep.
type Ctx struct {
WorkingDir string // Where to execute.
GOPATH string // Selected Go path, containing WorkingDir.
GOPATHs []string // Other Go paths.
Out, Err *log.Logger // Required loggers.
Verbose bool // Enables more verbose logging.
}
// NewContext creates a struct containing path information and loggers.
func NewContext(wd string, gopaths []string, out, err *log.Logger, verbose bool) *Ctx {
ctx := &Ctx{
WorkingDir: wd,
Out: out,
Err: err,
Verbose: verbose,
}
for _, gp := range gopaths {
ctx.GOPATHs = append(ctx.GOPATHs, filepath.ToSlash(gp))
}
return ctx
}
func (c *Ctx) SourceManager() (*gps.SourceMgr, error) {
return gps.NewSourceManager(filepath.Join(c.GOPATH, "pkg", "dep"))
}
// LoadProject starts from the current working directory and searches up the
// directory tree for a project root. The search stops when a file with the name
// ManifestName (Gopkg.toml, by default) is located.
//
// The Project contains the parsed manifest as well as a parsed lock file, if
// present. The import path is calculated as the remaining path segment
// below Ctx.GOPATH/src.
func (c *Ctx) LoadProject() (*Project, error) {
var err error
p := new(Project)
p.AbsRoot, err = findProjectRoot(c.WorkingDir)
if err != nil {
return nil, err
}
// The path may lie within a symlinked directory, resolve the path
// before moving forward
p.AbsRoot, c.GOPATH, err = c.ResolveProjectRootAndGOPATH(p.AbsRoot)
if err != nil {
return nil, errors.Wrapf(err, "resolve project root")
} else if c.GOPATH == "" {
return nil, errors.New("project not within a GOPATH")
}
ip, err := c.SplitAbsoluteProjectRoot(p.AbsRoot)
if err != nil {
return nil, errors.Wrap(err, "split absolute project root")
}
p.ImportRoot = gps.ProjectRoot(ip)
mp := filepath.Join(p.AbsRoot, ManifestName)
mf, err := os.Open(mp)
if err != nil {
if os.IsNotExist(err) {
// TODO: list possible solutions? (dep init, cd $project)
return nil, errors.Errorf("no %v found in project root %v", ManifestName, p.AbsRoot)
}
// Unable to read the manifest file
return nil, err
}
defer mf.Close()
var warns []error
p.Manifest, warns, err = readManifest(mf)
for _, warn := range warns {
c.Err.Printf("dep: WARNING: %v\n", warn)
}
if err != nil {
return nil, errors.Errorf("error while parsing %s: %s", mp, err)
}
lp := filepath.Join(p.AbsRoot, LockName)
lf, err := os.Open(lp)
if err != nil {
if os.IsNotExist(err) {
// It's fine for the lock not to exist
return p, nil
}
// But if a lock does exist and we can't open it, that's a problem
return nil, errors.Errorf("could not open %s: %s", lp, err)
}
defer lf.Close()
p.Lock, err = readLock(lf)
if err != nil {
return nil, errors.Errorf("error while parsing %s: %s", lp, err)
}
return p, nil
}
// ResolveProjectRootAndGOPATH evaluates the project root and the containing GOPATH
// by doing the following:
//
// If path isn't a symlink and is within a GOPATH, path and its GOPATH are returned.
//
// If path is a symlink not within any GOPATH and resolves to a directory within a
// GOPATH, the resolved path and its GOPATH are returned.
//
// ResolveProjectRootAndGOPATH will return an error in the following cases:
//
// If path is not a symlink and it's not within any GOPATH.
// If both path and the directory it resolves to are not within any GOPATH.
// If path is a symlink within a GOPATH, an error is returned.
// If both path and the directory it resolves to are within the same GOPATH.
// If path and the directory it resolves to are each within a different GOPATH.
func (c *Ctx) ResolveProjectRootAndGOPATH(path string) (string, string, error) {
pgp, pgperr := c.detectGOPATH(path)
if sym, err := fs.IsSymlink(path); err != nil {
return "", "", errors.Wrap(err, "IsSymlink")
} else if !sym {
// If path is not a symlink and detectGOPATH() failed, then we assume that path is not
// within a known GOPATH.
if pgperr != nil {
return "", "", errors.Errorf("project root %v not within a GOPATH", path)
}
return path, pgp, nil
}
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
return "", "", errors.Wrap(err, "resolveProjectRoot")
}
rgp, rgperr := c.detectGOPATH(resolved)
if pgperr != nil && rgperr != nil {
return "", "", errors.Errorf("path %s resolved to %s, both are not within any GOPATH", path, resolved)
}
// If pgp equals rgp, then both are within the same GOPATH.
if pgp == rgp {
return "", "", errors.Errorf("path %s resolved to %s, both are in the same GOPATH %s", path, resolved, pgp)
}
// path and resolved are within different GOPATHs
if pgp != "" && rgp != "" && pgp == rgp {
return "", "", errors.Errorf("path %s resolved to %s, each is in a different GOPATH", path, resolved)
}
// Otherwise, either the symlink or the resolved path is within a GOPATH.
if pgp == "" {
return resolved, rgp, nil
}
return path, pgp, nil
}
// detectGOPATH detects the GOPATH for a given path from ctx.GOPATHs.
func (c *Ctx) detectGOPATH(path string) (string, error) {
for _, gp := range c.GOPATHs {
if fs.HasFilepathPrefix(filepath.FromSlash(path), gp) {
return gp, nil
}
}
return "", errors.Errorf("unable to detect GOPATH for %s", path)
}
// SplitAbsoluteProjectRoot takes an absolute path and compares it against declared
// GOPATH(s) to determine what portion of the input path should be treated as an
// import path - as a project root.
//
// The second returned string indicates which GOPATH value was used.
func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
if fs.HasFilepathPrefix(path, srcprefix) {
if len(path) <= len(srcprefix) {
return "", errors.New("dep does not currently support using $GOPATH/src as the project root.")
}
// filepath.ToSlash because we're dealing with an import path now,
// not an fs path
return filepath.ToSlash(path[len(srcprefix):]), nil
}
return "", errors.Errorf("%s not in any $GOPATH", path)
}
// absoluteProjectRoot determines the absolute path to the project root
// including the $GOPATH. This will not work with stdlib packages and the
// package directory needs to exist.
func (c *Ctx) absoluteProjectRoot(path string) (string, error) {
posspath := filepath.Join(c.GOPATH, "src", path)
dirOK, err := fs.IsDir(posspath)
if err != nil {
return "", errors.Wrapf(err, "checking if %s is a directory", posspath)
}
if !dirOK {
return "", errors.Errorf("%s does not exist", posspath)
}
return posspath, nil
}
func (c *Ctx) VersionInWorkspace(root gps.ProjectRoot) (gps.Version, error) {
pr, err := c.absoluteProjectRoot(string(root))
if err != nil {
return nil, errors.Wrapf(err, "determine project root for %s", root)
}
repo, err := vcs.NewRepo("", pr)
if err != nil {
return nil, errors.Wrapf(err, "creating new repo for root: %s", pr)
}
ver, err := repo.Current()
if err != nil {
return nil, errors.Wrapf(err, "finding current branch/version for root: %s", pr)
}
rev, err := repo.Version()
if err != nil {
return nil, errors.Wrapf(err, "getting repo version for root: %s", pr)
}
// First look through tags.
tags, err := repo.Tags()
if err != nil {
return nil, errors.Wrapf(err, "getting repo tags for root: %s", pr)
}
// Try to match the current version to a tag.
if contains(tags, ver) {
// Assume semver if it starts with a v.
if strings.HasPrefix(ver, "v") {
return gps.NewVersion(ver).Is(gps.Revision(rev)), nil
}
return nil, errors.Errorf("version for root %s does not start with a v: %q", pr, ver)
}
// Look for the current branch.
branches, err := repo.Branches()
if err != nil {
return nil, errors.Wrapf(err, "getting repo branch for root: %s")
}
// Try to match the current version to a branch.
if contains(branches, ver) {
return gps.NewBranch(ver).Is(gps.Revision(rev)), nil
}
return gps.Revision(rev), nil
}
// contains checks if a array of strings contains a value
func contains(a []string, b string) bool {
for _, v := range a {
if b == v {
return true
}
}
return false
}