This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
Add dep prune subcommand #322
Merged
+188
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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. | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/golang/dep" | ||
"github.com/sdboyer/gps" | ||
"github.com/sdboyer/gps/pkgtree" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const pruneShortHelp = `Prune the vendor tree of unused packages` | ||
const pruneLongHelp = ` | ||
Prune is used to remove unused packages from your vendor tree. | ||
|
||
STABILITY NOTICE: this command creates problems for vendor/ verification. As | ||
such, it may be removed and/or moved out into a separate project later on. | ||
` | ||
|
||
type pruneCommand struct { | ||
} | ||
|
||
func (cmd *pruneCommand) Name() string { return "prune" } | ||
func (cmd *pruneCommand) Args() string { return "" } | ||
func (cmd *pruneCommand) ShortHelp() string { return pruneShortHelp } | ||
func (cmd *pruneCommand) LongHelp() string { return pruneLongHelp } | ||
func (cmd *pruneCommand) Hidden() bool { return false } | ||
|
||
func (cmd *pruneCommand) Register(fs *flag.FlagSet) { | ||
} | ||
|
||
func (cmd *pruneCommand) Run(ctx *dep.Ctx, args []string) error { | ||
p, err := ctx.LoadProject("") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sm, err := ctx.SourceManager() | ||
if err != nil { | ||
return err | ||
} | ||
sm.UseDefaultSignalHandling() | ||
defer sm.Release() | ||
|
||
// While the network churns on ListVersions() requests, statically analyze | ||
// code from the current project. | ||
ptree, err := pkgtree.ListPackages(p.AbsRoot, string(p.ImportRoot)) | ||
if err != nil { | ||
return errors.Wrap(err, "analysis of local packages failed: %v") | ||
} | ||
|
||
// Set up a solver in order to check the InputHash. | ||
params := gps.SolveParameters{ | ||
RootDir: p.AbsRoot, | ||
RootPackageTree: ptree, | ||
Manifest: p.Manifest, | ||
// Locks aren't a part of the input hash check, so we can omit it. | ||
} | ||
if *verbose { | ||
params.Trace = true | ||
params.TraceLogger = log.New(os.Stderr, "", 0) | ||
} | ||
|
||
s, err := gps.Prepare(params, sm) | ||
if err != nil { | ||
return errors.Wrap(err, "could not set up solver for input hashing") | ||
} | ||
|
||
if !bytes.Equal(s.HashInputs(), p.Lock.Memo) { | ||
return fmt.Errorf("lock hash doesn't match") | ||
} | ||
|
||
return dep.PruneProject(p, sm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I now just have one docs nit...let's please have this say the following:
I just want to warn people everywhere we can 😄