Skip to content

Commit 09554d3

Browse files
committed
Make PrefixFilter a little more useful (and add a docstring)
1 parent 65f247d commit 09554d3

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

git/ref_filter.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,25 @@ func AllReferencesFilter(_ Reference) bool {
1010
return true
1111
}
1212

13+
// PrefixFilter returns a `ReferenceFilter` that matches references
14+
// whose names start with the specified `prefix`, which must match at
15+
// a component boundary. For example,
16+
//
17+
// * Prefix "refs/foo" matches "refs/foo" and "refs/foo/bar" but not
18+
// "refs/foobar".
19+
//
20+
// * Prefix "refs/foo/" matches "refs/foo/bar" but not "refs/foo" or
21+
// "refs/foobar".
1322
func PrefixFilter(prefix string) ReferenceFilter {
23+
if strings.HasSuffix(prefix, "/") {
24+
return func(r Reference) bool {
25+
return strings.HasPrefix(r.Refname, prefix)
26+
}
27+
}
28+
1429
return func(r Reference) bool {
15-
return strings.HasPrefix(r.Refname, prefix)
30+
return strings.HasPrefix(r.Refname, prefix) &&
31+
(len(r.Refname) == len(prefix) || r.Refname[len(prefix)] == '/')
1632
}
1733
}
1834

0 commit comments

Comments
 (0)