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

Commit d3c8160

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 aef755e commit d3c8160

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
@@ -39,10 +39,12 @@ func (cmd *initCommand) Hidden() bool { return false }
3939

4040
func (cmd *initCommand) Register(fs *flag.FlagSet) {
4141
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
42+
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
4243
}
4344

4445
type initCommand struct {
4546
noExamples bool
47+
gopath bool
4648
}
4749

4850
func trimPathPrefix(p1, p2 string) string {
@@ -105,39 +107,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
105107
sm.UseDefaultSignalHandling()
106108
defer sm.Release()
107109

108-
pd, err := getProjectData(ctx, pkgT, cpr, sm)
109-
if err != nil {
110-
return err
111-
}
110+
// Create empty manifest, lock and projectData, and fill them according to
111+
// init operation modes. If gopath flag is set, fetch projectData by searching
112+
// through GOPATH, else operate in network mode, solve all the dependencies
113+
// over network.
112114
m := &dep.Manifest{
113-
Dependencies: pd.constraints,
115+
Dependencies: make(gps.ProjectConstraints),
114116
}
117+
l := &dep.Lock{}
118+
var pd projectData
115119

116-
// Make an initial lock from what knowledge we've collected about the
117-
// versions on disk
118-
l := &dep.Lock{
119-
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
120-
}
120+
if cmd.gopath {
121+
pd, err = getProjectData(ctx, pkgT, cpr, sm)
122+
if err != nil {
123+
return err
124+
}
121125

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

135-
l.P = append(l.P, gps.NewLockedProject(
136-
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
137-
)
134+
for pr, v := range pd.ondisk {
135+
// That we have to chop off these path prefixes is a symptom of
136+
// a problem in gps itself
137+
pkgs := make([]string, 0, len(pd.dependencies[pr]))
138+
prslash := string(pr) + "/"
139+
for _, pkg := range pd.dependencies[pr] {
140+
if pkg == string(pr) {
141+
pkgs = append(pkgs, ".")
142+
} else {
143+
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
144+
}
145+
}
146+
147+
l.P = append(l.P, gps.NewLockedProject(
148+
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
149+
)
150+
}
138151
}
139152

140-
// Run solver with project versions found on disk
153+
// Run solver with the available knowledge to solve the dependency constraints
141154
internal.Vlogf("Solving...")
142155
params := gps.SolveParameters{
143156
RootDir: root,
@@ -163,14 +176,22 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
163176
}
164177
l = dep.LockFromInterface(soln)
165178

166-
// Pick notondisk project constraints from solution and add to manifest
167-
for k, _ := range pd.notondisk {
168-
for _, x := range l.Projects() {
169-
if k == x.Ident().ProjectRoot {
170-
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
171-
break
179+
// Populate manifest based on operation mode.
180+
if cmd.gopath {
181+
// Pick notondisk project constraints from solution and add to manifest
182+
for k, _ := range pd.notondisk {
183+
for _, x := range l.Projects() {
184+
if k == x.Ident().ProjectRoot {
185+
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
186+
break
187+
}
172188
}
173189
}
190+
} else {
191+
// Pick all the solved projects from the above solution and add to manifest
192+
for _, x := range l.Projects() {
193+
m.Dependencies[x.Ident().ProjectRoot] = getProjectPropertiesFromVersion(x.Version())
194+
}
174195
}
175196

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

0 commit comments

Comments
 (0)