Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit b9395aa

Browse files
committed
Add -gopath flag to init
- Separate network mode and GOPATH mode of operations. - In GOPATH mode, fetch projectData by searching through the current GOPATH. Solve the dependency constraints based on the projects found on disk and use network to solve for projects not found on disk. - In Network mode, do not check GOPATH or any ondisk projects. Solve the dependency constraints completely over network.
1 parent d010781 commit b9395aa

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

cmd/dep/init.go

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ func (cmd *initCommand) Hidden() bool { return false }
4646

4747
func (cmd *initCommand) Register(fs *flag.FlagSet) {
4848
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
49+
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
4950
}
5051

5152
type initCommand struct {
5253
noExamples bool
54+
gopath bool
5355
}
5456

5557
func trimPathPrefix(p1, p2 string) string {
@@ -112,39 +114,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
112114
sm.UseDefaultSignalHandling()
113115
defer sm.Release()
114116

115-
pd, err := getProjectData(ctx, pkgT, cpr, sm)
116-
if err != nil {
117-
return err
118-
}
117+
// Create empty manifest, lock and projectData, and fill them according to
118+
// init operation modes. If gopath flag is set, fetch projectData by searching
119+
// through GOPATH, else operate in network mode, solve all the dependencies
120+
// over network.
119121
m := &dep.Manifest{
120-
Dependencies: pd.constraints,
122+
Dependencies: make(gps.ProjectConstraints),
121123
}
124+
l := &dep.Lock{}
125+
var pd projectData
122126

123-
// Make an initial lock from what knowledge we've collected about the
124-
// versions on disk
125-
l := &dep.Lock{
126-
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
127-
}
127+
if cmd.gopath {
128+
pd, err = getProjectData(ctx, pkgT, cpr, sm)
129+
if err != nil {
130+
return err
131+
}
128132

129-
for pr, v := range pd.ondisk {
130-
// That we have to chop off these path prefixes is a symptom of
131-
// a problem in gps itself
132-
pkgs := make([]string, 0, len(pd.dependencies[pr]))
133-
prslash := string(pr) + "/"
134-
for _, pkg := range pd.dependencies[pr] {
135-
if pkg == string(pr) {
136-
pkgs = append(pkgs, ".")
137-
} else {
138-
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
139-
}
133+
m.Dependencies = pd.constraints
134+
135+
// Make an initial lock from what knowledge we've collected about the
136+
// versions on disk
137+
l = &dep.Lock{
138+
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
140139
}
141140

142-
l.P = append(l.P, gps.NewLockedProject(
143-
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
144-
)
141+
for pr, v := range pd.ondisk {
142+
// That we have to chop off these path prefixes is a symptom of
143+
// a problem in gps itself
144+
pkgs := make([]string, 0, len(pd.dependencies[pr]))
145+
prslash := string(pr) + "/"
146+
for _, pkg := range pd.dependencies[pr] {
147+
if pkg == string(pr) {
148+
pkgs = append(pkgs, ".")
149+
} else {
150+
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
151+
}
152+
}
153+
154+
l.P = append(l.P, gps.NewLockedProject(
155+
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
156+
)
157+
}
145158
}
146159

147-
// Run solver with project versions found on disk
160+
// Run solver with the available knowledge to solve the dependency constraints
148161
internal.Vlogf("Solving...")
149162
params := gps.SolveParameters{
150163
RootDir: root,
@@ -170,14 +183,22 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
170183
}
171184
l = dep.LockFromInterface(soln)
172185

173-
// Pick notondisk project constraints from solution and add to manifest
174-
for k, _ := range pd.notondisk {
175-
for _, x := range l.Projects() {
176-
if k == x.Ident().ProjectRoot {
177-
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
178-
break
186+
// Populate manifest based on operation mode.
187+
if cmd.gopath {
188+
// Pick notondisk project constraints from solution and add to manifest
189+
for k, _ := range pd.notondisk {
190+
for _, x := range l.Projects() {
191+
if k == x.Ident().ProjectRoot {
192+
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
193+
break
194+
}
179195
}
180196
}
197+
} else {
198+
// Pick all the solved projects from the above solution and add to manifest
199+
for _, x := range l.Projects() {
200+
m.Dependencies[x.Ident().ProjectRoot] = getProjectPropertiesFromVersion(x.Version())
201+
}
181202
}
182203

183204
// Run gps.Prepare with appropriate constraint solutions from solve run

0 commit comments

Comments
 (0)