Skip to content

Commit 5e51c89

Browse files
committed
Include new packages in output, performance tweaks
Favor time over space by using more maps and less appends. Include new packages in the output so that new services will also be deployed
1 parent 1e65631 commit 5e51c89

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

main.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"sort"
1213
"strings"
1314
)
1415

@@ -26,7 +27,7 @@ more of its dependacies have been modified`)
2627
}
2728

2829
var (
29-
packages []string // the packages to check for taint
30+
packages map[string]struct{} // the packages to check for taint
3031
changedDirs map[string]struct{} // the directories which contain modified files
3132
cache map[string]*build.Package // a map[>package name>]<build.Package> to skip repeat lookups
3233
gitDirPtr *string // the git directory to check for changes
@@ -37,6 +38,7 @@ var (
3738
func init() {
3839
cache = make(map[string]*build.Package)
3940
changedDirs = make(map[string]struct{})
41+
packages = make(map[string]struct{})
4042
}
4143

4244
func main() {
@@ -70,16 +72,27 @@ func main() {
7072
if err != nil {
7173
log.Fatal(err)
7274
}
73-
for _, v := range packages {
75+
output := make(map[string]struct{})
76+
for k := range packages {
7477
// get all the deps
75-
deps, err := findDeps(v, cwd)
78+
deps, err := findDeps(k, cwd)
7679
if err != nil {
7780
log.Fatal(err)
7881
}
7982
if hasChanges(deps) {
80-
fmt.Println(v)
83+
output[k] = struct{}{}
8184
}
8285
}
86+
// finally to make it all pretty, sort it in a slice
87+
prettyOutput := make([]string, 0, len(output))
88+
for k := range output {
89+
prettyOutput = append(prettyOutput, k)
90+
}
91+
if len(prettyOutput) == 0 {
92+
return
93+
}
94+
sort.Strings(prettyOutput)
95+
fmt.Println(strings.Join(prettyOutput, "\n"))
8396
}
8497

8598
// checks to see if any of the deps have the same suffix as anything in the changedDirs
@@ -98,7 +111,7 @@ func hasChanges(deps []string) bool {
98111
func readPackages() {
99112
scanner := bufio.NewScanner(os.Stdin)
100113
for scanner.Scan() {
101-
packages = append(packages, scanner.Text())
114+
packages[scanner.Text()] = struct{}{}
102115
}
103116
if err := scanner.Err(); err != nil {
104117
log.Fatal(err)

packages.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
type context struct {
9-
soFar map[string]bool
9+
soFar map[string]struct{}
1010
ctx build.Context
1111
}
1212

@@ -28,11 +28,11 @@ func (c *context) find(name, dir string) (err error) {
2828
}
2929

3030
if name != "." {
31-
c.soFar[pkg.ImportPath] = true
31+
c.soFar[pkg.ImportPath] = struct{}{}
3232
}
3333
imports := pkg.Imports
3434
for _, imp := range imports {
35-
if !c.soFar[imp] {
35+
if _, ok := c.soFar[imp]; !ok {
3636
if err := c.find(imp, pkg.Dir); err != nil {
3737
return err
3838
}
@@ -45,17 +45,15 @@ func findDeps(name, dir string) ([]string, error) {
4545
ctx := build.Default
4646

4747
c := &context{
48-
soFar: make(map[string]bool),
48+
soFar: make(map[string]struct{}),
4949
ctx: ctx,
5050
}
5151
if err := c.find(name, dir); err != nil {
5252
return nil, err
5353
}
5454
deps := make([]string, 0, len(c.soFar))
5555
for p := range c.soFar {
56-
if p != name {
57-
deps = append(deps, p)
58-
}
56+
deps = append(deps, p)
5957
}
6058
sort.Strings(deps)
6159
return deps, nil

0 commit comments

Comments
 (0)