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

Commit c7e220d

Browse files
committed
Remove InputsDigest, add InputImports
First steps towards a better in-sync checking system that does not rely on a merge-conflict-prone explicit hash digest.
1 parent a437df4 commit c7e220d

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

gps/selection.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (s *selection) getRequiredPackagesIn(id ProjectIdentifier) map[string]int {
119119
return uniq
120120
}
121121

122-
// Suppress unused warning.
122+
// Suppress unused linting warning.
123123
var _ = (*selection)(nil).getSelectedPackagesIn
124124

125125
// Compute a list of the unique packages within the given ProjectIdentifier that
@@ -141,6 +141,29 @@ func (s *selection) getSelectedPackagesIn(id ProjectIdentifier) map[string]int {
141141
return uniq
142142
}
143143

144+
// getProjectImportMap extracts the set of package imports from the used
145+
// packages in each selected project.
146+
func (s *selection) getProjectImportMap() map[ProjectRoot]map[string]struct{} {
147+
importMap := make(map[ProjectRoot]map[string]struct{})
148+
for _, edges := range s.deps {
149+
for _, edge := range edges {
150+
var curmap map[string]struct{}
151+
if imap, has := importMap[edge.depender.id.ProjectRoot]; !has {
152+
curmap = make(map[string]struct{})
153+
} else {
154+
curmap = imap
155+
}
156+
157+
for _, pl := range edge.dep.pl {
158+
curmap[pl] = struct{}{}
159+
}
160+
importMap[edge.depender.id.ProjectRoot] = curmap
161+
}
162+
}
163+
164+
return importMap
165+
}
166+
144167
func (s *selection) getConstraint(id ProjectIdentifier) Constraint {
145168
deps, exists := s.deps[id.ProjectRoot]
146169
if !exists || len(deps) == 0 {

gps/solution_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ func init() {
3939
},
4040
}
4141
basicResult.analyzerInfo = (naiveAnalyzer{}).Info()
42-
43-
// Just in case something needs punishing, kubernetes offers a complex,
44-
// real-world set of dependencies, and this revision is known to work.
45-
/*
46-
_ = atom{
47-
id: pi("github.com/kubernetes/kubernetes"),
48-
v: NewVersion("1.0.0").Pair(Revision("528f879e7d3790ea4287687ef0ab3f2a01cc2718")),
49-
}
50-
*/
5142
}
5243

