git: 302c208fc513 - main - Component: git hooks

From: Luca Pizzamiglio <pizzamig_at_FreeBSD.org>
Date: Thu, 12 Jan 2023 21:11:17 UTC
The branch main has been updated by pizzamig:

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

commit 302c208fc51342e75b089d56da612d8c740a7ac1
Author:     Luca Pizzamiglio <pizzamig@FreeBSD.org>
AuthorDate: 2023-01-12 21:09:50 +0000
Commit:     Luca Pizzamiglio <pizzamig@FreeBSD.org>
CommitDate: 2023-01-12 21:09:50 +0000

    Component: git hooks
    
    Add common error function in hooks
    Error messages are printed in stderr
    
    Reviewed By: tcberner, #portmgr
    Differential Revision: https://reviews.freebsd.org/D38026
---
 .hooks/pre-commit.d/check_category_makefile |  7 ++++++-
 .hooks/pre-commit.d/check_created_by        | 13 +++++++++----
 .hooks/pre-commit.d/check_files             |  9 +++++++--
 .hooks/pre-commit.d/check_mk_indentations   |  9 +++++++--
 .hooks/pre-commit.d/check_moved             | 10 +++++++---
 .hooks/pre-commit.d/check_portepoch         |  9 +++++++--
 .hooks/pre-commit.d/common.sh               |  7 +++++++
 7 files changed, 50 insertions(+), 14 deletions(-)

diff --git a/.hooks/pre-commit.d/check_category_makefile b/.hooks/pre-commit.d/check_category_makefile
index fb43d4353256..fea09ef6325c 100755
--- a/.hooks/pre-commit.d/check_category_makefile
+++ b/.hooks/pre-commit.d/check_category_makefile
@@ -3,6 +3,11 @@
 # Check that ports are hooked into the build
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
+
 newish_makefiles=$(git diff --name-only --cached --diff-filter=ACR | grep -E '^[^/]+/[^/]+/Makefile$')
 if [ $? -eq 0 ] ; then
 	for newish_makefile in ${newish_makefiles} ; do
@@ -10,7 +15,7 @@ if [ $? -eq 0 ] ; then
 		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"
+			pre_commit_error "Missing 'SUBDIR += ${port}' in ${category}/Makefile"
 			exit 1
 		fi
 	done
diff --git a/.hooks/pre-commit.d/check_created_by b/.hooks/pre-commit.d/check_created_by
index 6e4d8d8bc3cc..4d22b3fdd607 100755
--- a/.hooks/pre-commit.d/check_created_by
+++ b/.hooks/pre-commit.d/check_created_by
@@ -3,15 +3,20 @@
 # Check that ports do not contain a 'Created by' tag
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
+
 makefiles=$(git diff --name-only --cached --diff-filter=ACMR | grep -E '^[^/]+/[^/]+/Makefile$')
 if [ $? -eq 0 ] ; then
 	for makefile in ${makefiles} ; do
 		created_by=$(head -n1 ${makefile} | awk -F : '/Created by/{print $NF}')
 		if [ -n "${created_by}" ] ; then
-			echo -e "[pre-commit] ERROR: ${makefile} contains obsolete 'Created by' line\n" \
-			        "                    Please remove the the line, and append:\n\n" \
-			        "                    - Removed 'Created by ${created_by}'.\n\n" \
-			        "                    to your commit message."
+			pre_commit_error "${makefile} contains obsolete 'Created by' line\n" \
+			        "                   Please remove the the line, and append:\n\n" \
+			        "                   - Removed 'Created by ${created_by}'.\n\n" \
+			        "                   to your commit message."
 			exit 1
 		fi
 	done
diff --git a/.hooks/pre-commit.d/check_files b/.hooks/pre-commit.d/check_files
index e9891f3cc827..300c10bfc6b4 100755
--- a/.hooks/pre-commit.d/check_files
+++ b/.hooks/pre-commit.d/check_files
@@ -8,6 +8,11 @@
 #    pkg-.*
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
+
 category_regex="($(make -VSUBDIR | sed 's# #\|#g'))"
 newish_files=$(git diff --name-only --cached --diff-filter=ACR | grep -E "^${category_regex}/[^/]+/[^/]+$")
 
