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

Commit 680a7d8

Browse files
committed
Improve solution-lock comparison and other minor fixes
1 parent 1ce6fdb commit 680a7d8

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

cmd/dep/status.go

+46-32
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type outputter interface {
8282
MissingFooter() error
8383
}
8484

85-
// Only a subset of the outputters should be able to output old statuses
85+
// Only a subset of the outputters should be able to output old statuses.
8686
type oldOutputter interface {
8787
OldHeader() error
8888
OldLine(*OldStatus) error
@@ -311,7 +311,7 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
311311

312312
if cmd.old {
313313
if _, ok := out.(oldOutputter); !ok {
314-
return errors.Errorf("invalid output command usesed")
314+
return errors.Errorf("invalid output format used")
315315
}
316316
err = cmd.runOld(ctx, out.(oldOutputter), p, sm)
317317
ctx.Out.Print(buf.String())
@@ -451,7 +451,7 @@ func (cmd *statusCommand) runOld(ctx *dep.Ctx, out oldOutputter, p *dep.Project,
451451
// Locks aren't a part of the input hash check, so we can omit it.
452452
}
453453

454-
// Check update for all the projects
454+
// Check update for all the projects.
455455
params.ChangeAll = true
456456

457457
solver, err := gps.Prepare(params, sm)
@@ -465,40 +465,54 @@ func (cmd *statusCommand) runOld(ctx *dep.Ctx, out oldOutputter, p *dep.Project,
465465
}
466466

467467
var oldStatuses []OldStatus
468+
lockProjects := p.Lock.Projects()
468469
solutionProjects := solution.Projects()
469470

470-
for _, proj := range p.Lock.Projects() {
471-
for i := range solutionProjects {
472-
// Look for the same project in solution and lock
473-
if solutionProjects[i].Ident().ProjectRoot != proj.Ident().ProjectRoot {
474-
continue
475-
}
471+
sort.Slice(solutionProjects, func(i, j int) bool {
472+
return solutionProjects[i].Ident().Less(solutionProjects[j].Ident())
473+
})
476474

477-
// If revisions are not the same then it is old and we should display it
478-
latestRev, _, _ := gps.VersionComponentStrings(solutionProjects[i].Version())
479-
atRev, _, _ := gps.VersionComponentStrings(proj.Version())
480-
if atRev == latestRev {
481-
continue
482-
}
475+
sort.Slice(lockProjects, func(i, j int) bool {
476+
return lockProjects[i].Ident().Less(lockProjects[j].Ident())
477+
})
483478

484-
// Generate the old status data and append it
485-
os := OldStatus{
486-
ProjectRoot: proj.Ident().String(),
487-
Revision: gps.Revision(atRev),
488-
Latest: gps.Revision(latestRev),
489-
}
490-
// Getting Constraint
491-
if pp, has := p.Manifest.Ovr[proj.Ident().ProjectRoot]; has && pp.Constraint != nil {
492-
// manifest has override for project
493-
os.Constraint = pp.Constraint
494-
} else if pp, has := p.Manifest.Constraints[proj.Ident().ProjectRoot]; has && pp.Constraint != nil {
495-
// manifest has normal constraint
496-
os.Constraint = pp.Constraint
497-
} else {
498-
os.Constraint = gps.Any()
499-
}
500-
oldStatuses = append(oldStatuses, os)
479+
for i := range solutionProjects {
480+
lProj := lockProjects[i]
481+
sProj := solutionProjects[i]
482+
483+
if lProj.Ident().ProjectRoot != sProj.Ident().ProjectRoot {
484+
// We should always be comparing the same projects and should enter be here.
485+
return errors.New("Projects from the solution and lock are not in the same order")
486+
}
487+
488+
// If revisions are not the same then it is old and we should display it.
489+
latestRev, _, _ := gps.VersionComponentStrings(sProj.Version())
490+
atRev, _, _ := gps.VersionComponentStrings(lProj.Version())
491+
if atRev == latestRev {
492+
continue
493+
}
494+
495+
var constraint gps.Constraint
496+
// Getting Constraint
497+
if pp, has := p.Manifest.Ovr[lProj.Ident().ProjectRoot]; has && pp.Constraint != nil {
498+
// Manifest has override for project.
499+
constraint = pp.Constraint
500+
} else if pp, has := p.Manifest.Constraints[lProj.Ident().ProjectRoot]; has && pp.Constraint != nil {
501+
// Manifest has normal constraint.
502+
constraint = pp.Constraint
503+
} else {
504+
// No constraint exists. No need to worry about displaying it
505+
continue
506+
}
507+
508+
// Generate the old status data and append it.
509+
os := OldStatus{
510+
ProjectRoot: lProj.Ident().String(),
511+
Revision: gps.Revision(atRev),
512+
Latest: gps.Revision(latestRev),
513+
Constraint: constraint,
501514
}
515+
oldStatuses = append(oldStatuses, os)
502516
}
503517

504518
out.OldHeader()

cmd/dep/testdata/harness_tests/status/old_constraints/initial/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The Go Authors. All rights reserved.
1+
// Copyright 2018 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

0 commit comments

Comments
 (0)