Skip to Content

Pre-commit hook for CFLint

Working on a CFLint presentation for TACFUG and wanted to demo using CFLint in a pre-commit hook.

After a bit of hacking I came up with the following:

:::sh
#!C:/Program\ Files/Git/usr/bin/sh.exe
#
# This will run CFLint and report an errors.
# If warnings are fount - the commit will be halted.
# Info issues will be committed!

# check for staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -i "cf[c,m]$")
if [ -z "$STAGED_FILES" ]; then
	echo "No staged files"
    exit 0
fi

# avoid using booleans in bash - they are a pita
PASS=SUCCESS
echo "--------------------------------------------------"
for FILE in $STAGED_FILES
do
	if java -jar D:\\tacfug\\CFLint-1.3.0-all.jar --quiet --file "$FILE" -text -stdout | grep -i warnings: ; then
			echo "CFLint Failed: $FILE"
			PASS=FAILED
		else
			echo "CFLint Passed: $FILE"
	fi
	echo "--------------------------------------------------"
done

if [[ "$PASS" == SUCCESS ]]; then
		echo "Passed!"
		echo "--------------------------------------------------"
		exit 0;
	else
		echo "Commit halted!"
		echo "--------------------------------------------------"
		exit 1;
fi

I’m running Windows at work with Git bash installed. Since this was a demo I wasn’t too worried about paths. This could be easily cleaned up and modified to suit your environment and CFLint needs.