[PATCH 2/3] scripts: Check F: entries in all MAINTAINERS files

Michal Simek <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <6a07d4bb5c0da18c885a05b24f29d2df69489853.1787555916.git.michal.simek@amd.com>
Extend the checker to walk every MAINTAINERS file in the tree instead of
only the top-level one, so stale F: references in the per-board and
per-driver MAINTAINERS files are caught too.

Use git ls-files to enumerate the MAINTAINERS files so that only tracked
files are checked, ignored build artifacts are skipped and paths are
reported relative to the repository root. Fall back to find when running
outside a git checkout.

MAINTAINERS files under tools/buildman/ are skipped as they are test
fixtures whose F: paths are relative to the test setup rather than the
repository root.

Signed-off-by: Michal Simek <[email protected]>
---

 scripts/check_maintainers_files.sh | 80 ++++++++++++++++++++----------
 1 file changed, 53 insertions(+), 27 deletions(-)

diff --git a/scripts/check_maintainers_files.sh b/scripts/check_maintainers_files.sh
index 4c1279958e98..ccac97a2d93e 100755
--- a/scripts/check_maintainers_files.sh
+++ b/scripts/check_maintainers_files.sh
@@ -1,44 +1,70 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0
 #
-# Check that every "F:" file reference in MAINTAINERS exists.
+# Check that every "F:" file reference in the MAINTAINERS files exists.
 #
-# Each F: entry is treated as a shell glob (relative to the repo root),
-# matching the way MAINTAINERS wildcards work. If a pattern matches no
-# existing file or directory, it is reported and the script exits non-zero.
+# Every MAINTAINERS file in the tree is checked (the top-level one plus the
+# per-board/per-driver MAINTAINERS files). All F: paths are interpreted
+# relative to the repository root and are treated as shell globs, matching the
+# way MAINTAINERS wildcards work. If a pattern matches no existing file or
+# directory it is reported and the script exits non-zero.
 
 set -u
 
-MAINTAINERS="${1:-MAINTAINERS}"
+# Move to the repository root so that F: globs (which are root relative)
+# resolve correctly regardless of where the script is invoked from.
+if root=$(git rev-parse --show-toplevel 2>/dev/null); then
+	cd "$root" || exit 2
+fi
 
-if [ ! -f "$MAINTAINERS" ]; then
-	echo "error: cannot find $MAINTAINERS" >&2
-	exit 2
+# Collect the list of MAINTAINERS files to check. Newlines separate entries;
+# MAINTAINERS paths never contain spaces.
+set -f
+IFS='
+'
+if list=$(git ls-files '*MAINTAINERS' 'MAINTAINERS' 2>/dev/null) && [ -n "$list" ]; then
+	set -- $list
+else
+	# Fall back to a filesystem search if git is unavailable.
+	set -- $(find . -name MAINTAINERS -type f | sed 's,^\./,,')
 fi
+unset IFS
+set +f
 
 rc=0
 
-while IFS= read -r pattern; do
-	# Strip the "F:" prefix and surrounding whitespace.
-	pattern=$(printf '%s\n' "$pattern" | sed -e 's/^F:[[:space:]]*//' -e 's/[[:space:]]*$//')
-	[ -n "$pattern" ] || continue
-
-	# Expand the pattern as a glob; if nothing matches the glob stays literal.
-	matched=0
-	for path in $pattern; do
-		if [ -e "$path" ]; then
-			matched=1
-			break
+for maint in "$@"; do
+	[ -f "$maint" ] || continue
+
+	# Skip Buildman's test fixtures, whose F: paths are relative to the
+	# test setup rather than the repository root.
+	case "$maint" in
+	tools/buildman/*) continue ;;
+	esac
+
+	while IFS= read -r pattern; do
+		# Strip the "F:" prefix and surrounding whitespace, then drop the
+		# backslashes MAINTAINERS uses to escape regex-special characters
+		# (e.g. "board/k\+p/") so the pattern can be used as a plain glob.
+		pattern=$(printf '%s\n' "$pattern" | sed -e 's/^F:[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/\\\(.\)/\1/g')
+		[ -n "$pattern" ] || continue
+
+		matched=0
+		for path in $pattern; do
+			if [ -e "$path" ]; then
+				matched=1
+				break
+			fi
+		done
+
+		if [ "$matched" -eq 0 ]; then
+			echo "error: $maint references non-existing file: $pattern" >&2
+			rc=1
 		fi
-	done
-
-	if [ "$matched" -eq 0 ]; then
-		echo "error: MAINTAINERS references non-existing file: $pattern" >&2
-		rc=1
-	fi
-done <<EOF
-$(grep '^F:' "$MAINTAINERS")
+	done <<EOF
+$(grep '^F:' "$maint")
 EOF
+done
 
 if [ "$rc" -eq 0 ]; then
 	echo "All MAINTAINERS F: entries exist."
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.