forked from jharlap/affected
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
182 lines (156 loc) · 3.65 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/tools/go/buildutil"
)
var ignoreDirs []string
func main() {
d := flag.String("ignore-dirs", ".checkout_git", "Dir patterns (static string) to ignore, comma-separated")
flag.Parse()
if len(*d) > 0 {
ignoreDirs = strings.Split(*d, ",")
}
args := flag.Args()
if len(args) != 1 {
die("Usage: %s commit..commit\n", os.Args[0])
}
commitRange := args[0]
files := changedFiles(commitRange)
cwd, err := os.Getwd()
if err != nil {
die("Could not get CWD: %s", err)
}
repo := gitRoot()
pkgSeen := make(map[string]bool)
buildContext := build.Default
for _, f := range files {
if isIgnored(f) {
continue
}
pkg, err := buildutil.ContainingPackage(&buildContext, cwd, f)
if err != nil {
fmt.Fprintf(os.Stderr, "Error finding package for file %s: %s\n", f, err)
continue
}
if pkg.Goroot {
fmt.Fprintf(os.Stderr, "Ignoring STDLIB file %s\n", f)
continue
}
if pkgSeen[pkg.ImportPath] {
continue
}
pkgSeen[pkg.ImportPath] = true
}
// TODO: list all packages in the repo, determine dependency tree and filter package list to those that transitively import affected packages
// build the import tree
var (
imports = make(map[string][]string)
scanErr error
)
buildutil.ForEachPackage(&buildContext, func(importPath string, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read package %s: %s\n", importPath, err)
scanErr = err
return
}
if isIgnored(importPath) {
return
}
pkg, err := buildContext.Import(importPath, repo, build.AllowBinary)
if err, ok := err.(*build.NoGoError); err != nil && ok {
return
}
if err != nil {
fmt.Fprintf(os.Stderr, "Could not import dir %s: %s\n", importPath, err)
scanErr = err
return
}
for _, imp := range pkg.Imports {
imports[imp] = append(imports[imp], importPath)
}
})
if scanErr != nil {
die("Package scan incomplete, aborting")
}
// filter the package list to those affected
var affectedPackages []string
for path := range pkgSeen {
affectedPackages = append(affectedPackages, path)
}
addedMore := true
for addedMore {
addedMore = false
var newAdditions []string
for _, p := range affectedPackages {
for _, imp := range imports[p] {
if isIgnored(imp) {
continue
}
var found bool
for _, p := range affectedPackages {
if imp == p {
found = true
break
}
}
for _, p := range newAdditions {
if imp == p {
found = true
break
}
}
if found {
continue
}
newAdditions = append(newAdditions, imp)
}
}
addedMore = len(newAdditions) > 0
affectedPackages = append(affectedPackages, newAdditions...)
}
fmt.Println(strings.Join(affectedPackages, "\n"))
}
func die(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
}
func isIgnored(f string) bool {
for _, d := range ignoreDirs {
if strings.Contains(f, d) {
return true
}
}
return false
}
func gitRoot() string {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
dat, err := cmd.Output()
if err != nil {
die("Could not find git root: %s", err)
}
return strings.TrimSpace(string(dat))
}
func changedFiles(commitRange string) []string {
root := gitRoot()
cmd := exec.Command("git", "diff", "--name-only", commitRange)
dat, err := cmd.Output()
if err != nil {
die("Could not run git diff-tree: %v", err)
}
files := strings.Split(string(dat), "\n")
var res []string
for _, f := range files {
f = strings.TrimSpace(f)
if len(f) == 0 {
continue
}
res = append(res, filepath.Join(root, f))
}
return res
}