maintainers.go: correct handling of globs

maintainers.go does not handle globs as described in MAINTAINERS.
Instead of only matching the files inside a directory, it also matches
everything below. Also, a glob used in between (`e.g. path/to/*/dir`)
could lead to matching many more paths unexpectedly.

This is caused by the way paths using globs are converted to regegular
expressions for use with gerrit:

1. The script converts all paths with trailing slash to a path with
   trailing glob. That means, a recursive match on a directory gets
   converted to match only the files in the directory (at least
   according to the documentation - if there wasn't 2).

   Example: `path/to/dir/` becomes `path/to/dir/*`

2. When converting the path to a regex, all globs get converted to
   prefix matching by replacing the glob by `.*`. Instead of only
   matching the files in the directory, everything below matches,
   which is a) not what the documentation states and b) the opposite
   of what 1. did first.

   Example: `path/to/dir/*` becomes `^path/to/dir/.*$`

In sum, this leads to all sorts of issues. Examples:
  - `path/*/dir`    becomes `^path/.*/dir$`
  - `path/to/dir/*` becomes `^path/to/dir/.*$`
  - `path/to/*.c`   becomes `^path/to/.*\.c$`

This change fixes that behaviour by:
- dropping the wrong conversion from 1. above.
- fixing glob matching by replacing `*` by `[^/]`.
- handling paths with trailing `/` as prefix, as documented.

The change was not split because these changes depend on each other and
splitting would break recursive matching between the commits.

Tests:
1. diffed output before and after is equal (!= the same)
2. manual testing of glob matching

Change-Id: I4347a60874e4f07e41bdee43cc312547bea99008
Signed-off-by: Michael Niewöhner <foss@mniewoehner.de>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/52275
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
Michael Niewöhner 2021-04-13 00:17:39 +02:00 committed by Patrick Georgi
parent 20e04efc33
commit 158fed9ee7
1 changed files with 8 additions and 5 deletions

View File

@ -77,11 +77,14 @@ func get_maintainers() ([]string, error) {
}
func path_to_regexstr(path string) string {
// if prefix, allow all subdirectories
if path[len(path)-1] == '/' {
path += "*"
regexstr := glob_to_regex(path)
/* Handle path with trailing '/' as prefix */
if regexstr[len(regexstr)-2:] == "/$" {
regexstr = regexstr[:len(regexstr)-1] + ".*$"
}
return glob_to_regex(path)
return regexstr;
}
func path_to_regex(path string) *regexp.Regexp {
@ -200,7 +203,7 @@ func glob_to_regex(glob string) string {
}
case '*':
if inClass == 0 {
regex += ".*"
regex += "[^/]*"
} else {
regex += "*"
}