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

Commit 0368da4

Browse files
committed
ensure: Absorb dep prune into dep ensure
This merge incorporates the long-running branch that consolidated all the work related to absorbing dep prune into dep ensure. As of this commit, dep prune is now a hidden dummy command, and dep ensure does all the heavy lifting automatically.
2 parents 52307d4 + 142735b commit 0368da4

File tree

49 files changed

+1238
-1191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1238
-1191
lines changed

cmd/dep/ensure.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
279279
// that "verification" is supposed to look like (#121); in the meantime,
280280
// we unconditionally write out vendor/ so that `dep ensure`'s behavior
281281
// is maximally compatible with what it will eventually become.
282-
sw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways)
282+
sw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions)
283283
if err != nil {
284284
return err
285285
}
@@ -304,7 +304,7 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
304304
return handleAllTheFailuresOfTheWorld(err)
305305
}
306306

307-
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), cmd.vendorBehavior())
307+
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), cmd.vendorBehavior(), p.Manifest.PruneOptions)
308308
if err != nil {
309309
return err
310310
}
@@ -329,7 +329,7 @@ func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Proj
329329
}
330330
// Pass the same lock as old and new so that the writer will observe no
331331
// difference and choose not to write it out.
332-
sw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways)
332+
sw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions)
333333
if err != nil {
334334
return err
335335
}
@@ -394,7 +394,7 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
394394
return handleAllTheFailuresOfTheWorld(err)
395395
}
396396

397-
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), cmd.vendorBehavior())
397+
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), cmd.vendorBehavior(), p.Manifest.PruneOptions)
398398
if err != nil {
399399
return err
400400
}
@@ -693,7 +693,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
693693
}
694694
sort.Strings(reqlist)
695695

696-
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), dep.VendorOnChanged)
696+
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), dep.VendorOnChanged, p.Manifest.PruneOptions)
697697
if err != nil {
698698
return err
699699
}

cmd/dep/init.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
118118
return errors.Wrap(err, "init failed: unable to prepare an initial manifest and lock for the solver")
119119
}
120120

121+
// Set default prune options for go-tests and unused-packages
122+
p.Manifest.PruneOptions.PruneOptions = gps.PruneNestedVendorDirs + gps.PruneGoTestFiles + gps.PruneUnusedPackages
123+
121124
if cmd.gopath {
122125
gs := newGopathScanner(ctx, directDeps, sm)
123126
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
@@ -177,7 +180,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
177180
ctx.Err.Printf("Old vendor backed up to %v", vendorbak)
178181
}
179182

180-
sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways)
183+
sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions)
181184
if err != nil {
182185
return errors.Wrap(err, "init failed: unable to create a SafeWriter")
183186
}

cmd/dep/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (c *Config) Run() int {
6767
&initCommand{},
6868
&statusCommand{},
6969
&ensureCommand{},
70-
&hashinCommand{},
7170
&pruneCommand{},
71+
&hashinCommand{},
7272
&versionCommand{},
7373
}
7474

cmd/dep/prune.go

+11-181
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,36 @@
1-
// Copyright 2016 The Go Authors. All rights reserved.
1+
// Copyright 2017 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

55
package main
66

77
import (
8-
"bytes"
98
"flag"
10-
"io/ioutil"
11-
"log"
12-
"os"
13-
"path/filepath"
14-
"sort"
15-
"strings"
169

1710
"github.com/golang/dep"
18-
"github.com/golang/dep/gps"
19-
"github.com/golang/dep/gps/pkgtree"
20-
"github.com/golang/dep/internal/fs"
21-
"github.com/pkg/errors"
2211
)
2312

24-
const pruneShortHelp = `Prune the vendor tree of unused packages`
13+
const pruneShortHelp = `Pruning is now performed automatically by dep ensure.`
2514
const pruneLongHelp = `
26-
Prune is used to remove unused packages from your vendor tree.
27-
28-
STABILITY NOTICE: this command creates problems for vendor/ verification. As
29-
such, it may be removed and/or moved out into a separate project later on.
15+
Prune was merged into the ensure command.
16+
Set prune options in the manifest and it will be applied after every ensure.
17+
dep prune will be removed in a future version of dep, causing this command to exit non-0.
3018
`
3119

32-
type pruneCommand struct {
33-
}
20+
type pruneCommand struct{}
3421

