-
Notifications
You must be signed in to change notification settings - Fork 9
/
purge.go
206 lines (191 loc) · 5.22 KB
/
purge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"sort"
"strings"
)
// Purge directories in the indicated gopaths if they to not contain source
// referenced from a non-indicated gopath.
func purge(w *workspace, args []string) {
confirmed := false
var gopaths []string
for _, a := range args {
if a == "--confirm" {
confirmed = true
continue
}
gopaths = append(gopaths, a)
}
bctx := build.Default
bctx.GOPATH = w.Gopath(false)
if len(gopaths) == 0 {
gopaths = w.Gopaths[:1] // By default, the first one is vendor/.
}
if len(gopaths) == 0 {
fmt.Fprintf(os.Stderr, "must purge at least one GOPATH\n")
os.Exit(1)
}
wgps := map[string]bool{}
for _, wpg := range w.Gopaths {
wgps[wpg] = true
}
pgs := map[string]bool{}
for _, pg := range gopaths {
pgs[pg] = true
if !wgps[pg] {
fmt.Fprintf(os.Stderr, "unknown GOPATH %q", pg)
os.Exit(1)
}
}
if len(gopaths) == len(w.Gopaths) {
fmt.Fprintf(os.Stderr, "cannot purge all GOPATHs; try 'rm -r' instead\n")
os.Exit(1)
}
// Collect a set of safe directories that will not get purged.
safeDirs := []string{}
// Keep them in a map too, to protect against loops.
safeDirsAll := map[string]bool{}
// Start by adding all directories in the non-purged gopaths.
for _, wpg := range w.Gopaths {
if pgs[wpg] {
// skip the purged ones
continue
}
// Only deal with abs paths from here on out, to make checking easier.
if !filepath.IsAbs(wpg) {
wpg = filepath.Join(w.Root, wpg)
}
filepath.Walk(filepath.Join(wpg, "src"), func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
safeDirs = append(safeDirs, path)
safeDirsAll[path] = true
}
return nil
})
}
// Go through each safe dir and add its subsafedirs to the end of the list.
for i := 0; i < len(safeDirs); i++ {
deps, err := getDepDirs(bctx, safeDirs[i])
if err != nil {
fmt.Fprintf(os.Stderr, "problem inspecting %s: %v\n", safeDirs[i], err)
os.Exit(1)
}
for _, d := range deps {
if safeDirsAll[d] {
// cut off cycles
continue
}
safeDirs = append(safeDirs, d)
safeDirsAll[d] = true
}
}
// Expand the list of safe dirs to be all parents of safe dirs, to make checking easier later.
for dir := range safeDirsAll {
for _, parent := range getAllParents(dir) {
safeDirsAll[parent] = true
}
}
// Armed with a list of safe dirs, go through gopaths and purge those that aren't safe.
dirsToPurge := map[string]bool{}
for _, pg := range gopaths {
if !filepath.IsAbs(pg) {
pg = filepath.Join(w.Root, pg)
}
filepath.Walk(filepath.Join(pg, "src"), func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
// If this directory is safe, or is the parent of somethinge safe, we keep it.
// We stored all the parents of the safe directories so we only need to do a single check here.
if safeDirsAll[path] {
return nil
}
// Not safe, will be purged.
dirsToPurge[path] = true
return nil
})
}
for d := range dirsToPurge {
// If we're already removing a parent, forget about this directory.
pds := getAllParents(d)
for _, pd := range pds {
if dirsToPurge[pd] {
dirsToPurge[d] = false
} else {
//fmt.Println("no parent", pd, d)
}
}
}
sortedPurge := []string{}
for d, rm := range dirsToPurge {
if rm {
rd, err := filepath.Rel(w.Root, d)
if err != nil {
fmt.Fprintf(os.Stderr, "problem with relative path for %q: %v\n", d, err)
os.Exit(1)
}
if strings.HasPrefix(rd, "..") {
fmt.Fprintf(os.Stderr, "skipping path outside of workspace %q", d)
continue
}
sortedPurge = append(sortedPurge, rd)
}
}
sort.Strings(sortedPurge)
if !confirmed {
fmt.Fprintln(os.Stderr, "Directories containing no imported source:")
for _, d := range sortedPurge {
fmt.Println(d)
}
fmt.Fprintln(os.Stderr, "To delete the listed directories, run this command again with '--confirm'.")
os.Exit(0)
}
for _, d := range sortedPurge {
if err := os.RemoveAll(filepath.Join(w.Root, d)); err != nil {
fmt.Println("Error removing %q: %v\n", d, err)
}
}
}
func getDepDirs(bctx build.Context, dir string) ([]string, error) {
pkg, err := bctx.ImportDir(dir, 0)
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
return nil, err
}
}
depDirs := []string{}
for _, imp := range pkg.Imports {
depPkg, err := bctx.Import(imp, dir, 0)
if err == nil {
depDirs = append(depDirs, depPkg.Dir)
}
}
return depDirs, nil
}
func getAllParents(dir string) []string {
var parents []string
for {
parent, _ := filepath.Split(dir)
parent = filepath.Clean(parent)
if parent == dir {
return parents
}
parents = append(parents, parent)
dir = parent
}
}