@@ -19,12 +24,12 @@ if [ $? -eq 0 ] ; then
 		file=$(echo "${newish_file}" | awk -F '/' '{print $3}')
 		valid=$(echo "${file}" | grep -Eq '^((Makefile|distinfo|pkg-)(.*))|(.*\.mk)$')
 		if [ $? -ne 0 ] ; then
-			echo "[pre-commit] ERROR: invalid file '${file}' in '${category}/${port}'"
+			pre_commit_error "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."
+	error "             Consider moving non-standard files to files/ or force-ignore this hook."
 	exit 1
 fi
diff --git a/.hooks/pre-commit.d/check_mk_indentations b/.hooks/pre-commit.d/check_mk_indentations
index 00fdb16f24cf..d5f6e601554a 100755
--- a/.hooks/pre-commit.d/check_mk_indentations
+++ b/.hooks/pre-commit.d/check_mk_indentations
@@ -3,6 +3,11 @@
 # Check that Mk/ files are properly indented
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
+
 check_indentation() {
 	local mkfile="$1"
 	local name=$(echo "${mkfile}" | awk -F / '{print $NF}')
@@ -13,12 +18,12 @@ check_indentation() {
 	fi
 	cp ${mkfile} ${tempdir} && Tools/scripts/indent_make_if.pl ${tempdir}/${name}
 	if [ $? -ne 0 ] ; then
-		echo "[pre-commit] failed to run Tools/scripts/indent_make_if.pl"
+		pre_commit_error "failed to run Tools/scripts/indent_make_if.pl"
 		exit 2
 	fi
 	cmp -s ${tempdir}/*
 	if [ $? -ne 0 ] ; then
-		echo "[pre-commit] ${name} is not properly indented -- please use ${tempdir}/${name} which was created using Tools/scripts/indent_make_if.pl"
+		pre_commit_error "${name} is not properly indented -- please use ${tempdir}/${name} which was created using Tools/scripts/indent_make_if.pl"
 		exit 1
 	fi
 }
diff --git a/.hooks/pre-commit.d/check_moved b/.hooks/pre-commit.d/check_moved
index 3d6a3209aef0..5a0a507ab061 100755
--- a/.hooks/pre-commit.d/check_moved
+++ b/.hooks/pre-commit.d/check_moved
@@ -3,6 +3,10 @@
 # Check that newly added MOVED lines are valid
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
 
 moved_changed=$(git diff --name-only --cached --diff-filter=M | grep -E '^MOVED$')
 if [ $? -eq 0 ] ; then
@@ -13,9 +17,9 @@ if [ $? -eq 0 ] ; then
 
 	errors=$(PORTSDIR=${tree} Tools/scripts/MOVEDlint.awk -v lastdate="${lastdate}")
 	if [ $? -ne 0 ] ; then
-		echo -e "[pre-commit] ERROR: MOVED contains errors.\n" \
-			"                   Please apply the suggested changes:\n"
-		echo "${errors}"
+		pre_commit_error "MOVED contains errors.\n" \
+			"                  Please apply the suggested changes:\n"
+		error "${errors}"
 		exit 1
 	fi
 fi
diff --git a/.hooks/pre-commit.d/check_portepoch b/.hooks/pre-commit.d/check_portepoch
index 34d93e2efcf4..1e93cd595708 100755
--- a/.hooks/pre-commit.d/check_portepoch
+++ b/.hooks/pre-commit.d/check_portepoch
@@ -3,17 +3,22 @@
 # Check that PORTEPOCH is not being dropped, and is non-decreasing
 #
 
+common_functions="$(realpath "$(dirname "$0")")/common.sh"
+if [ -r "${common_functions}" ]; then
+	. "${common_functions}"
+fi
+
 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}" -a -n "${old_epoch}" ] ; then
-		echo "[pre-commit] dropped PORTEPOCH ${old_epoch} in ${makefile}"
+		pre_commit_error "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}"
+			pre_commit_error "PORTEPOCH decreasing from ${old_epoch} to ${new_epoch} in ${makefile}"
 			exit 2
 		fi
 	fi
diff --git a/.hooks/pre-commit.d/common.sh b/.hooks/pre-commit.d/common.sh
new file mode 100644
index 000000000000..6b4a82fb62dc
--- /dev/null
+++ b/.hooks/pre-commit.d/common.sh
@@ -0,0 +1,7 @@
+error() {
+	echo -e "$*" > /dev/stderr
+}
+
+pre_commit_error() {
+	error "[pre-commit] ERROR: $*"
+}