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

Commit

Permalink
dep: Fix nil logger panic, cleanup delta output
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Jul 11, 2018
1 parent 5a7960a commit c73cf30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
10 changes: 5 additions & 5 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,18 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
lsat := verify.LockSatisfiesInputs(p.Lock, p.Manifest, params.RootPackageTree)
if !lsat.Satisfied() {
if ctx.Verbose {
ctx.Out.Println("Gopkg.lock is out of sync with Gopkg.toml and project code:")
ctx.Out.Println("# Gopkg.lock is out of sync with Gopkg.toml and project code:")
for _, missing := range lsat.MissingImports {
ctx.Out.Printf("\t%s is missing from input-imports\n", missing)
ctx.Out.Printf("%s: missing from input-imports\n", missing)
}
for _, excess := range lsat.ExcessImports {
ctx.Out.Printf("\t%s is in input-imports, but isn't imported\n", excess)
ctx.Out.Printf("%s: in input-imports, but isn't imported\n", excess)
}
for pr, unmatched := range lsat.UnmetOverrides {
ctx.Out.Printf("\t%s is at %s, which is not allowed by override %s\n", pr, unmatched.V, unmatched.C)
ctx.Out.Printf("%s@%s: not allowed by override %s\n", pr, unmatched.V, unmatched.C)
}
for pr, unmatched := range lsat.UnmetConstraints {
ctx.Out.Printf("\t%s is at %s, which is not allowed by constraint %s\n", pr, unmatched.V, unmatched.C)
ctx.Out.Printf("%s@%s: not allowed by constraint %s\n", pr, unmatched.V, unmatched.C)
}
ctx.Out.Println()
}
Expand Down
39 changes: 29 additions & 10 deletions txn_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package dep

import (
"context"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -348,6 +349,9 @@ fail:

// PrintPreparedActions logs the actions a call to Write would perform.
func (sw *SafeWriter) PrintPreparedActions(output *log.Logger, verbose bool) error {
if output == nil {
output = log.New(ioutil.Discard, "", 0)
}
if sw.HasManifest() {
if verbose {
m, err := sw.Manifest.MarshalTOML()
Expand Down Expand Up @@ -493,14 +497,18 @@ func (dw *DeltaWriter) Write(path string, sm gps.SourceManager, examples bool, l
return errors.Errorf("target path (%q) must be the parent of the original vendor path (%q)", path, dw.vendorDir)
}

if logger == nil {
logger = log.New(ioutil.Discard, "", 0)
}

lpath := filepath.Join(path, LockName)
vpath := dw.vendorDir

// Write the modified projects to a new adjacent directory. We use an
// adjacent directory to minimize the possibility of cross-filesystem renames
// becoming expensive copies, and to make removal of unneeded projects implicit
// and automatic.
vnewpath := vpath + "-new"
vnewpath := filepath.Join(filepath.Dir(vpath), ".vendor-new")
if _, err := os.Stat(vnewpath); err == nil {
return errors.Errorf("scratch directory %s already exists, please remove it", vnewpath)
}
Expand All @@ -518,6 +526,9 @@ func (dw *DeltaWriter) Write(path string, sm gps.SourceManager, examples bool, l
dropped := []gps.ProjectRoot{}
i := 0
tot := len(dw.changed)
if len(dw.changed) > 0 {
logger.Println("\n# Bringing vendor into sync")
}
for pr, reason := range dw.changed {
if reason == projectRemoved {
dropped = append(dropped, pr)
Expand Down Expand Up @@ -623,16 +634,12 @@ func changeExplanation(c changeType, lpd verify.LockedProjectDelta) string {
if lpd.SourceChanged() {
return fmt.Sprintf("source changed (%s -> %s)", lpd.SourceBefore, lpd.SourceAfter)
} else if lpd.VersionChanged() {
bv, av := "(none)", "(none)"
if lpd.VersionBefore != nil {
bv = lpd.VersionBefore.String()
}
if lpd.VersionAfter != nil {
av = lpd.VersionAfter.String()
if lpd.VersionBefore == nil {
return fmt.Sprintf("version changed (was a bare revision)")
}
return fmt.Sprintf("version changed (%s -> %s)", bv, av)
return fmt.Sprintf("version changed (was %s)", lpd.VersionAfter.String())
} else if lpd.RevisionChanged() {
return fmt.Sprintf("revision changed (%s -> %s)", lpd.RevisionBefore, lpd.RevisionAfter)
return fmt.Sprintf("revision changed (%s -> %s)", trimSHA(lpd.RevisionBefore), trimSHA(lpd.RevisionAfter))
} else if lpd.PackagesChanged() {
la, lr := len(lpd.PackagesAdded), len(lpd.PackagesRemoved)
if la > 0 && lr > 0 {
Expand All @@ -651,7 +658,7 @@ func changeExplanation(c changeType, lpd verify.LockedProjectDelta) string {
new := lpd.PruneOptsAfter & ^gps.PruneNestedVendorDirs
return fmt.Sprintf("prune options changed (%s -> %s)", old, new)
case hashMismatch:
return "hash mismatch between Gopkg.lock and vendor contents"
return "hash of vendored tree didn't match digest in Gopkg.lock"
case hashVersionMismatch:
return "hashing algorithm mismatch"
case hashAbsent:
Expand Down Expand Up @@ -708,3 +715,15 @@ type TreeWriter interface {
PrintPreparedActions(output *log.Logger, verbose bool) error
Write(path string, sm gps.SourceManager, examples bool, logger *log.Logger) error
}

// trimSHA checks if revision is a valid SHA1 digest and trims to 10 characters.
func trimSHA(revision gps.Revision) string {
if len(revision) == 40 {
if _, err := hex.DecodeString(string(revision)); err == nil {
// Valid SHA1 digest
revision = revision[0:10]
}
}

return string(revision)
}

0 comments on commit c73cf30

Please sign in to comment.