3522
func (cmd *pruneCommand) Name() string { return "prune" }
3623
func (cmd *pruneCommand) Args() string { return "" }
3724
func (cmd *pruneCommand) ShortHelp() string { return pruneShortHelp }
3825
func (cmd *pruneCommand) LongHelp() string { return pruneLongHelp }
39-
func (cmd *pruneCommand) Hidden() bool { return false }
26+
func (cmd *pruneCommand) Hidden() bool { return true }
4027

41-
func (cmd *pruneCommand) Register(fs *flag.FlagSet) {
42-
}
28+
func (cmd *pruneCommand) Register(fs *flag.FlagSet) {}
4329

4430
func (cmd *pruneCommand) Run(ctx *dep.Ctx, args []string) error {
45-
p, err := ctx.LoadProject()
46-
if err != nil {
47-
return err
48-
}
49-
50-
sm, err := ctx.SourceManager()
51-
if err != nil {
52-
return err
53-
}
54-
sm.UseDefaultSignalHandling()
55-
defer sm.Release()
56-
57-
// While the network churns on ListVersions() requests, statically analyze
58-
// code from the current project.
59-
ptree, err := pkgtree.ListPackages(p.ResolvedAbsRoot, string(p.ImportRoot))
60-
if err != nil {
61-
return errors.Wrap(err, "analysis of local packages failed: %v")
62-
}
63-
64-
// Set up a solver in order to check the InputHash.
65-
params := p.MakeParams()
66-
params.RootPackageTree = ptree
67-
68-
if ctx.Verbose {
69-
params.TraceLogger = ctx.Err
70-
}
71-
72-
s, err := gps.Prepare(params, sm)
73-
if err != nil {
74-
return errors.Wrap(err, "could not set up solver for input hashing")
75-
}
76-
77-
if p.Lock == nil {
78-
return errors.Errorf("Gopkg.lock must exist for prune to know what files are safe to remove.")
79-
}
80-
81-
if !bytes.Equal(s.HashInputs(), p.Lock.SolveMeta.InputsDigest) {
82-
return errors.Errorf("Gopkg.lock is out of sync; run dep ensure before pruning.")
83-
}
84-
85-
pruneLogger := ctx.Err
86-
if !ctx.Verbose {
87-
pruneLogger = log.New(ioutil.Discard, "", 0)
88-
}
89-
return pruneProject(p, sm, pruneLogger)
90-
}
91-
92-
// pruneProject removes unused packages from a project.
93-
func pruneProject(p *dep.Project, sm gps.SourceManager, logger *log.Logger) error {
94-
td, err := ioutil.TempDir(os.TempDir(), "dep")
95-
if err != nil {
96-
return errors.Wrap(err, "error while creating temp dir for writing manifest/lock/vendor")
97-
}
98-
defer os.RemoveAll(td)
99-
100-
if err := gps.WriteDepTree(td, p.Lock, sm, true, logger); err != nil {
101-
return err
102-
}
103-
104-
var toKeep []string
105-
for _, project := range p.Lock.Projects() {
106-
projectRoot := string(project.Ident().ProjectRoot)
107-
for _, pkg := range project.Packages() {
108-
toKeep = append(toKeep, filepath.Join(projectRoot, pkg))
109-
}
110-
}
111-
112-
toDelete, err := calculatePrune(td, toKeep, logger)
113-
if err != nil {
114-
return err
115-
}
116-
117-
if len(toDelete) > 0 {
118-
logger.Println("Calculated the following directories to prune:")
119-
for _, d := range toDelete {
120-
logger.Printf(" %s\n", d)
121-
}
122-
} else {
123-
logger.Println("No directories found to prune")
124-
}
125-
126-
if err := deleteDirs(toDelete); err != nil {
127-
return err
128-
}
129-
130-
vpath := filepath.Join(p.AbsRoot, "vendor")
131-
vendorbak := vpath + ".orig"
132-
var failerr error
133-
if _, err := os.Stat(vpath); err == nil {
134-
// Move out the old vendor dir. just do it into an adjacent dir, to
135-
// try to mitigate the possibility of a pointless cross-filesystem
136-
// move with a temp directory.
137-
if _, err := os.Stat(vendorbak); err == nil {
138-
// If the adjacent dir already exists, bite the bullet and move
139-
// to a proper tempdir.
140-
vendorbak = filepath.Join(td, "vendor.orig")
141-
}
142-
failerr = fs.RenameWithFallback(vpath, vendorbak)
143-
if failerr != nil {
144-
goto fail
145-
}
146-
}
147-
148-
// Move in the new one.
149-
failerr = fs.RenameWithFallback(td, vpath)
150-
if failerr != nil {
151-
goto fail
152-
}
153-
154-
os.RemoveAll(vendorbak)
31+
ctx.Out.Printf("Pruning is now performed automatically by dep ensure.\n")
32+
ctx.Out.Printf("Set prune settings in %s and it it will be applied when running ensure.\n", dep.ManifestName)
33+
ctx.Out.Printf("\ndep prune will be removed in a future version, and this command will exit non-0.\nPlease update your scripts.\n")
15534

15635
return nil
157-
158-
fail:
159-
fs.RenameWithFallback(vendorbak, vpath)
160-
return failerr
16136
}
162-
163-
func calculatePrune(vendorDir string, keep []string, logger *log.Logger) ([]string, error) {
164-
logger.Println("Calculating prune. Checking the following packages:")
165-
sort.Strings(keep)
166-
toDelete := []string{}
167-
err := filepath.Walk(vendorDir, func(path string, info os.FileInfo, err error) error {
168-
if _, err := os.Lstat(path); err != nil {
169-
return nil
170-
}
171-
if !info.IsDir() {
172-
return nil
173-
}
174-
if path == vendorDir {
175-
return nil
176-
}
177-
178-
name := strings.TrimPrefix(path, vendorDir+string(filepath.Separator))
179-
logger.Printf(" %s", name)
180-
i := sort.Search(len(keep), func(i int) bool {
181-
return name <= keep[i]
182-
})
183-
if i >= len(keep) || !strings.HasPrefix(keep[i], name) {
184-
toDelete = append(toDelete, path)
185-
}
186-
return nil
187-
})
188-
return toDelete, err
189-
}
190-
191-
func deleteDirs(toDelete []string) error {
192-
// sort by length so we delete sub dirs first
193-
sort.Sort(byLen(toDelete))
194-
for _, path := range toDelete {
195-
if err := os.RemoveAll(path); err != nil {
196-
return err
197-
}
198-
}
199-
return nil
200-
}
201-
202-
type byLen []string
203-
204-
func (a byLen) Len() int { return len(a) }
205-
func (a byLen) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
206-
func (a byLen) Less(i, j int) bool { return len(a[i]) > len(a[j]) }

