@@ -27,14 +27,19 @@ Initialize the project at filepath root by parsing its dependencies, writing
27
27
manifest and lock files, and vendoring the dependencies. If root isn't
28
28
specified, use the current directory.
29
29
30
- The version of each dependency will reflect the current state of the GOPATH. If
31
- a dependency doesn't exist in the GOPATH, a version will be selected from the
32
- versions available from the upstream source per the following algorithm:
30
+ By default, the dependencies are resolved over the network. A version will be
31
+ selected from the versions available from the upstream source per the following
32
+ algorithm:
33
33
34
34
- Tags conforming to semver (sorted by semver rules)
35
35
- Default branch(es) (sorted lexicographically)
36
36
- Non-semver tags (sorted lexicographically)
37
37
38
+ An alternate mode can be activated by passing -gopath. In this mode, version of
39
+ each dependency will reflect the current state of the GOPATH. If a dependency
40
+ doesn't exist in the GOPATH, a version will be selected based on the above
41
+ network version selection algorithm.
42
+
38
43
A Gopkg.toml file will be written with inferred version constraints for all
39
44
direct dependencies. Gopkg.lock will be written with precise versions, and
40
45
vendor/ will be populated with the precise versions written to Gopkg.lock.
@@ -48,10 +53,12 @@ func (cmd *initCommand) Hidden() bool { return false }
48
53
49
54
func (cmd * initCommand ) Register (fs * flag.FlagSet ) {
50
55
fs .BoolVar (& cmd .noExamples , "no-examples" , false , "don't include example in Gopkg.toml" )
56
+ fs .BoolVar (& cmd .gopath , "gopath" , false , "search in GOPATH for dependencies" )
51
57
}
52
58
53
59
type initCommand struct {
54
60
noExamples bool
61
+ gopath bool
55
62
}
56
63
57
64
func trimPathPrefix (p1 , p2 string ) string {
@@ -115,44 +122,56 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
115
122
sm .UseDefaultSignalHandling ()
116
123
defer sm .Release ()
117
124
118
- ctx .Loggers .Err .Println ("Searching GOPATH for projects..." )
119
- pd , err := getProjectData (ctx , pkgT , cpr , sm )
120
- if err != nil {
121
- return err
122
- }
125
+ // Create empty manifest, lock and projectData, and fill them according to
126
+ // init operation modes. If gopath flag is set, fetch projectData by searching
127
+ // through GOPATH, else operate in network mode, solve all the dependencies
128
+ // over network.
123
129
m := & dep.Manifest {
124
- Dependencies : pd . constraints ,
130
+ Dependencies : make (gps. ProjectConstraints ) ,
125
131
}
132
+ l := & dep.Lock {}
133
+ var pd projectData
126
134
127
- // Make an initial lock from what knowledge we've collected about the
128
- // versions on disk
129
- l := & dep.Lock {
130
- P : make ([]gps.LockedProject , 0 , len (pd .ondisk )),
131
- }
135
+ if cmd .gopath {
136
+ ctx .Loggers .Err .Println ("Searching GOPATH for projects..." )
137
+ pd , err = getProjectData (ctx , pkgT , cpr , sm )
138
+ if err != nil {
139
+ return err
140
+ }
132
141
133
- for pr , v := range pd .ondisk {
134
- // That we have to chop off these path prefixes is a symptom of
135
- // a problem in gps itself
136
- pkgs := make ([]string , 0 , len (pd .dependencies [pr ]))
137
- prslash := string (pr ) + "/"
138
- for _ , pkg := range pd .dependencies [pr ] {
139
- if pkg == string (pr ) {
140
- pkgs = append (pkgs , "." )
141
- } else {
142
- pkgs = append (pkgs , trimPathPrefix (pkg , prslash ))
142
+ m .Dependencies = pd .constraints
143
+
144
+ // Make an initial lock from what knowledge we've collected about the
145
+ // versions on disk
146
+ l = & dep.Lock {
147
+ P : make ([]gps.LockedProject , 0 , len (pd .ondisk )),
148
+ }
149
+
150
+ for pr , v := range pd .ondisk {
151
+ // That we have to chop off these path prefixes is a symptom of
152
+ // a problem in gps itself
153
+ pkgs := make ([]string , 0 , len (pd .dependencies [pr ]))
154
+ prslash := string (pr ) + "/"
155
+ for _ , pkg := range pd .dependencies [pr ] {
156
+ if pkg == string (pr ) {
157
+ pkgs = append (pkgs , "." )
158
+ } else {
159
+ pkgs = append (pkgs , trimPathPrefix (pkg , prslash ))
160
+ }
143
161
}
162
+
163
+ l .P = append (l .P , gps .NewLockedProject (
164
+ gps.ProjectIdentifier {ProjectRoot : pr }, v , pkgs ),
165
+ )
144
166
}
145
167
146
- l .P = append (l .P , gps .NewLockedProject (
147
- gps.ProjectIdentifier {ProjectRoot : pr }, v , pkgs ),
148
- )
168
+ ctx .Loggers .Err .Println ("Using network for remaining projects..." )
149
169
}
150
170
151
- ctx .Loggers .Err .Println ("Using network for remaining projects..." )
152
171
// Copy lock before solving. Use this to separate new lock projects from soln
153
172
copyLock := * l
154
173
155
- // Run solver with project versions found on disk
174
+ // Run solver with the available knowledge to solve the dependency constraints
156
175
if ctx .Loggers .Verbose {
157
176
ctx .Loggers .Err .Println ("dep: Solving..." )
158
177
}
@@ -180,25 +199,43 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
180
199
}
181
200
l = dep .LockFromInterface (soln )
182
201
183
- // Iterate through the new projects in solved lock and add them to manifest
184
- // if direct deps and log feedback for all the new projects.
185
- for _ , x := range l .Projects () {
186
- pr := x .Ident ().ProjectRoot
187
- newProject := true
188
- // Check if it's a new project, not in the old lock
189
- for _ , y := range copyLock .Projects () {
190
- if pr == y .Ident ().ProjectRoot {
191
- newProject = false
202
+ // Populate manifest based on operation mode.
203
+ if cmd .gopath {
204
+ // Iterate through the new projects in solved lock and add them to manifest
205
+ // if direct deps and log feedback for all the new projects.
206
+ for _ , x := range l .Projects () {
207
+ pr := x .Ident ().ProjectRoot
208
+ newProject := true
209
+ // Check if it's a new project, not in the old lock
210
+ for _ , y := range copyLock .Projects () {
211
+ if pr == y .Ident ().ProjectRoot {
212
+ newProject = false
213
+ }
214
+ }
215
+ if newProject {
216
+ // Check if it's in notondisk project map. These are direct deps, should
217
+ // be added to manifest.
218
+ if _ , ok := pd .notondisk [pr ]; ok {
219
+ m .Dependencies [pr ] = getProjectPropertiesFromVersion (x .Version ())
220
+ feedback (x .Version (), pr , fb .DepTypeDirect , ctx )
221
+ } else {
222
+ // Log feedback of transitive project
223
+ feedback (x .Version (), pr , fb .DepTypeTransitive , ctx )
224
+ }
192
225
}
193
226
}
194
- if newProject {
195
- // Check if it's in notondisk project map. These are direct deps, should
196
- // be added to manifest.
197
- if _ , ok := pd .notondisk [pr ]; ok {
227
+ } else {
228
+ // Get direct dependencies
229
+ rm , _ := pkgT .ToReachMap (true , true , false , nil )
230
+ directDep := rm .Flatten (false )
231
+
232
+ // Pick the direct dependencies from the above solution and add to manifest
233
+ for _ , x := range l .Projects () {
234
+ pr := x .Ident ().ProjectRoot
235
+ if contains (directDep , string (pr )) {
198
236
m .Dependencies [pr ] = getProjectPropertiesFromVersion (x .Version ())
199
237
feedback (x .Version (), pr , fb .DepTypeDirect , ctx )
200
238
} else {
201
- // Log feedback of transitive project
202
239
feedback (x .Version (), pr , fb .DepTypeTransitive , ctx )
203
240
}
204
241
}
0 commit comments