git: 5ad46769a81f - main - framework: add git-hook to check PORTEPOCH validity

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Fri, 11 Nov 2022 20:40:43 UTC
The branch main has been updated by tcberner:

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

commit 5ad46769a81f77b8fd592a16f3367d359d34ecc4
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2022-11-11 20:37:54 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2022-11-11 20:39:46 +0000

    framework: add git-hook to check  PORTEPOCH validity
    
    - checks dropping of PORTEPOCH:
            [pre-commit] dropped PORTEPOCH 1 in net/kf5-kdav/Makefile
    
    - checks it being non-decreasing:
            [pre-commit] PORTEPOCH decreasing from 2 to 1 in net-p2p/frost/Makefile
    
    Reviewed By: rene, bapt
    Differential Revision: https://reviews.freebsd.org/D35733
---
 .hooks/pre-commit.d/check_portepoch | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/.hooks/pre-commit.d/check_portepoch b/.hooks/pre-commit.d/check_portepoch
new file mode 100755
index 000000000000..1a59a39b5407
--- /dev/null
+++ b/.hooks/pre-commit.d/check_portepoch
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Check that PORTEPOCH is not being dropped, and is non-decreasing
+#
+
+check_epoch() {
+	local makefile="$1"
+	local old_epoch=$(git diff --cached -U0 "${makefile}" | grep '^\-PORTEPOCH' | grep -oE '[0-9]+')
+	local new_epoch=$(git diff --cached -U0 "${makefile}" | grep '^\+PORTEPOCH' | grep -oE '[0-9]+')
+	if [ -z "${new_epoch}" ] ; then
+		echo "[pre-commit] dropped PORTEPOCH ${old_epoch} in ${makefile}"
+		exit 1
+	fi
+	if [ -n "${old_epoch}" ] ; then
+		if [ ${new_epoch} -lt ${old_epoch} ] ; then
+			echo "[pre-commit] PORTEPOCH decreasing from ${old_epoch} to ${new_epoch} in ${makefile}"
+			exit 2
+		fi
+	fi
+}
+
+modified_makefiles=$(git diff --name-only --cached --diff-filter=M -GPORTEPOCH | grep -E '^[^/]+/[^/]+/Makefile$')
+if [ $? -eq 0 ] ; then
+	for modified_makefile in ${modified_makefiles} ; do
+		check_epoch ${modified_makefile}
+	done
+fi