git: 27bebbf28a72 - main - framework: add hook to check indentation of conditionals of Mk/ files

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Mon, 25 Apr 2022 17:35:16 UTC
The branch main has been updated by tcberner:

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

commit 27bebbf28a72ab5286623603611c5c0e39b6a4c8
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2022-04-24 10:54:43 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2022-04-25 17:34:55 +0000

    framework: add hook to check indentation of conditionals of Mk/ files
    
    This adds a further pre-commit hook to enforce indentations in Mk/*
    
    Usage:
            > vim Mk/Uses/tcl.mk
            [add/remove some whitespaces between a `.` and an `if`]
            > git add Mk/Uses/tcl.mk
            > git commit -m "Mk/Uses/tcl.mk: test hook"
            [pre-commit] tcl.mk is not properly indented -- please use /tmp/check_indentations-tcl.mk.LShfR7TD48/tcl.mk which was created using Tools/scripts/indent_make_if.pl
    
    Reminder: to make use of the hooks provided by the ports tree use the
    following from [1]
    
        Add the prepare-commit-msg hook to the repository.
    
        To make use of it, the easiest way is to run:
    
          git config --add core.hooksPath .hooks
    
    [1] bbc2474ef7a65eb8561c8ecf7af80c2bfed1f248
    
    Reviewed by:            portmgr (rene)
    Differential Revision:  https://reviews.freebsd.org/D35041
---
 .hooks/pre-commit.d/check_mk_indentations | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/.hooks/pre-commit.d/check_mk_indentations b/.hooks/pre-commit.d/check_mk_indentations
new file mode 100755
index 000000000000..00fdb16f24cf
--- /dev/null
+++ b/.hooks/pre-commit.d/check_mk_indentations
@@ -0,0 +1,31 @@
+#!/bin/sh
+#
+# Check that Mk/ files are properly indented
+#
+
+check_indentation() {
+	local mkfile="$1"
+	local name=$(echo "${mkfile}" | awk -F / '{print $NF}')
+	local tempdir=$(mktemp -d -t check_indentations-${name})
+	if [ $? -ne 0 ] ; then
+		echo "[pre-commit] failed to create tempdir"
+		exit 2
+	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"
+		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"
+		exit 1
+	fi
+}
+
+modified_mks=$(git diff --name-only --cached --diff-filter=ACRM | grep -E '^Mk/.*\.mk$')
+if [ $? -eq 0 ] ; then
+	for modified_mk in ${modified_mks} ; do
+		check_indentation "${modified_mk}"
+	done
+fi