cmd/dep/prune_test.go

-50
This file was deleted.

cmd/dep/testdata/harness_tests/ensure/add/all-new-double-spec/final/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
branch = "master"
44
name = "github.com/sdboyer/deptesttres"
55

6+
[prune]
7+
go-tests = true
8+
unused-packages = true
9+
610
[[constraint]]
711
name = "github.com/sdboyer/deptest"
812
version = "0.8.1"

cmd/dep/testdata/harness_tests/ensure/add/all-new-double/final/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
name = "github.com/sdboyer/deptest"
44
version = "1.0.0"
55

6+
[prune]
7+
go-tests = true
8+
unused-packages = true
9+
610
[[constraint]]
711
branch = "master"
812
name = "github.com/sdboyer/deptesttres"

cmd/dep/testdata/harness_tests/ensure/add/all-new-spec/final/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
branch = "master"
44
name = "github.com/sdboyer/deptesttres"
55

6+
[prune]
7+
go-tests = true
8+
unused-packages = true
9+
610
[[constraint]]
711
name = "github.com/sdboyer/deptest"
812
version = "0.8.1"

cmd/dep/testdata/harness_tests/ensure/add/all-new/final/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
branch = "master"
44
name = "github.com/sdboyer/deptesttres"
55

6+
[prune]
7+
go-tests = true
8+
unused-packages = true
9+
610
[[constraint]]
711
name = "github.com/sdboyer/deptest"
812
version = "1.0.0"

cmd/dep/testdata/harness_tests/ensure/add/errs/double-diff-spec/final/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
[[constraint]]
33
branch = "master"
44
name = "github.com/sdboyer/deptesttres"
5+
6+
[prune]
7+
go-tests = true
8+
unused-packages = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[prune]
3+
go-tests = true
4+
unused-packages = true

0 commit comments

Comments
 (0)