2016-12-23 17:49:24 +03:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
package dep
|
2016-12-23 17:49:24 +03:00
|
|
|
|
|
|
|
import (
|
2017-03-01 23:52:09 +03:00
|
|
|
"bytes"
|
2017-03-18 00:48:57 +03:00
|
|
|
"encoding/hex"
|
2016-12-23 17:49:24 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-01 23:52:09 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-03-23 01:11:43 +03:00
|
|
|
"github.com/pelletier/go-toml"
|
2016-12-23 18:34:20 +03:00
|
|
|
"github.com/pkg/errors"
|
2016-12-23 17:49:24 +03:00
|
|
|
"github.com/sdboyer/gps"
|
|
|
|
)
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
// SafeWriter transactionalizes writes of manifest, lock, and vendor dir, both
|
2016-12-23 17:49:24 +03:00
|
|
|
// individually and in any combination, into a pseudo-atomic action with
|
|
|
|
// transactional rollback.
|
|
|
|
//
|
|
|
|
// It is not impervious to errors (writing to disk is hard), but it should
|
|
|
|
// guard against non-arcane failure conditions.
|
2017-01-27 23:39:07 +03:00
|
|
|
type SafeWriter struct {
|
2017-03-09 22:28:42 +03:00
|
|
|
Payload *SafeWriterPayload
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
// SafeWriterPayload represents the actions SafeWriter will execute when SafeWriter.Write is called.
|
|
|
|
type SafeWriterPayload struct {
|
2017-03-13 17:34:32 +03:00
|
|
|
Manifest *Manifest
|
|
|
|
Lock *Lock
|
|
|
|
LockDiff *LockDiff
|
|
|
|
WriteVendor bool
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *SafeWriterPayload) HasLock() bool {
|
|
|
|
return payload.Lock != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *SafeWriterPayload) HasManifest() bool {
|
|
|
|
return payload.Manifest != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *SafeWriterPayload) HasVendor() bool {
|
2017-03-13 17:34:32 +03:00
|
|
|
return payload.WriteVendor
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockDiff is the set of differences between an existing lock file and an updated lock file.
|
2017-03-01 23:52:09 +03:00
|
|
|
// Fields are only populated when there is a difference, otherwise they are empty.
|
2017-03-09 22:28:42 +03:00
|
|
|
// TODO(carolynvs) this should be moved to gps
|
|
|
|
type LockDiff struct {
|
2017-03-01 23:52:09 +03:00
|
|
|
HashDiff *StringDiff
|
|
|
|
Add []LockedProjectDiff
|
2017-03-06 20:39:39 +03:00
|
|
|
Remove []LockedProjectDiff
|
2017-03-01 23:52:09 +03:00
|
|
|
Modify []LockedProjectDiff
|
|
|
|
}
|
|
|
|
|
2017-04-05 04:47:09 +03:00
|
|
|
type rawLockedProjectDiffs struct {
|
|
|
|
Projects []LockedProjectDiff `toml:"projects"`
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:52:09 +03:00
|
|
|
func (diff *LockDiff) Format() (string, error) {
|
|
|
|
if diff == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
if diff.HashDiff != nil {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString(fmt.Sprintf("Memo: %s\n\n", diff.HashDiff))
|
2017-03-18 00:48:57 +03:00
|
|
|
}
|
|
|
|
|
2017-03-23 01:11:43 +03:00
|
|
|
writeDiffs := func(diffs []LockedProjectDiff) error {
|
2017-04-05 04:47:09 +03:00
|
|
|
raw := rawLockedProjectDiffs{diffs}
|
|
|
|
chunk, err := toml.Marshal(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-23 01:11:43 +03:00
|
|
|
}
|
2017-04-05 04:47:09 +03:00
|
|
|
buf.Write(chunk)
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("\n")
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-01 23:52:09 +03:00
|
|
|
|
2017-03-23 01:11:43 +03:00
|
|
|
if len(diff.Add) > 0 {
|
|
|
|
buf.WriteString("Add:")
|
|
|
|
err := writeDiffs(diff.Add)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Add")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.Remove) > 0 {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("Remove:")
|
|
|
|
err := writeDiffs(diff.Remove)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Remove")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.Modify) > 0 {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("Modify:")
|
|
|
|
err := writeDiffs(diff.Modify)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Modify")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockedProjectDiff contains the before and after snapshot of a project reference.
|
2017-03-01 23:52:09 +03:00
|
|
|
// Fields are only populated when there is a difference, otherwise they are empty.
|
2017-03-09 22:28:42 +03:00
|
|
|
// TODO(carolynvs) this should be moved to gps
|
|
|
|
type LockedProjectDiff struct {
|
2017-04-02 23:39:48 +03:00
|
|
|
Name gps.ProjectRoot `toml:"name"`
|
2017-04-05 04:47:09 +03:00
|
|
|
Source *StringDiff `toml:"source,omitempty"`
|
|
|
|
Version *StringDiff `toml:"version,omitempty"`
|
|
|
|
Branch *StringDiff `toml:"branch,omitempty"`
|
|
|
|
Revision *StringDiff `toml:"revision,omitempty"`
|
|
|
|
Packages []StringDiff `toml:"packages,omitempty"`
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type StringDiff struct {
|
|
|
|
Previous string
|
|
|
|
Current string
|
|
|
|
}
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
func (diff StringDiff) String() string {
|
2017-03-01 23:52:09 +03:00
|
|
|
if diff.Previous == "" && diff.Current != "" {
|
2017-03-18 00:48:57 +03:00
|
|
|
return fmt.Sprintf("+ %s", diff.Current)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff.Previous != "" && diff.Current == "" {
|
|
|
|
return fmt.Sprintf("- %s", diff.Previous)
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
if diff.Previous != diff.Current {
|
|
|
|
return fmt.Sprintf("%s -> %s", diff.Previous, diff.Current)
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
return diff.Current
|
|
|
|
}
|
|
|
|
|
2017-04-05 04:47:09 +03:00
|
|
|
func (diff StringDiff) MarshalTOML() ([]byte, error) {
|
|
|
|
return []byte(diff.String()), nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-03-18 04:53:00 +03:00
|
|
|
// VendorBehavior defines when the vendor directory should be written.
|
|
|
|
type VendorBehavior int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
|
|
|
|
VendorOnChanged VendorBehavior = iota
|
|
|
|
// VendorAlways forces the vendor directory to always be written.
|
|
|
|
VendorAlways
|
|
|
|
// VendorNever indicates the vendor directory should never be written.
|
|
|
|
VendorNever
|
|
|
|
)
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
// Prepare to write a set of config yaml, lock and vendor tree.
|
2016-12-23 17:49:24 +03:00
|
|
|
//
|
2017-03-09 22:28:42 +03:00
|
|
|
// - If manifest is provided, it will be written to the standard manifest file
|
|
|
|
// name beneath root.
|
2017-03-18 04:53:00 +03:00
|
|
|
// - If newLock is provided, it will be written to the standard lock file
|
|
|
|
// name beneath root.
|
|
|
|
// - If vendor is VendorAlways, or is VendorOnChanged and the locks are different,
|
|
|
|
// the vendor directory will be written beneath root based on newLock.
|
|
|
|
// - If oldLock is provided without newLock, error.
|
|
|
|
// - If vendor is VendorAlways without a newLock, error.
|
|
|
|
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior) error {
|
2017-03-09 22:28:42 +03:00
|
|
|
sw.Payload = &SafeWriterPayload{
|
2017-03-15 19:00:05 +03:00
|
|
|
Manifest: manifest,
|
2017-03-18 04:53:00 +03:00
|
|
|
Lock: newLock,
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-03-18 04:53:00 +03:00
|
|
|
if oldLock != nil {
|
|
|
|
if newLock == nil {
|
|
|
|
return errors.New("must provide newLock when oldLock is specified")
|
|
|
|
}
|
2017-03-15 19:00:05 +03:00
|
|
|
sw.Payload.LockDiff = diffLocks(oldLock, newLock)
|
|
|
|
}
|
|
|
|
|
2017-03-18 04:53:00 +03:00
|
|
|
switch vendor {
|
|
|
|
case VendorAlways:
|
2017-03-15 19:00:05 +03:00
|
|
|
sw.Payload.WriteVendor = true
|
2017-03-18 04:53:00 +03:00
|
|
|
case VendorOnChanged:
|
|
|
|
if sw.Payload.LockDiff != nil || (newLock != nil && oldLock == nil) {
|
|
|
|
sw.Payload.WriteVendor = true
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-15 19:00:05 +03:00
|
|
|
|
2017-03-18 04:53:00 +03:00
|
|
|
if sw.Payload.WriteVendor && newLock == nil {
|
|
|
|
return errors.New("must provide newLock in order to write out vendor")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2017-03-18 04:53:00 +03:00
|
|
|
|
|
|
|
return nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) error {
|
|
|
|
if root == "" {
|
2016-12-30 06:15:23 +03:00
|
|
|
return errors.New("root path must be non-empty")
|
|
|
|
}
|
2017-03-09 22:28:42 +03:00
|
|
|
if is, err := IsDir(root); !is {
|
2016-12-30 06:15:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-22 18:58:50 +03:00
|
|
|
return errors.Errorf("root path %q does not exist", root)
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if payload.HasVendor() && sm == nil {
|
2017-01-25 00:48:14 +03:00
|
|
|
return errors.New("must provide a SourceManager if writing out a vendor dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write saves some combination of config yaml, lock, and a vendor tree.
|
|
|
|
// root is the absolute path of root dir in which to write.
|
|
|
|
// sm is only required if vendor is being written.
|
|
|
|
//
|
|
|
|
// It first writes to a temp dir, then moves them in place if and only if all the write
|
|
|
|
// operations succeeded. It also does its best to roll back if any moves fail.
|
|
|
|
// This mostly guarantees that dep cannot exit with a partial write that would
|
|
|
|
// leave an undefined state on disk.
|
|
|
|
func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error {
|
|
|
|
if sw.Payload == nil {
|
|
|
|
return errors.New("Cannot call SafeWriter.Write before SafeWriter.Prepare")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sw.Payload.validate(root, sm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sw.Payload.HasManifest() && !sw.Payload.HasLock() && !sw.Payload.HasVendor() {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mpath := filepath.Join(root, ManifestName)
|
|
|
|
lpath := filepath.Join(root, LockName)
|
|
|
|
vpath := filepath.Join(root, "vendor")
|
2016-12-30 06:56:11 +03:00
|
|
|
|
2017-01-19 03:11:38 +03:00
|
|
|
td, err := ioutil.TempDir(os.TempDir(), "dep")
|
2016-12-23 17:49:24 +03:00
|
|
|
if err != nil {
|
2016-12-23 18:34:20 +03:00
|
|
|
return errors.Wrap(err, "error while creating temp dir for writing manifest/lock/vendor")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasManifest() {
|
|
|
|
if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil {
|
2016-12-23 18:34:20 +03:00
|
|
|
return errors.Wrap(err, "failed to write manifest file to temp dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasLock() {
|
|
|
|
if err := writeFile(filepath.Join(td, LockName), sw.Payload.Lock); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write lock file to temp dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasVendor() {
|
|
|
|
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.Payload.Lock, sm, true)
|
2016-12-23 17:49:24 +03:00
|
|
|
if err != nil {
|
2016-12-30 06:15:23 +03:00
|
|
|
return errors.Wrap(err, "error while writing out vendor tree")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the existing files and dirs to the temp dir while we put the new
|
2017-01-25 10:29:04 +03:00
|
|
|
// ones in, to provide insurance against errors for as long as possible.
|
2016-12-23 17:49:24 +03:00
|
|
|
type pathpair struct {
|
|
|
|
from, to string
|
|
|
|
}
|
|
|
|
var restore []pathpair
|
2016-12-30 06:56:11 +03:00
|
|
|
var failerr error
|
|
|
|
var vendorbak string
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasManifest() {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(mpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old one.
|
2017-01-27 23:39:07 +03:00
|
|
|
tmploc := filepath.Join(td, ManifestName+".orig")
|
2017-01-01 04:11:05 +03:00
|
|
|
failerr = renameWithFallback(mpath, tmploc)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: tmploc, to: mpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-01-27 23:39:07 +03:00
|
|
|
failerr = renameWithFallback(filepath.Join(td, ManifestName), mpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasLock() {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(lpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old one.
|
2017-01-27 23:39:07 +03:00
|
|
|
tmploc := filepath.Join(td, LockName+".orig")
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-01-01 04:11:05 +03:00
|
|
|
failerr = renameWithFallback(lpath, tmploc)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: tmploc, to: lpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-01-27 23:39:07 +03:00
|
|
|
failerr = renameWithFallback(filepath.Join(td, LockName), lpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasVendor() {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(vpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old vendor dir. just do it into an adjacent dir, to
|
2016-12-23 18:34:20 +03:00
|
|
|
// try to mitigate the possibility of a pointless cross-filesystem
|
2017-01-25 10:29:04 +03:00
|
|
|
// move with a temp directory.
|
2016-12-30 06:15:23 +03:00
|
|
|
vendorbak = vpath + ".orig"
|
2016-12-23 17:49:24 +03:00
|
|
|
if _, err := os.Stat(vendorbak); err == nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
// If the adjacent dir already exists, bite the bullet and move
|
2017-01-25 10:29:04 +03:00
|
|
|
// to a proper tempdir.
|
2016-12-23 18:34:20 +03:00
|
|
|
vendorbak = filepath.Join(td, "vendor.orig")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-23 18:34:20 +03:00
|
|
|
|
2017-01-01 04:11:05 +03:00
|
|
|
failerr = renameWithFallback(vpath, vendorbak)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: vendorbak, to: vpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-01-01 04:11:05 +03:00
|
|
|
failerr = renameWithFallback(filepath.Join(td, "vendor"), vpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renames all went smoothly. The deferred os.RemoveAll will get the temp
|
|
|
|
// dir, but if we wrote vendor, we have to clean that up directly
|
2017-03-09 22:28:42 +03:00
|
|
|
if sw.Payload.HasVendor() {
|
2016-12-30 06:56:11 +03:00
|
|
|
// Nothing we can really do about an error at this point, so ignore it
|
2016-12-23 17:49:24 +03:00
|
|
|
os.RemoveAll(vendorbak)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-12-30 06:56:11 +03:00
|
|
|
|
|
|
|
fail:
|
2017-01-25 10:29:04 +03:00
|
|
|
// If we failed at any point, move all the things back into place, then bail.
|
2016-12-30 06:56:11 +03:00
|
|
|
for _, pair := range restore {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Nothing we can do on err here, as we're already in recovery mode.
|
2017-01-01 04:11:05 +03:00
|
|
|
renameWithFallback(pair.from, pair.to)
|
2016-12-30 06:56:11 +03:00
|
|
|
}
|
|
|
|
return failerr
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2017-03-09 22:28:42 +03:00
|
|
|
|
|
|
|
func (sw *SafeWriter) PrintPreparedActions() error {
|
|
|
|
if sw.Payload.HasManifest() {
|
2017-04-02 22:36:22 +03:00
|
|
|
fmt.Printf("Would have written the following %s:\n", ManifestName)
|
2017-03-20 21:14:02 +03:00
|
|
|
m, err := sw.Payload.Manifest.MarshalTOML()
|
2017-03-09 22:28:42 +03:00
|
|
|
if err != nil {
|
2017-03-01 23:52:09 +03:00
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
fmt.Println(string(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sw.Payload.HasLock() {
|
2017-03-15 18:06:07 +03:00
|
|
|
if sw.Payload.LockDiff == nil {
|
2017-04-02 22:36:22 +03:00
|
|
|
fmt.Printf("Would have written the following %s:\n", LockName)
|
2017-03-20 21:14:02 +03:00
|
|
|
l, err := sw.Payload.Lock.MarshalTOML()
|
2017-03-15 18:06:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
|
|
|
|
}
|
|
|
|
fmt.Println(string(l))
|
|
|
|
} else {
|
2017-04-02 22:36:22 +03:00
|
|
|
fmt.Printf("Would have written the following changes to %s:\n", LockName)
|
2017-03-15 18:06:07 +03:00
|
|
|
diff, err := sw.Payload.LockDiff.Format()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize the lock diff")
|
|
|
|
}
|
|
|
|
fmt.Println(diff)
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sw.Payload.HasVendor() {
|
|
|
|
fmt.Println("Would have written the following projects to the vendor directory:")
|
|
|
|
for _, project := range sw.Payload.Lock.Projects() {
|
|
|
|
prj := project.Ident()
|
2017-03-01 23:52:09 +03:00
|
|
|
rev, _, _ := getVersionInfo(project.Version())
|
2017-03-09 22:28:42 +03:00
|
|
|
if prj.Source == "" {
|
|
|
|
fmt.Printf("%s@%s\n", prj.ProjectRoot, rev)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s -> %s@%s\n", prj.ProjectRoot, prj.Source, rev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-01 23:52:09 +03:00
|
|
|
|
|
|
|
// diffLocks compares two locks and identifies the differences between them.
|
|
|
|
// Returns nil if there are no differences.
|
|
|
|
// TODO(carolynvs) this should be moved to gps
|
|
|
|
func diffLocks(l1 gps.Lock, l2 gps.Lock) *LockDiff {
|
|
|
|
// Default nil locks to empty locks, so that we can still generate a diff
|
|
|
|
if l1 == nil {
|
|
|
|
l1 = &gps.SimpleLock{}
|
|
|
|
}
|
|
|
|
if l2 == nil {
|
|
|
|
l2 = &gps.SimpleLock{}
|
|
|
|
}
|
|
|
|
|
|
|
|
p1, p2 := l1.Projects(), l2.Projects()
|
|
|
|
|
|
|
|
// Check if the slices are sorted already. If they are, we can compare
|
|
|
|
// without copying. Otherwise, we have to copy to avoid altering the
|
|
|
|
// original input.
|
|
|
|
sp1, sp2 := SortedLockedProjects(p1), SortedLockedProjects(p2)
|
|
|
|
if len(p1) > 1 && !sort.IsSorted(sp1) {
|
|
|
|
p1 = make([]gps.LockedProject, len(p1))
|
|
|
|
copy(p1, l1.Projects())
|
|
|
|
sort.Sort(SortedLockedProjects(p1))
|
|
|
|
}
|
|
|
|
if len(p2) > 1 && !sort.IsSorted(sp2) {
|
|
|
|
p2 = make([]gps.LockedProject, len(p2))
|
|
|
|
copy(p2, l2.Projects())
|
|
|
|
sort.Sort(SortedLockedProjects(p2))
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := LockDiff{}
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
h1 := hex.EncodeToString(l1.InputHash())
|
|
|
|
h2 := hex.EncodeToString(l2.InputHash())
|
|
|
|
if h1 != h2 {
|
|
|
|
diff.HashDiff = &StringDiff{Previous: h1, Current: h2}
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var i2next int
|
|
|
|
for i1 := 0; i1 < len(p1); i1++ {
|
|
|
|
lp1 := p1[i1]
|
|
|
|
pr1 := lp1.Ident().ProjectRoot
|
|
|
|
|
|
|
|
var matched bool
|
|
|
|
for i2 := i2next; i2 < len(p2); i2++ {
|
|
|
|
lp2 := p2[i2]
|
|
|
|
pr2 := lp2.Ident().ProjectRoot
|
|
|
|
|
|
|
|
switch strings.Compare(string(pr1), string(pr2)) {
|
|
|
|
case 0: // Found a matching project
|
|
|
|
matched = true
|
|
|
|
pdiff := diffProjects(lp1, lp2)
|
|
|
|
if pdiff != nil {
|
|
|
|
diff.Modify = append(diff.Modify, *pdiff)
|
|
|
|
}
|
|
|
|
i2next = i2 + 1 // Don't evaluate to this again
|
|
|
|
case -1: // Found a new project
|
2017-03-06 20:39:39 +03:00
|
|
|
add := buildLockedProjectDiff(lp2)
|
2017-03-01 23:52:09 +03:00
|
|
|
diff.Add = append(diff.Add, add)
|
|
|
|
i2next = i2 + 1 // Don't evaluate to this again
|
|
|
|
continue // Keep looking for a matching project
|
|
|
|
case +1: // Project has been removed, handled below
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
break // Done evaluating this project, move onto the next
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matched {
|
2017-03-06 20:39:39 +03:00
|
|
|
remove := buildLockedProjectDiff(lp1)
|
|
|
|
diff.Remove = append(diff.Remove, remove)
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything that still hasn't been evaluated are adds
|
|
|
|
for i2 := i2next; i2 < len(p2); i2++ {
|
|
|
|
lp2 := p2[i2]
|
2017-03-06 20:39:39 +03:00
|
|
|
add := buildLockedProjectDiff(lp2)
|
2017-03-01 23:52:09 +03:00
|
|
|
diff.Add = append(diff.Add, add)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff.HashDiff == nil && len(diff.Add) == 0 && len(diff.Remove) == 0 && len(diff.Modify) == 0 {
|
|
|
|
return nil // The locks are the equivalent
|
|
|
|
}
|
|
|
|
return &diff
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:39:39 +03:00
|
|
|
func buildLockedProjectDiff(lp gps.LockedProject) LockedProjectDiff {
|
2017-03-01 23:52:09 +03:00
|
|
|
r2, b2, v2 := getVersionInfo(lp.Version())
|
|
|
|
var rev, version, branch *StringDiff
|
|
|
|
if r2 != "" {
|
|
|
|
rev = &StringDiff{Previous: r2, Current: r2}
|
|
|
|
}
|
|
|
|
if b2 != "" {
|
|
|
|
branch = &StringDiff{Previous: b2, Current: b2}
|
|
|
|
}
|
|
|
|
if v2 != "" {
|
|
|
|
version = &StringDiff{Previous: v2, Current: v2}
|
|
|
|
}
|
|
|
|
add := LockedProjectDiff{
|
|
|
|
Name: lp.Ident().ProjectRoot,
|
|
|
|
Revision: rev,
|
|
|
|
Version: version,
|
|
|
|
Branch: branch,
|
|
|
|
Packages: make([]StringDiff, len(lp.Packages())),
|
|
|
|
}
|
|
|
|
for i, pkg := range lp.Packages() {
|
|
|
|
add.Packages[i] = StringDiff{Previous: pkg, Current: pkg}
|
|
|
|
}
|
|
|
|
return add
|
|
|
|
}
|
|
|
|
|
|
|
|
// diffProjects compares two projects and identifies the differences between them.
|
|
|
|
// Returns nil if there are no differences
|
|
|
|
// TODO(carolynvs) this should be moved to gps and updated once the gps unexported fields are available to use.
|
|
|
|
func diffProjects(lp1 gps.LockedProject, lp2 gps.LockedProject) *LockedProjectDiff {
|
|
|
|
diff := LockedProjectDiff{Name: lp1.Ident().ProjectRoot}
|
|
|
|
|
|
|
|
s1 := lp1.Ident().Source
|
|
|
|
s2 := lp2.Ident().Source
|
|
|
|
if s1 != s2 {
|
2017-03-06 20:35:47 +03:00
|
|
|
diff.Source = &StringDiff{Previous: s1, Current: s2}
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
r1, b1, v1 := getVersionInfo(lp1.Version())
|
|
|
|
r2, b2, v2 := getVersionInfo(lp2.Version())
|
|
|
|
if r1 != r2 {
|
|
|
|
diff.Revision = &StringDiff{Previous: r1, Current: r2}
|
|
|
|
}
|
|
|
|
if b1 != b2 {
|
|
|
|
diff.Branch = &StringDiff{Previous: b1, Current: b2}
|
|
|
|
}
|
|
|
|
if v1 != v2 {
|
|
|
|
diff.Version = &StringDiff{Previous: v1, Current: v2}
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 := lp1.Packages()
|
|
|
|
p2 := lp2.Packages()
|
|
|
|
if !sort.StringsAreSorted(p1) {
|
|
|
|
p1 = make([]string, len(p1))
|
|
|
|
copy(p1, lp1.Packages())
|
|
|
|
sort.Strings(p1)
|
|
|
|
}
|
|
|
|
if !sort.StringsAreSorted(p2) {
|
|
|
|
p2 = make([]string, len(p2))
|
|
|
|
copy(p2, lp2.Packages())
|
|
|
|
sort.Strings(p2)
|
|
|
|
}
|
|
|
|
|
|
|
|
var i2next int
|
|
|
|
for i1 := 0; i1 < len(p1); i1++ {
|
|
|
|
pkg1 := p1[i1]
|
|
|
|
|
|
|
|
var matched bool
|
|
|
|
for i2 := i2next; i2 < len(p2); i2++ {
|
|
|
|
pkg2 := p2[i2]
|
|
|
|
|
|
|
|
switch strings.Compare(pkg1, pkg2) {
|
|
|
|
case 0: // Found matching package
|
|
|
|
matched = true
|
|
|
|
i2next = i2 + 1 // Don't evaluate to this again
|
|
|
|
case +1: // Found a new package
|
|
|
|
add := StringDiff{Current: pkg2}
|
|
|
|
diff.Packages = append(diff.Packages, add)
|
|
|
|
i2next = i2 + 1 // Don't evaluate to this again
|
|
|
|
continue // Keep looking for a match
|
|
|
|
case -1: // Package has been removed (handled below)
|
|
|
|
}
|
|
|
|
|
|
|
|
break // Done evaluating this package, move onto the next
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matched {
|
|
|
|
diff.Packages = append(diff.Packages, StringDiff{Previous: pkg1})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything that still hasn't been evaluated are adds
|
|
|
|
for i2 := i2next; i2 < len(p2); i2++ {
|
|
|
|
pkg2 := p2[i2]
|
|
|
|
add := StringDiff{Current: pkg2}
|
|
|
|
diff.Packages = append(diff.Packages, add)
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:35:47 +03:00
|
|
|
if diff.Source == nil && diff.Version == nil && diff.Revision == nil && len(diff.Packages) == 0 {
|
2017-03-01 23:52:09 +03:00
|
|
|
return nil // The projects are equivalent
|
|
|
|
}
|
|
|
|
return &diff
|
|
|
|
}
|
2017-03-13 13:43:21 +03:00
|
|
|
|
|
|
|
func PruneProject(p *Project, sm gps.SourceManager) error {
|
|
|
|
td, err := ioutil.TempDir(os.TempDir(), "dep")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error while creating temp dir for writing manifest/lock/vendor")
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
if err := gps.WriteDepTree(td, p.Lock, sm, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var toKeep []string
|
|
|
|
for _, project := range p.Lock.Projects() {
|
|
|
|
projectRoot := string(project.Ident().ProjectRoot)
|
|
|
|
for _, pkg := range project.Packages() {
|
|
|
|
toKeep = append(toKeep, filepath.Join(projectRoot, pkg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toDelete, err := calculatePrune(td, toKeep)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := deleteDirs(toDelete); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vpath := filepath.Join(p.AbsRoot, "vendor")
|
|
|
|
vendorbak := vpath + ".orig"
|
|
|
|
var failerr error
|
|
|
|
if _, err := os.Stat(vpath); err == nil {
|
|
|
|
// Move out the old vendor dir. just do it into an adjacent dir, to
|
|
|
|
// try to mitigate the possibility of a pointless cross-filesystem
|
|
|
|
// move with a temp directory.
|
|
|
|
if _, err := os.Stat(vendorbak); err == nil {
|
|
|
|
// If the adjacent dir already exists, bite the bullet and move
|
|
|
|
// to a proper tempdir.
|
|
|
|
vendorbak = filepath.Join(td, "vendor.orig")
|
|
|
|
}
|
|
|
|
failerr = renameWithFallback(vpath, vendorbak)
|
|
|
|
if failerr != nil {
|
|
|
|
goto fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move in the new one.
|
|
|
|
failerr = renameWithFallback(td, vpath)
|
|
|
|
if failerr != nil {
|
|
|
|
goto fail
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(vendorbak)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
fail:
|
|
|
|
renameWithFallback(vendorbak, vpath)
|
|
|
|
return failerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculatePrune(vendorDir string, keep []string) ([]string, error) {
|
|
|
|
sort.Strings(keep)
|
|
|
|
toDelete := []string{}
|
|
|
|
err := filepath.Walk(vendorDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if _, err := os.Lstat(path); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if path == vendorDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := strings.TrimPrefix(path, vendorDir+"/")
|
|
|
|
i := sort.Search(len(keep), func(i int) bool {
|
|
|
|
return name <= keep[i]
|
|
|
|
})
|
|
|
|
if i >= len(keep) || !strings.HasPrefix(keep[i], name) {
|
|
|
|
toDelete = append(toDelete, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return toDelete, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteDirs(toDelete []string) error {
|
|
|
|
// sort by length so we delete sub dirs first
|
|
|
|
sort.Sort(byLen(toDelete))
|
|
|
|
for _, path := range toDelete {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type byLen []string
|
|
|
|
|
|
|
|
func (a byLen) Len() int { return len(a) }
|
|
|
|
func (a byLen) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byLen) Less(i, j int) bool { return len(a[i]) > len(a[j]) }
|