Skip to content

Commit 054c2a2

Browse files
icholyhairyhenderson
authored andcommitted
feat: add PatternIndex method
1 parent 95db36b commit 054c2a2

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

codeowners.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,27 @@ func NewCodeowner(pattern string, owners []string) (Codeowner, error) {
218218
// Owners - return the list of code owners for the given path
219219
// (within the repo root)
220220
func (c *Codeowners) Owners(path string) []string {
221+
if i := c.PatternIndex(path); i >= 0 {
222+
return c.Patterns[i].Owners
223+
}
224+
return nil
225+
}
226+
227+
// PatternIndex - return the index of the pattern that matches the given path
228+
// (within the repo root)
229+
func (c *Codeowners) PatternIndex(path string) int {
221230
if strings.HasPrefix(path, c.repoRoot) {
222231
path = strings.Replace(path, c.repoRoot, "", 1)
223232
}
224233

225234
// Order is important; the last matching pattern takes the most precedence.
226235
for i := len(c.Patterns) - 1; i >= 0; i-- {
227-
p := c.Patterns[i]
228-
229-
if p.re.MatchString(path) {
230-
return p.Owners
236+
if c.Patterns[i].re.MatchString(path) {
237+
return i
231238
}
232239
}
233240

234-
return nil
241+
return -1
235242
}
236243

237244
// precompile all regular expressions

0 commit comments

Comments
 (0)