5344
func testWriteDepTree(t *testing.T) {

gps/solver.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1363,18 +1363,16 @@ func pa2lp(pa atom, pkgs map[string]struct{}) LockedProject {
13631363
panic("unreachable")
13641364
}
13651365

1366-
lp.pkgs = make([]string, len(pkgs))
1367-
k := 0
1366+
lp.pkgs = make([]string, 0, len(pkgs))
13681367

13691368
pr := string(pa.id.ProjectRoot)
13701369
trim := pr + "/"
13711370
for pkg := range pkgs {
13721371
if pkg == string(pa.id.ProjectRoot) {
1373-
lp.pkgs[k] = "."
1372+
lp.pkgs = append(lp.pkgs, ".")
13741373
} else {
1375-
lp.pkgs[k] = strings.TrimPrefix(pkg, trim)
1374+
lp.pkgs = append(lp.pkgs, strings.TrimPrefix(pkg, trim))
13761375
}
1377-
k++
13781376
}
13791377
sort.Strings(lp.pkgs)
13801378

lock.go

+12-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package dep
66

77
import (
88
"bytes"
9-
"encoding/hex"
109
"io"
1110
"sort"
1211

@@ -26,7 +25,6 @@ type Lock struct {
2625

2726
// SolveMeta holds solver meta data.
2827
type SolveMeta struct {
29-
InputsDigest []byte
3028
AnalyzerName string
3129
AnalyzerVersion int
3230
SolverName string
@@ -39,11 +37,11 @@ type rawLock struct {
3937
}
4038

4139
type solveMeta struct {
42-
InputsDigest string `toml:"inputs-digest"`
43-
AnalyzerName string `toml:"analyzer-name"`
44-
AnalyzerVersion int `toml:"analyzer-version"`
45-
SolverName string `toml:"solver-name"`
46-
SolverVersion int `toml:"solver-version"`
40+
AnalyzerName string `toml:"analyzer-name"`
41+
AnalyzerVersion int `toml:"analyzer-version"`
42+
SolverName string `toml:"solver-name"`
43+
SolverVersion int `toml:"solver-version"`
44+
InputImports []string `toml:"input-imports"`
4745
}
4846

4947
type rawLockedProject struct {
@@ -77,15 +75,11 @@ func fromRawLock(raw rawLock) (*Lock, error) {
7775
P: make([]gps.LockedProject, len(raw.Projects)),
7876
}
7977

80-
l.SolveMeta.InputsDigest, err = hex.DecodeString(raw.SolveMeta.InputsDigest)
81-
if err != nil {
82-
return nil, errors.Errorf("invalid hash digest in lock's memo field")
83-
}
84-
8578
l.SolveMeta.AnalyzerName = raw.SolveMeta.AnalyzerName
8679
l.SolveMeta.AnalyzerVersion = raw.SolveMeta.AnalyzerVersion
8780
l.SolveMeta.SolverName = raw.SolveMeta.SolverName
8881
l.SolveMeta.SolverVersion = raw.SolveMeta.SolverVersion
82+
l.SolveMeta.InputImports = raw.SolveMeta.InputImports
8983

9084
for i, ld := range raw.Projects {
9185
r := gps.Revision(ld.Revision)
@@ -106,15 +100,17 @@ func fromRawLock(raw rawLock) (*Lock, error) {
106100
ProjectRoot: gps.ProjectRoot(ld.Name),
107101
Source: ld.Source,
108102
}
109-
l.P[i] = gps.NewLockedProject(id, v, ld.Packages)
103+
l.P[i] = gps.NewLockedProject(id, v, ld.Packages, ld.Imports)
110104
}
111105

112106
return l, nil
113107
}
114108

115109
// InputsDigest returns the hash of inputs which produced this lock data.
110+
//
111+
// TODO(sdboyer) remove, this is now deprecated
116112
func (l *Lock) InputsDigest() []byte {
117-
return l.SolveMeta.InputsDigest
113+
return nil
118114
}
119115

120116
// Projects returns the list of LockedProjects contained in the lock data.
@@ -140,7 +136,6 @@ func (l *Lock) HasProjectWithRoot(root gps.ProjectRoot) bool {
140136
func (l *Lock) toRaw() rawLock {
141137
raw := rawLock{
142138
SolveMeta: solveMeta{
143-
InputsDigest: hex.EncodeToString(l.SolveMeta.InputsDigest),
144139
AnalyzerName: l.SolveMeta.AnalyzerName,
145140
AnalyzerVersion: l.SolveMeta.AnalyzerVersion,
146141
SolverName: l.SolveMeta.SolverName,
@@ -182,13 +177,12 @@ func (l *Lock) MarshalTOML() ([]byte, error) {
182177
// LockFromSolution converts a gps.Solution to dep's representation of a lock.
183178
//
184179
// Data is defensively copied wherever necessary to ensure the resulting *lock
185-
// shares no memory with the original lock.
180+
// shares no memory with the input solution.
186181
func LockFromSolution(in gps.Solution) *Lock {
187-
h, p := in.InputsDigest(), in.Projects()
182+
p := in.Projects()
188183

189184
l := &Lock{
190185
SolveMeta: SolveMeta{
191-
InputsDigest: make([]byte, len(h)),
192186
AnalyzerName: in.AnalyzerName(),
193187
AnalyzerVersion: in.AnalyzerVersion(),
194188
SolverName: in.SolverName(),
@@ -197,7 +191,6 @@ func LockFromSolution(in gps.Solution) *Lock {
197191
P: make([]gps.LockedProject, len(p)),
198192
}
199193

200-
copy(l.SolveMeta.InputsDigest, h)
201194
copy(l.P, p)
202195
return l
203196
}

lock_test.go

-12
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ func TestReadLock(t *testing.T) {
2828

2929
b, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
3030
want := &Lock{
31-
SolveMeta: SolveMeta{
32-
InputsDigest: b,
33-
},
3431
P: []gps.LockedProject{
3532
gps.NewLockedProject(
3633
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/golang/dep")},
@@ -54,9 +51,6 @@ func TestReadLock(t *testing.T) {
5451

5552
b, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
5653
want = &Lock{
57-
SolveMeta: SolveMeta{
58-
InputsDigest: b,
59-
},
6054
P: []gps.LockedProject{
6155
gps.NewLockedProject(
6256
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/golang/dep")},
@@ -79,9 +73,6 @@ func TestWriteLock(t *testing.T) {
7973
want := h.GetTestFileString(golden)
8074
memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
8175
l := &Lock{
82-
SolveMeta: SolveMeta{
83-
InputsDigest: memo,
84-
},
8576
P: []gps.LockedProject{
8677
gps.NewLockedProject(
8778
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/golang/dep")},
@@ -110,9 +101,6 @@ func TestWriteLock(t *testing.T) {
110101
want = h.GetTestFileString(golden)
111102
memo, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
112103
l = &Lock{
113-
SolveMeta: SolveMeta{
114-
InputsDigest: memo,
115-
},
116104
P: []gps.LockedProject{
117105
gps.NewLockedProject(
118106
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/golang/dep")},

txn_writer.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ func formatLockDiff(diff gps.LockDiff) (string, error) {
233233
type VendorBehavior int
234234

235235
const (
236-
// VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
236+
// VendorOnChanged indicates that the vendor directory should be written
237+
// when the lock is new or changed, or a project in vendor differs from its
238+
// intended state.
237239
VendorOnChanged VendorBehavior = iota
238240
// VendorAlways forces the vendor directory to always be written.
239241
VendorAlways
@@ -259,14 +261,14 @@ func (sw SafeWriter) validate(root string, sm gps.SourceManager) error {
259261
return nil
260262
}
261263

262-
// Write saves some combination of config yaml, lock, and a vendor tree.
263-
// root is the absolute path of root dir in which to write.
264-
// sm is only required if vendor is being written.
264+
// Write saves some combination of manifest, lock, and a vendor tree. root is
265+
// the absolute path of root dir in which to write. sm is only required if
266+
// vendor is being written.
265267
//
266-
// It first writes to a temp dir, then moves them in place if and only if all the write
267-
// operations succeeded. It also does its best to roll back if any moves fail.
268-
// This mostly guarantees that dep cannot exit with a partial write that would
269-
// leave an undefined state on disk.
268+
// It first writes to a temp dir, then moves them in place if and only if all
269+
// the write operations succeeded. It also does its best to roll back if any
270+
// moves fail. This mostly guarantees that dep cannot exit with a partial write
271+
// that would leave an undefined state on disk.
270272
//
271273
// If logger is not nil, progress will be logged after each project write.
272274
func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool, logger *log.Logger) error {

0 commit comments

Comments
 (0)