forked from launchdarkly-labs/find-affected-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-affected-packages.go
201 lines (174 loc) · 5.54 KB
/
find-affected-packages.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
log.Fatalf("Usage: %s commit..commit [packages...]\n", os.Args[0])
}
commitRange := args[0]
filterPackages := args[1:]
localPackagesToDeps := packagesToDeps(filterPackages)
changedLocalPackages := changedLocalPackages(commitRange)
changedModules := changedModules(commitRange)
for _, affectedPackage := range calcAffectedPackages(localPackagesToDeps, changedLocalPackages, changedModules) {
fmt.Println(affectedPackage) // nolint:no-printf // inapplicable
}
}
// Determines which of the given local packages (from the keys of localPackagesToDeps) were affected by changes to the given list of changedLocalPackages or changedModules
func calcAffectedPackages(localPackagesToDeps packagesToDepMap, changedLocalPackages, changedModules []string) []string {
affectedPackageMap := make(map[string]bool)
for _, pkg := range changedLocalPackages {
if _, ok := localPackagesToDeps[pkg]; ok {
// Any directly changed package is affected, as long as it came back from `go list <filterPackages>`
affectedPackageMap[pkg] = true
}
// Add all packages which depend on this changed package (including indirectly)
for pkgPath, pkgDeps := range localPackagesToDeps {
if pkgDeps[pkg] {
affectedPackageMap[pkgPath] = true
}
}
}
for _, module := range changedModules {
for pkgPath, pkgDeps := range localPackagesToDeps {
for pkgDep := range pkgDeps {
if strings.HasPrefix(pkgDep, module+"/") || pkgDep == module {
// This local package was affected by a changed module or a subpackage of that module
affectedPackageMap[pkgPath] = true
}
}
}
}
affectedPackages := make([]string, 0, len(affectedPackageMap))
for affectedPackage := range affectedPackageMap {
affectedPackages = append(affectedPackages, affectedPackage)
}
sort.Strings(affectedPackages)
return affectedPackages
}
func currentModule() string {
cmd := exec.Command("go", "list", "-m")
cmdOut, err := cmd.Output()
if err != nil {
log.Fatalf("Could not run git go list -m: %v", err)
}
return strings.TrimSpace(string(cmdOut))
}
// Returns a list of local packages which were changed in the given commit range.
// This normalizes all changed paths to be their Go import path, so:
// "service" becomes "<module root>/service"
func changedLocalPackages(commitRange string) []string {
cmd := exec.Command("git", "diff", "--name-only", commitRange)
cmdOut, err := cmd.Output()
if err != nil {
log.Fatalf("Could not run git diff: %v", err)
}
return calcChangedLocalPackages(string(cmdOut), currentModule())
}
func calcChangedLocalPackages(gitDiffOut, thisModule string) []string {
packagesMap := make(map[string]bool)
for _, f := range strings.Split(gitDiffOut, "\n") {
f = strings.TrimSpace(f)
if f == "" {
continue
}
if !strings.HasSuffix(f, ".go") {
// skip non-Go files
continue
}
firstDir := strings.Split(f, string(filepath.Separator))[0]
if firstDir == "vendor" {
// skip vendored files
continue
}
pkg := filepath.Join(thisModule, filepath.Dir(f))
packagesMap[pkg] = true
}
packages := make([]string, 0, len(packagesMap))
for pkg := range packagesMap {
packages = append(packages, pkg)
}
sort.Strings(packages)
return packages
}
func changedModules(commitRange string) []string {
if _, err := os.Stat("go.sum"); os.IsNotExist(err) {
// No go module here.
return nil
}
cmd := exec.Command("git", "diff", commitRange, "go.sum")
cmdOut, err := cmd.Output()
if err != nil {
log.Fatalf("Could not run git diff on go.sum: %v", err)
}
return calcChangedModules(string(cmdOut))
}
func calcChangedModules(gitDiffOut string) []string {
moduleMap := make(map[string]bool)
passedToFileHeader := false
for _, line := range strings.Split(gitDiffOut, "\n") {
// Skip lines until we're safely past the "+++ b/go.mod" line
if !passedToFileHeader {
if strings.HasPrefix(line, "+++") {
passedToFileHeader = true
}
continue
}
if strings.HasPrefix(line, "+") {
lineContents := strings.TrimSpace(line[1:])
lineParts := strings.Split(lineContents, " ")
if len(lineParts) == 3 {
moduleMap[lineParts[0]] = true
}
}
}
modules := make([]string, 0, len(moduleMap))
for mod := range moduleMap {
modules = append(modules, mod)
}
sort.Strings(modules)
return modules
}
type packagesToDepMap map[string]map[string]bool
// Gets a map of all local packages (anything not inside vendor) to a map of their dependencies, for easy lookups.
func packagesToDeps(filterPackages []string) packagesToDepMap {
args := []string{"list", "-f", `{{.ImportPath}}|{{join .Deps ":"}}`}
// Default to ./..., but if filters were given, limit our dependency graph to packages matching them.
if len(filterPackages) == 0 {
args = append(args, "./...")
} else {
args = append(args, filterPackages...)
}
cmd := exec.Command("go", args...)
cmdOut, err := cmd.Output()
if err != nil {
log.Fatalf("Error running go list: %s", err)
}
return calcPackagesToDeps(string(cmdOut))
}
func calcPackagesToDeps(goListOut string) packagesToDepMap {
var result = make(packagesToDepMap)
for _, pkgLine := range strings.Split(goListOut, "\n") {
stringParts := strings.SplitN(pkgLine, "|", 2)
importPath := stringParts[0]
dependencyMap := make(map[string]bool)
if len(stringParts) == 2 {
dependencyPaths := strings.Split(stringParts[1], ":")
for _, depPath := range dependencyPaths {
dependencyMap[depPath] = true
}
result[importPath] = dependencyMap
}
}
return result
}