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

Commit a31e404

Browse files
committed
Tidy up the logic for -update vs. a bare ensure
1 parent 9e1b219 commit a31e404

File tree

1 file changed

+80
-61
lines changed

1 file changed

+80
-61
lines changed

cmd/dep/ensure.go

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,67 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
119119
defer sm.Release()
120120

121121
params := p.MakeParams()
122-
if cmd.update && len(args) == 0 {
123-
// If -update was specified without args, we want the solver to allow all versions to change
124-
params.ChangeAll = cmd.update
122+
if *verbose {
123+
params.Trace = true
124+
params.TraceLogger = log.New(os.Stderr, "", 0)
125+
}
126+
params.RootPackageTree, err = gps.ListPackages(p.AbsRoot, string(p.ImportRoot))
127+
if err != nil {
128+
return errors.Wrap(err, "ensure ListPackage for project")
129+
}
130+
131+
if cmd.update {
132+
applyUpdateArgs(args, &params)
133+
} else {
134+
err := applyEnsureArgs(args, cmd.overrides, p, sm, &params)
135+
if err != nil {
136+
return err
137+
}
138+
}
139+
140+
solver, err := gps.Prepare(params, sm)
141+
if err != nil {
142+
return errors.Wrap(err, "ensure Prepare")
143+
}
144+
solution, err := solver.Solve()
145+
if err != nil {
146+
handleAllTheFailuresOfTheWorld(err)
147+
return errors.Wrap(err, "ensure Solve()")
148+
}
149+
150+
sw := dep.SafeWriter{
151+
Root: p.AbsRoot,
152+
Manifest: p.Manifest,
153+
Lock: p.Lock,
154+
NewLock: solution,
155+
SourceManager: sm,
156+
}
157+
158+
// check if vendor exists, because if the locks are the same but
159+
// vendor does not exist we should write vendor
160+
var writeV bool
161+
vendorExists, _ := dep.IsDir(filepath.Join(sw.Root, "vendor"))
162+
if !vendorExists && solution != nil {
163+
writeV = true
164+
}
165+
166+
return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
167+
}
168+
169+
func applyUpdateArgs(args []string, params *gps.SolveParameters) {
170+
// When -update is specified without args, allow every project to change versions, regardless of the lock file
171+
if len(args) == 0 {
172+
params.ChangeAll = true
173+
return
125174
}
126175

176+
// Allow any of specified project versions to change, regardless of the lock file
177+
for _, arg := range args {
178+
params.ToChange = append(params.ToChange, gps.ProjectRoot(arg))
179+
}
180+
}
181+
182+
func applyEnsureArgs(args []string, overrides stringSlice, p *dep.Project, sm *gps.SourceMgr, params *gps.SolveParameters) error {
127183
var errs []error
128184
for _, arg := range args {
129185
pc, err := getProjectConstraint(arg, sm)
@@ -132,33 +188,31 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
132188
continue
133189
}
134190

135-
// Ignore the lockfile for this dependency and allow its version to change
136-
params.ToChange = append(params.ToChange, pc.Ident.ProjectRoot)
137-
138-
if !cmd.update {
139-
if gps.IsAny(pc.Constraint) && pc.Ident.Source == "" {
140-
// If the input specified neither a network name nor a constraint,
141-
// then the strict thing to do would be to remove the entry
142-
// entirely. But that would probably be quite surprising for users,
143-
// and it's what rm is for, so just ignore the input.
144-
//
145-
// TODO(sdboyer): for this case - or just in general - do we want to
146-
// add project args to the requires list temporarily for this run?
147-
if _, has := p.Manifest.Dependencies[pc.Ident.ProjectRoot]; !has {
148-
logf("No constraint or alternate source specified for %q, omitting from manifest", pc.Ident.ProjectRoot)
149-
}
150-
// If it's already in the manifest, no need to log
151-
continue
191+
if gps.IsAny(pc.Constraint) && pc.Ident.Source == "" {
192+
// If the input specified neither a network name nor a constraint,
193+
// then the strict thing to do would be to remove the entry
194+
// entirely. But that would probably be quite surprising for users,
195+
// and it's what rm is for, so just ignore the input.
196+
//
197+
// TODO(sdboyer): for this case - or just in general - do we want to
198+
// add project args to the requires list temporarily for this run?
199+
if _, has := p.Manifest.Dependencies[pc.Ident.ProjectRoot]; !has {
200+
logf("No constraint or alternate source specified for %q, omitting from manifest", pc.Ident.ProjectRoot)
152201
}
202+
// If it's already in the manifest, no need to log
203+
continue
204+
}
153205

154-
p.Manifest.Dependencies[pc.Ident.ProjectRoot] = gps.ProjectProperties{
155-
Source: pc.Ident.Source,
156-
Constraint: pc.Constraint,
157-
}
206+
p.Manifest.Dependencies[pc.Ident.ProjectRoot] = gps.ProjectProperties{
207+
Source: pc.Ident.Source,
208+
Constraint: pc.Constraint,
158209
}
210+
211+
// Ignore the lockfile for this dependency and allow its version to change
212+
params.ToChange = append(params.ToChange, pc.Ident.ProjectRoot)
159213
}
160214

161-
for _, ovr := range cmd.overrides {
215+
for _, ovr := range overrides {
162216
pc, err := getProjectConstraint(ovr, sm)
163217
if err != nil {
164218
errs = append(errs, err)
@@ -187,42 +241,7 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
187241
return errors.New(buf.String())
188242
}
189243

190-
if *verbose {
191-
params.Trace = true
192-
params.TraceLogger = log.New(os.Stderr, "", 0)
193-
}
194-
195-
params.RootPackageTree, err = gps.ListPackages(p.AbsRoot, string(p.ImportRoot))
196-
if err != nil {
197-
return errors.Wrap(err, "ensure ListPackage for project")
198-
}
199-
solver, err := gps.Prepare(params, sm)
200-
if err != nil {
201-
return errors.Wrap(err, "ensure Prepare")
202-
}
203-
solution, err := solver.Solve()
204-
if err != nil {
205-
handleAllTheFailuresOfTheWorld(err)
206-
return errors.Wrap(err, "ensure Solve()")
207-
}
208-
209-
sw := dep.SafeWriter{
210-
Root: p.AbsRoot,
211-
Manifest: p.Manifest,
212-
Lock: p.Lock,
213-
NewLock: solution,
214-
SourceManager: sm,
215-
}
216-
217-
// check if vendor exists, because if the locks are the same but
218-
// vendor does not exist we should write vendor
219-
var writeV bool
220-
vendorExists, _ := dep.IsDir(filepath.Join(sw.Root, "vendor"))
221-
if !vendorExists && solution != nil {
222-
writeV = true
223-
}
224-
225-
return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
244+
return nil
226245
}
227246

228247
type stringSlice []string

0 commit comments

Comments
 (0)