|
1 |
| -// Copyright 2016 The Go Authors. All rights reserved. |
| 1 | +// Copyright 2017 The Go Authors. All rights reserved. |
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | package main
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "bytes" |
9 | 8 | "flag"
|
10 |
| - "io/ioutil" |
11 |
| - "log" |
12 |
| - "os" |
13 |
| - "path/filepath" |
14 |
| - "sort" |
15 |
| - "strings" |
16 | 9 |
|
17 | 10 | "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" |
22 | 11 | )
|
23 | 12 |
|
24 |
| -const pruneShortHelp = `Prune the vendor tree of unused packages` |
| 13 | +const pruneShortHelp = `Pruning is now performed automatically by dep ensure.` |
25 | 14 | 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. |
30 | 18 | `
|
31 | 19 |
|
32 |
| -type pruneCommand struct { |
33 |
| -} |
| 20 | +type pruneCommand struct{} |
34 | 21 |
|
35 | 22 | func (cmd *pruneCommand) Name() string { return "prune" }
|
36 | 23 | func (cmd *pruneCommand) Args() string { return "" }
|
37 | 24 | func (cmd *pruneCommand) ShortHelp() string { return pruneShortHelp }
|
38 | 25 | func (cmd *pruneCommand) LongHelp() string { return pruneLongHelp }
|
39 |
| -func (cmd *pruneCommand) Hidden() bool { return false } |
| 26 | +func (cmd *pruneCommand) Hidden() bool { return true } |
40 | 27 |
|
41 |
| -func (cmd *pruneCommand) Register(fs *flag.FlagSet) { |
42 |
| -} |
| 28 | +func (cmd *pruneCommand) Register(fs *flag.FlagSet) {} |
43 | 29 |
|
44 | 30 | 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") |
155 | 34 |
|
156 | 35 | return nil
|
157 |
| - |
158 |
| -fail: |
159 |
| - fs.RenameWithFallback(vendorbak, vpath) |
160 |
| - return failerr |
161 | 36 | }
|
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]) } |
0 commit comments