Skip to content

Commit

Permalink
Include new packages in output, performance tweaks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kynrai committed Jun 5, 2018
1 parent 1e65631 commit 5e51c89
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)

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

var (
packages []string // the packages to check for taint
packages map[string]struct{} // the packages to check for taint
changedDirs map[string]struct{} // the directories which contain modified files
cache map[string]*build.Package // a map[>package name>]<build.Package> to skip repeat lookups
gitDirPtr *string // the git directory to check for changes
Expand All @@ -37,6 +38,7 @@ var (
func init() {
cache = make(map[string]*build.Package)
changedDirs = make(map[string]struct{})
packages = make(map[string]struct{})
}

func main() {
Expand Down Expand Up @@ -70,16 +72,27 @@ func main() {
if err != nil {
log.Fatal(err)
}
for _, v := range packages {
output := make(map[string]struct{})
for k := range packages {
// get all the deps
deps, err := findDeps(v, cwd)
deps, err := findDeps(k, cwd)
if err != nil {
log.Fatal(err)
}
if hasChanges(deps) {
fmt.Println(v)
output[k] = struct{}{}
}
}
// finally to make it all pretty, sort it in a slice
prettyOutput := make([]string, 0, len(output))
for k := range output {
prettyOutput = append(prettyOutput, k)
}
if len(prettyOutput) == 0 {
return
}
sort.Strings(prettyOutput)
fmt.Println(strings.Join(prettyOutput, "\n"))
}

// checks to see if any of the deps have the same suffix as anything in the changedDirs
Expand All @@ -98,7 +111,7 @@ func hasChanges(deps []string) bool {
func readPackages() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
packages = append(packages, scanner.Text())
packages[scanner.Text()] = struct{}{}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
Expand Down
12 changes: 5 additions & 7 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type context struct {
soFar map[string]bool
soFar map[string]struct{}
ctx build.Context
}

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

if name != "." {
c.soFar[pkg.ImportPath] = true
c.soFar[pkg.ImportPath] = struct{}{}
}
imports := pkg.Imports
for _, imp := range imports {
if !c.soFar[imp] {
if _, ok := c.soFar[imp]; !ok {
if err := c.find(imp, pkg.Dir); err != nil {
return err
}
Expand All @@ -45,17 +45,15 @@ func findDeps(name, dir string) ([]string, error) {
ctx := build.Default

c := &context{
soFar: make(map[string]bool),
soFar: make(map[string]struct{}),
ctx: ctx,
}
if err := c.find(name, dir); err != nil {
return nil, err
}
deps := make([]string, 0, len(c.soFar))
for p := range c.soFar {
if p != name {
deps = append(deps, p)
}
deps = append(deps, p)
}
sort.Strings(deps)
return deps, nil
Expand Down

0 comments on commit 5e51c89

Please sign in to comment.