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

Commit aaf8a8b

Browse files
authored
Merge pull request #325 from carolynvs/fix-ensure-unmodified-lock
Write vendor, regardless of lock, if force is true
2 parents a6fea76 + a5ab339 commit aaf8a8b

File tree

6 files changed

+242
-98
lines changed

6 files changed

+242
-98
lines changed

cmd/dep/ensure.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
153153
if err != nil {
154154
return errors.Wrap(err, "ensure vendor is a directory")
155155
}
156-
writeV := !vendorExists && solution != nil
156+
writeV := dep.VendorOnChanged
157+
if !vendorExists && solution != nil {
158+
writeV = dep.VendorAlways
159+
}
157160

158161
var sw dep.SafeWriter
159162
var manifest *dep.Manifest

cmd/dep/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
162162
vlogf("Writing manifest and lock files.")
163163

164164
var sw dep.SafeWriter
165-
sw.Prepare(m, l, nil, true)
165+
sw.Prepare(m, nil, l, dep.VendorAlways)
166166
if err := sw.Write(root, sm); err != nil {
167167
return errors.Wrap(err, "safe write of manifest and lock")
168168
}

cmd/dep/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
181181

182182
var sw dep.SafeWriter
183183
newLock := dep.LockFromInterface(soln)
184-
sw.Prepare(p.Manifest, p.Lock, newLock, false)
184+
sw.Prepare(p.Manifest, p.Lock, newLock, dep.VendorOnChanged)
185185
if err := sw.Write(p.AbsRoot, sm); err != nil {
186186
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
187187
}

