git: 537c7c00047f - main - framework: new hook to only allow 'default' files in category/port

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Tue, 15 Nov 2022 18:21:46 UTC
The branch main has been updated by tcberner:

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

commit 537c7c00047fd4ba18247c3e160529f697f7d1a2
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2022-11-14 18:34:18 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2022-11-15 18:20:27 +0000

    framework: new hook to only allow 'default' files in category/port
    
    This hooks enforces that all files in the top-directory of a port match:
      - .*\.mk
      - Makefile.*
      - distinfo.*
      - pkg-.*
    
    An example error message would look like:
    
     [pre-commit] ERROR: invalid file 'Foo' in 'kate/editors'
                 Consider moving non-standard files to files/ or force-ignore this hook.
    
    Differential Revision: https://reviews.freebsd.org/D37387
---
 .hooks/pre-commit.d/check_files | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/.hooks/pre-commit.d/check_files b/.hooks/pre-commit.d/check_files
new file mode 100755
index 000000000000..01eae4f2b3d8
--- /dev/null
+++ b/.hooks/pre-commit.d/check_files
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Check that only standard files are added to a port
+#
+#    .*\.mk
+#    Makefile.*
+#    distinfo.*
+#    pkg-.*
+#
+
+category_regex="($(make -VSUBDIR | sed 's# #\|#g'))"
+newish_files=$(git diff --name-only --cached --diff-filter=ACR | grep -E "^${category_regex}/[^/]+/[^/]+$")
+
+status=0
+if [ $? -eq 0 ] ; then
+	for newish_file in ${newish_files} ; do
+		category=$(echo "${newish_file}" | awk -F '/' '{print $1}')
+		port=$(echo "${newish_file}" | awk -F '/' '{print $2}')
+		file=$(echo "${newish_file}" | awk -F '/' '{print $3}')
+		valid=$(echo "${file}" | grep -q '^((Makefile|distinfo|pkg-)(.*))|(.*\.mk)$')
+		if [ $? -ne 0 ] ; then
+			echo "[pre-commit] ERROR: invalid file '${file}' in '${category}/${port}'"
+			status=1
+		fi
+	done
+fi
+if [ ${status} -eq 1 ] ; then
+	echo "             Consider moving non-standard files to files/ or force-ignore this hook."
+	exit 1
+fi