Skip to content

Commit

Permalink
feat: add PatternIndex method (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy authored Dec 7, 2024
1 parent 95db36b commit a5c2e59
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,27 @@ func NewCodeowner(pattern string, owners []string) (Codeowner, error) {
// Owners - return the list of code owners for the given path
// (within the repo root)
func (c *Codeowners) Owners(path string) []string {
if i := c.PatternIndex(path); i >= 0 {
return c.Patterns[i].Owners
}
return nil
}

// PatternIndex - return the index of the pattern that matches the given path
// (within the repo root)
func (c *Codeowners) PatternIndex(path string) int {
if strings.HasPrefix(path, c.repoRoot) {
path = strings.Replace(path, c.repoRoot, "", 1)
}

// Order is important; the last matching pattern takes the most precedence.
for i := len(c.Patterns) - 1; i >= 0; i-- {
p := c.Patterns[i]

if p.re.MatchString(path) {
return p.Owners
if c.Patterns[i].re.MatchString(path) {
return i
}
}

return nil
return -1
}

// precompile all regular expressions
Expand Down

1 comment on commit a5c2e59

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: a5c2e59 Previous: 95db36b Ratio
BenchmarkParseCodeowners 28283 ns/op 26667 B/op 194 allocs/op 28587 ns/op 26667 B/op 194 allocs/op 0.99
BenchmarkParseCodeowners - ns/op 28283 ns/op 28587 ns/op 0.99
BenchmarkParseCodeowners - B/op 26667 B/op 26667 B/op 1
BenchmarkParseCodeowners - allocs/op 194 allocs/op 194 allocs/op 1
BenchmarkParseCodeowners 28472 ns/op 26666 B/op 194 allocs/op 28587 ns/op 26667 B/op 194 allocs/op 1.00
BenchmarkParseCodeowners - ns/op 28472 ns/op 28587 ns/op 1.00
BenchmarkParseCodeowners - B/op 26666 B/op 26667 B/op 1.00
BenchmarkParseCodeowners - allocs/op 194 allocs/op 194 allocs/op 1
BenchmarkParseCodeowners 28344 ns/op 26674 B/op 194 allocs/op 28587 ns/op 26667 B/op 194 allocs/op 0.99
BenchmarkParseCodeowners - ns/op 28344 ns/op 28587 ns/op 0.99
BenchmarkParseCodeowners - B/op 26674 B/op 26667 B/op 1.00
BenchmarkParseCodeowners - allocs/op 194 allocs/op 194 allocs/op 1
BenchmarkParseCodeowners 28594 ns/op 26662 B/op 194 allocs/op 28587 ns/op 26667 B/op 194 allocs/op 1.00
BenchmarkParseCodeowners - ns/op 28594 ns/op 28587 ns/op 1.00
BenchmarkParseCodeowners - B/op 26662 B/op 26667 B/op 1.00
BenchmarkParseCodeowners - allocs/op 194 allocs/op 194 allocs/op 1
BenchmarkParseCodeowners 28507 ns/op 26672 B/op 194 allocs/op 28587 ns/op 26667 B/op 194 allocs/op 1.00
BenchmarkParseCodeowners - ns/op 28507 ns/op 28587 ns/op 1.00
BenchmarkParseCodeowners - B/op 26672 B/op 26667 B/op 1.00
BenchmarkParseCodeowners - allocs/op 194 allocs/op 194 allocs/op 1
BenchmarkOwners/#foo/bar.go 347.5 ns/op 0 B/op 0 allocs/op 348.9 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/#foo/bar.go - ns/op 347.5 ns/op 348.9 ns/op 1.00
BenchmarkOwners/#foo/bar.go - B/op 0 B/op 0 B/op 1
BenchmarkOwners/#foo/bar.go - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/#foo/bar.go 346.8 ns/op 0 B/op 0 allocs/op 348.9 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/#foo/bar.go - ns/op 346.8 ns/op 348.9 ns/op 0.99
BenchmarkOwners/#foo/bar.go - B/op 0 B/op 0 B/op 1
BenchmarkOwners/#foo/bar.go - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/#foo/bar.go 346.4 ns/op 0 B/op 0 allocs/op 348.9 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/#foo/bar.go - ns/op 346.4 ns/op 348.9 ns/op 0.99
BenchmarkOwners/#foo/bar.go - B/op 0 B/op 0 B/op 1
BenchmarkOwners/#foo/bar.go - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/#foo/bar.go 345.4 ns/op 0 B/op 0 allocs/op 348.9 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/#foo/bar.go - ns/op 345.4 ns/op 348.9 ns/op 0.99
BenchmarkOwners/#foo/bar.go - B/op 0 B/op 0 B/op 1
BenchmarkOwners/#foo/bar.go - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/#foo/bar.go 346.1 ns/op 0 B/op 0 allocs/op 348.9 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/#foo/bar.go - ns/op 346.1 ns/op 348.9 ns/op 0.99
BenchmarkOwners/#foo/bar.go - B/op 0 B/op 0 B/op 1
BenchmarkOwners/#foo/bar.go - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/blah/docs/README.md 2592 ns/op 0 B/op 0 allocs/op 2598 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/blah/docs/README.md - ns/op 2592 ns/op 2598 ns/op 1.00
BenchmarkOwners/blah/docs/README.md - B/op 0 B/op 0 B/op 1
BenchmarkOwners/blah/docs/README.md - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/blah/docs/README.md 2588 ns/op 0 B/op 0 allocs/op 2598 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/blah/docs/README.md - ns/op 2588 ns/op 2598 ns/op 1.00
BenchmarkOwners/blah/docs/README.md - B/op 0 B/op 0 B/op 1
BenchmarkOwners/blah/docs/README.md - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/blah/docs/README.md 2542 ns/op 0 B/op 0 allocs/op 2598 ns/op 0 B/op 0 allocs/op 0.98
BenchmarkOwners/blah/docs/README.md - ns/op 2542 ns/op 2598 ns/op 0.98
BenchmarkOwners/blah/docs/README.md - B/op 0 B/op 0 B/op 1
BenchmarkOwners/blah/docs/README.md - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/blah/docs/README.md 2631 ns/op 0 B/op 0 allocs/op 2598 ns/op 0 B/op 0 allocs/op 1.01
BenchmarkOwners/blah/docs/README.md - ns/op 2631 ns/op 2598 ns/op 1.01
BenchmarkOwners/blah/docs/README.md - B/op 0 B/op 0 B/op 1
BenchmarkOwners/blah/docs/README.md - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/blah/docs/README.md 2589 ns/op 0 B/op 0 allocs/op 2598 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/blah/docs/README.md - ns/op 2589 ns/op 2598 ns/op 1.00
BenchmarkOwners/blah/docs/README.md - B/op 0 B/op 0 B/op 1
BenchmarkOwners/blah/docs/README.md - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js 5136 ns/op 0 B/op 0 allocs/op 5185 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - ns/op 5136 ns/op 5185 ns/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - B/op 0 B/op 0 B/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js 5158 ns/op 0 B/op 0 allocs/op 5185 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - ns/op 5158 ns/op 5185 ns/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - B/op 0 B/op 0 B/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js 5158 ns/op 0 B/op 0 allocs/op 5185 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - ns/op 5158 ns/op 5185 ns/op 0.99
BenchmarkOwners/foo/bar/docs/foo/foo.js - B/op 0 B/op 0 B/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js 5186 ns/op 0 B/op 0 allocs/op 5185 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/foo/bar/docs/foo/foo.js - ns/op 5186 ns/op 5185 ns/op 1.00
BenchmarkOwners/foo/bar/docs/foo/foo.js - B/op 0 B/op 0 B/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js 5177 ns/op 0 B/op 0 allocs/op 5185 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners/foo/bar/docs/foo/foo.js - ns/op 5177 ns/op 5185 ns/op 1.00
BenchmarkOwners/foo/bar/docs/foo/foo.js - B/op 0 B/op 0 B/op 1
BenchmarkOwners/foo/bar/docs/foo/foo.js - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//space/test_space/doc1.txt 914.3 ns/op 0 B/op 0 allocs/op 917.6 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners//space/test_space/doc1.txt - ns/op 914.3 ns/op 917.6 ns/op 1.00
BenchmarkOwners//space/test_space/doc1.txt - B/op 0 B/op 0 B/op 1
BenchmarkOwners//space/test_space/doc1.txt - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//space/test_space/doc1.txt 909.8 ns/op 0 B/op 0 allocs/op 917.6 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners//space/test_space/doc1.txt - ns/op 909.8 ns/op 917.6 ns/op 0.99
BenchmarkOwners//space/test_space/doc1.txt - B/op 0 B/op 0 B/op 1
BenchmarkOwners//space/test_space/doc1.txt - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//space/test_space/doc1.txt 910.8 ns/op 0 B/op 0 allocs/op 917.6 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners//space/test_space/doc1.txt - ns/op 910.8 ns/op 917.6 ns/op 0.99
BenchmarkOwners//space/test_space/doc1.txt - B/op 0 B/op 0 B/op 1
BenchmarkOwners//space/test_space/doc1.txt - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//space/test_space/doc1.txt 917 ns/op 0 B/op 0 allocs/op 917.6 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners//space/test_space/doc1.txt - ns/op 917 ns/op 917.6 ns/op 1.00
BenchmarkOwners//space/test_space/doc1.txt - B/op 0 B/op 0 B/op 1
BenchmarkOwners//space/test_space/doc1.txt - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//space/test_space/doc1.txt 925 ns/op 0 B/op 0 allocs/op 917.6 ns/op 0 B/op 0 allocs/op 1.01
BenchmarkOwners//space/test_space/doc1.txt - ns/op 925 ns/op 917.6 ns/op 1.01
BenchmarkOwners//space/test_space/doc1.txt - B/op 0 B/op 0 B/op 1
BenchmarkOwners//space/test_space/doc1.txt - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes 274.1 ns/op 0 B/op 0 allocs/op 274.1 ns/op 0 B/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes - ns/op 274.1 ns/op 274.1 ns/op 1
BenchmarkOwners//terraform/kubernetes - B/op 0 B/op 0 B/op 1
BenchmarkOwners//terraform/kubernetes - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes 273.2 ns/op 0 B/op 0 allocs/op 274.1 ns/op 0 B/op 0 allocs/op 1.00
BenchmarkOwners//terraform/kubernetes - ns/op 273.2 ns/op 274.1 ns/op 1.00
BenchmarkOwners//terraform/kubernetes - B/op 0 B/op 0 B/op 1
BenchmarkOwners//terraform/kubernetes - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes 271.1 ns/op 0 B/op 0 allocs/op 274.1 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners//terraform/kubernetes - ns/op 271.1 ns/op 274.1 ns/op 0.99
BenchmarkOwners//terraform/kubernetes - B/op 0 B/op 0 B/op 1
BenchmarkOwners//terraform/kubernetes - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes 272.6 ns/op 0 B/op 0 allocs/op 274.1 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners//terraform/kubernetes - ns/op 272.6 ns/op 274.1 ns/op 0.99
BenchmarkOwners//terraform/kubernetes - B/op 0 B/op 0 B/op 1
BenchmarkOwners//terraform/kubernetes - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkOwners//terraform/kubernetes 271.4 ns/op 0 B/op 0 allocs/op 274.1 ns/op 0 B/op 0 allocs/op 0.99
BenchmarkOwners//terraform/kubernetes - ns/op 271.4 ns/op 274.1 ns/op 0.99
BenchmarkOwners//terraform/kubernetes - B/op 0 B/op 0 B/op 1
BenchmarkOwners//terraform/kubernetes - allocs/op 0 allocs/op 0 allocs/op 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.