testdata/txn_writer/expected_diff_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Memo: 595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c -> 2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e
12
Add: [
23
{
34
"name": "github.com/stuff/realthing",

txn_writer.go

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dep
66

77
import (
88
"bytes"
9+
"encoding/hex"
910
"encoding/json"
1011
"fmt"
1112
"io/ioutil"
@@ -30,10 +31,10 @@ type SafeWriter struct {
3031

3132
// SafeWriterPayload represents the actions SafeWriter will execute when SafeWriter.Write is called.
3233
type SafeWriterPayload struct {
33-
Manifest *Manifest
34-
Lock *Lock
35-
LockDiff *LockDiff
36-
ForceWriteVendor bool
34+
Manifest *Manifest
35+
Lock *Lock
36+
LockDiff *LockDiff
37+
WriteVendor bool
3738
}
3839

3940
func (payload *SafeWriterPayload) HasLock() bool {
@@ -45,10 +46,7 @@ func (payload *SafeWriterPayload) HasManifest() bool {
4546
}
4647

4748
func (payload *SafeWriterPayload) HasVendor() bool {
48-
// TODO(carolynvs) this can be calculated based on if we are writing the lock
49-
// init -> switch to newlock
50-
// ensure checks existence, why not move that into the prep?
51-
return payload.ForceWriteVendor
49+
return payload.WriteVendor
5250
}
5351

5452
// LockDiff is the set of differences between an existing lock file and an updated lock file.
@@ -68,6 +66,10 @@ func (diff *LockDiff) Format() (string, error) {
6866

6967
var buf bytes.Buffer
7068

69+
if diff.HashDiff != nil {
70+
buf.WriteString(fmt.Sprintf("Memo: %s\n", diff.HashDiff))
71+
}
72+
7173
if len(diff.Add) > 0 {
7274
buf.WriteString("Add: ")
7375

@@ -124,63 +126,80 @@ type StringDiff struct {
124126
Current string
125127
}
126128

127-
func (diff StringDiff) MarshalJSON() ([]byte, error) {
128-
var value string
129-
129+
func (diff StringDiff) String() string {
130130
if diff.Previous == "" && diff.Current != "" {
131-
value = fmt.Sprintf("+ %s", diff.Current)
132-
} else if diff.Previous != "" && diff.Current == "" {
133-
value = fmt.Sprintf("- %s", diff.Previous)
134-
} else if diff.Previous != diff.Current {
135-
value = fmt.Sprintf("%s -> %s", diff.Previous, diff.Current)
136-
} else {
137-
value = diff.Current
131+
return fmt.Sprintf("+ %s", diff.Current)
132+
}
133+
134+
if diff.Previous != "" && diff.Current == "" {
135+
return fmt.Sprintf("- %s", diff.Previous)
136+
}
137+
138+
if diff.Previous != diff.Current {
139+
return fmt.Sprintf("%s -> %s", diff.Previous, diff.Current)
138140
}
139141

142+
return diff.Current
143+
}
144+
145+
func (diff StringDiff) MarshalJSON() ([]byte, error) {
140146
var buf bytes.Buffer
141147
enc := json.NewEncoder(&buf)
142148
enc.SetEscapeHTML(false)
143-
err := enc.Encode(value)
149+
err := enc.Encode(diff.String())
144150

145151
return buf.Bytes(), err
146152
}
147153

154+
// VendorBehavior defines when the vendor directory should be written.
155+
type VendorBehavior int
156+
157+
const (
158+
// VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
159+
VendorOnChanged VendorBehavior = iota
160+
// VendorAlways forces the vendor directory to always be written.
161+
VendorAlways
162+
// VendorNever indicates the vendor directory should never be written.
163+
VendorNever
164+
)
165+
148166
// Prepare to write a set of config yaml, lock and vendor tree.
149167
//
150168
// - If manifest is provided, it will be written to the standard manifest file
151169
// name beneath root.
152-
// - If lock is provided it will be written to the standard
153-
// lock file name in the root dir, but vendor will NOT be written
154-
// - If lock and newLock are both provided and are equivalent, then neither lock
155-
// nor vendor will be written
156-
// - If lock and newLock are both provided and are not equivalent,
157-
// the newLock will be written to the same location as above, and a vendor
158-
// tree will be written to the vendor directory
159-
// - If newLock is provided and lock is not, it will write both a lock
160-
// and the vendor directory in the same way
161-
// - If the forceVendor param is true, then vendor/ will be unconditionally
162-
// written out based on newLock if present, else lock, else error.
163-
func (sw *SafeWriter) Prepare(manifest *Manifest, lock *Lock, newLock *Lock, forceVendor bool) {
170+
// - If newLock is provided, it will be written to the standard lock file
171+
// name beneath root.
172+
// - If vendor is VendorAlways, or is VendorOnChanged and the locks are different,
173+
// the vendor directory will be written beneath root based on newLock.
174+
// - If oldLock is provided without newLock, error.
175+
// - If vendor is VendorAlways without a newLock, error.
176+
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior) error {
164177
sw.Payload = &SafeWriterPayload{
165-
Manifest: manifest,
166-
ForceWriteVendor: forceVendor,
178+
Manifest: manifest,
179+
Lock: newLock,
167180
}
168181

169-
if newLock != nil {
170-
if lock == nil {
171-
sw.Payload.Lock = newLock
172-
sw.Payload.ForceWriteVendor = true
173-
} else {
174-
diff := diffLocks(lock, newLock)
175-
if diff != nil {
176-
sw.Payload.Lock = newLock
177-
sw.Payload.LockDiff = diff
178-
sw.Payload.ForceWriteVendor = true
179-
}
182+
if oldLock != nil {
183+
if newLock == nil {
184+
return errors.New("must provide newLock when oldLock is specified")
185+
}
186+
sw.Payload.LockDiff = diffLocks(oldLock, newLock)
187+
}
188+
189+
switch vendor {
190+
case VendorAlways:
191+
sw.Payload.WriteVendor = true
192+
case VendorOnChanged:
193+
if sw.Payload.LockDiff != nil || (newLock != nil && oldLock == nil) {
194+
sw.Payload.WriteVendor = true
180195
}
181-
} else if lock != nil {
182-
sw.Payload.Lock = lock
183196
}
197+
198+
if sw.Payload.WriteVendor && newLock == nil {
199+
return errors.New("must provide newLock in order to write out vendor")
200+
}
201+
202+
return nil
184203
}
185204

186205
func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) error {
@@ -198,10 +217,6 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err
198217
return errors.New("must provide a SourceManager if writing out a vendor dir")
199218
}
200219

201-
if payload.HasVendor() && payload.Lock == nil {
202-
return errors.New("must provide a lock in order to write out vendor")
203-
}
204-
205220
return nil
206221
}
207222

@@ -358,12 +373,21 @@ func (sw *SafeWriter) PrintPreparedActions() error {
358373
}
359374

360375
if sw.Payload.HasLock() {
361-
fmt.Println("Would have written the following changes to lock.json:")
362-
diff, err := sw.Payload.LockDiff.Format()
363-
if err != nil {
364-
return errors.Wrap(err, "ensure DryRun cannot serialize the lock diff")
376+
if sw.Payload.LockDiff == nil {
377+
fmt.Println("Would have written the following lock.json:")
378+
l, err := sw.Payload.Lock.MarshalJSON()
379+
if err != nil {
380+
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
381+
}
382+
fmt.Println(string(l))
383+
} else {
384+
fmt.Println("Would have written the following changes to lock.json:")
385+
diff, err := sw.Payload.LockDiff.Format()
386+
if err != nil {
387+
return errors.Wrap(err, "ensure DryRun cannot serialize the lock diff")
388+
}
389+
fmt.Println(diff)
365390
}
366-
fmt.Println(diff)
367391
}
368392

369393
if sw.Payload.HasVendor() {
@@ -413,10 +437,10 @@ func diffLocks(l1 gps.Lock, l2 gps.Lock) *LockDiff {
413437

414438
diff := LockDiff{}
415439

416-
h1 := l1.InputHash()
417-
h2 := l2.InputHash()
418-
if !bytes.Equal(h1, h2) {
419-
diff.HashDiff = &StringDiff{Previous: string(h1), Current: string(h2)}
440+
h1 := hex.EncodeToString(l1.InputHash())
441+
h2 := hex.EncodeToString(l2.InputHash())
442+
if h1 != h2 {
443+
diff.HashDiff = &StringDiff{Previous: h1, Current: h2}
420444
}
421445

422446
var i2next int

0 commit comments

Comments
 (0)