Skip to content

Commit

Permalink
framework: add hook to check indentation of conditionals of Mk/ files
Browse files Browse the repository at this point in the history
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] bbc2474

Reviewed by:		portmgr (rene)
Differential Revision:	https://reviews.freebsd.org/D35041
  • Loading branch information
tcberner committed Apr 25, 2022
1 parent 52f351a commit 27bebbf
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .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

0 comments on commit 27bebbf

Please sign in to comment.