git: ea589ebd6615 - main - framework: add hook to detect whether new ports are hooked into the tree

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Wed, 13 Apr 2022 03:29:58 UTC
The branch main has been updated by tcberner:

URL: https://cgit.FreeBSD.org/ports/commit/?id=ea589ebd66157b2116e5c547d2af8ed94d10d715

commit ea589ebd66157b2116e5c547d2af8ed94d10d715
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2022-01-24 19:53:11 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2022-04-13 03:29:33 +0000

    framework: add hook to detect whether new ports are hooked into the tree
    
    This pre-commit hook looks for newly added files matching
            foo/bar/Makefile
    
    For these files, it is checking that a SUBDIR+=bar entry is
    present in foo/Makefile.
    
    Example run output:
            > mkdir -p graphics/test_port
            > touch graphics/test_port/Makefile
            > git add graphics/test_port/Makefile
            > git commit -m "graphics/test_port: not hooked into the build"
            [pre-commit] ERROR: Missing 'SUBDIR += test_port' in graphics/Makefile
    
    This should hopefully help reduce the number of index breakages.
    
    Differential Revision: https://reviews.freebsd.org/D34015
---
 .hooks/pre-commit | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/.hooks/pre-commit b/.hooks/pre-commit
new file mode 100755
index 000000000000..fb43d4353256
--- /dev/null
+++ b/.hooks/pre-commit
@@ -0,0 +1,17 @@
+#!/bin/sh
+#
+# Check that ports are hooked into the build
+#
+
+newish_makefiles=$(git diff --name-only --cached --diff-filter=ACR | grep -E '^[^/]+/[^/]+/Makefile$')
+if [ $? -eq 0 ] ; then
+	for newish_makefile in ${newish_makefiles} ; do
+		category=$(echo "${newish_makefile}" | awk -F '/' '{print $1}')
+		port=$(echo "${newish_makefile}" | awk -F '/' '{print $2}')
+		grep -q -E "^[[:space:]]+SUBDIR[[:space:]]\+=[[:space:]]*${port}\$" ${category}/Makefile
+		if [ $? -ne 0 ] ; then
+			echo "[pre-commit] ERROR: Missing 'SUBDIR += ${port}' in ${category}/Makefile"
+			exit 1
+		fi
+	done
+fi