git: 4e21a051bb0a - main - framework: create a sub-directory to support multiple pre-commit hooks

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Sun, 24 Apr 2022 10:25:28 UTC
The branch main has been updated by tcberner:

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

commit 4e21a051bb0aa1e6ee77a30f5803d5ad6c49d578
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2022-04-24 10:11:13 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2022-04-24 10:24:28 +0000

    framework: create a sub-directory to support multiple pre-commit hooks
---
 .hooks/pre-commit                           | 18 ++++++++----------
 .hooks/pre-commit.d/check_category_makefile | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/.hooks/pre-commit b/.hooks/pre-commit
index fb43d4353256..886d97d7cc1b 100755
--- a/.hooks/pre-commit
+++ b/.hooks/pre-commit
@@ -1,17 +1,15 @@
 #!/bin/sh
 #
-# Check that ports are hooked into the build
+# Runs every hook in pre-commit.d/ until the first failure
 #
 
-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
+hook_directory=$(realpath $(dirname $0))
+
+for hook in ${hook_directory}/pre-commit.d/* ; do
+	if [ -x "${hook}" ] ; then
+		${hook}
 		if [ $? -ne 0 ] ; then
-			echo "[pre-commit] ERROR: Missing 'SUBDIR += ${port}' in ${category}/Makefile"
 			exit 1
 		fi
-	done
-fi
+	fi
+done
diff --git a/.hooks/pre-commit.d/check_category_makefile b/.hooks/pre-commit.d/check_category_makefile
new file mode 100755
index 000000000000..fb43d4353256
--- /dev/null
+++ b/.hooks/pre-commit.d/check_category_makefile
@@ -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