Skip to content

Commit

Permalink
Merge pull request #12 from hmarr/performance-improvements
Browse files Browse the repository at this point in the history
Faster multi-file matching
  • Loading branch information
Harry Marr authored Oct 27, 2022
2 parents 75f94cf + a8d3c95 commit e554638
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
43 changes: 22 additions & 21 deletions cmd/codeowners/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/hmarr/codeowners"
"github.com/karrick/godirwalk"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -50,29 +51,29 @@ func main() {
ownerFilters[i] = strings.TrimLeft(ownerFilters[i], "@")
}

out := bufio.NewWriter(os.Stdout)
defer out.Flush()

for _, startPath := range paths {
// godirwalk only accepts directories, so we need to handle files separately
if !isDir(startPath) {
if err := printFileOwners(ruleset, startPath, ownerFilters, showUnowned); err != nil {
if err := printFileOwners(out, ruleset, startPath, ownerFilters, showUnowned); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
continue
}

err = godirwalk.Walk(startPath, &godirwalk.Options{
Callback: func(path string, dirent *godirwalk.Dirent) error {
if path == ".git" {
return filepath.SkipDir
}

// Only show code owners for files, not directories
if !dirent.IsDir() {
return printFileOwners(ruleset, path, ownerFilters, showUnowned)
}
return nil
},
Unsorted: true,
err = filepath.WalkDir(startPath, func(path string, d os.DirEntry, err error) error {
if path == ".git" {
return filepath.SkipDir
}

// Only show code owners for files, not directories
if !d.IsDir() {
return printFileOwners(out, ruleset, path, ownerFilters, showUnowned)
}
return nil
})

if err != nil {
Expand All @@ -82,7 +83,7 @@ func main() {
}
}

func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []string, showUnowned bool) error {
func printFileOwners(out io.Writer, ruleset codeowners.Ruleset, path string, ownerFilters []string, showUnowned bool) error {
rule, err := ruleset.Match(path)
if err != nil {
return err
Expand All @@ -91,13 +92,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
if rule == nil || rule.Owners == nil {
// Unless explicitly requested, don't show unowned files if we're filtering by owner
if len(ownerFilters) == 0 || showUnowned {
fmt.Printf("%-70s (unowned)\n", path)
fmt.Fprintf(out, "%-70s (unowned)\n", path)
}
return nil
}

// Figure out which of the owners we need to show according to the --owner filters
owners := []string{}
ownersToShow := make([]string, 0, len(rule.Owners))
for _, o := range rule.Owners {
// If there are no filters, show all owners
filterMatch := len(ownerFilters) == 0 && !showUnowned
Expand All @@ -107,13 +108,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
}
}
if filterMatch {
owners = append(owners, o.String())
ownersToShow = append(ownersToShow, o.String())
}
}

// If the owners slice is empty, no owners matched the filters so don't show anything
if len(owners) > 0 {
fmt.Printf("%-70s %s\n", path, strings.Join(owners, " "))
if len(ownersToShow) > 0 {
fmt.Fprintf(out, "%-70s %s\n", path, strings.Join(ownersToShow, " "))
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ type Ruleset []Rule
// last matching rule takes precedence.
func (r Ruleset) Match(path string) (*Rule, error) {
for i := len(r) - 1; i >= 0; i-- {
rule := r[i]
rule := &r[i]
match, err := rule.Match(path)
if match || err != nil {
return &rule, err
return rule, err
}
}
return nil, nil
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/hmarr/codeowners
go 1.14

require (
github.com/karrick/godirwalk v1.17.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down

0 comments on commit e554638

Please sign in to comment.