From 158fed9ee72ff09b5f8237c42910b406727caa9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Niew=C3=B6hner?= Date: Tue, 13 Apr 2021 00:17:39 +0200 Subject: maintainers.go: correct handling of globs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-on: https://review.coreboot.org/c/coreboot/+/52275 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi Reviewed-by: Nico Huber --- util/scripts/maintainers.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/util/scripts/maintainers.go b/util/scripts/maintainers.go index 459c030d1360..c68ea058f01d 100644 --- a/util/scripts/maintainers.go +++ b/util/scripts/maintainers.go @@ -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 += "*" } -- cgit v1.2.3