Skip to content

Instantly share code, notes, and snippets.

@socketbox
Last active May 23, 2022 22:30
Show Gist options
  • Save socketbox/9c1f2f7baf6cf30a36bca26be0cc51d8 to your computer and use it in GitHub Desktop.
Save socketbox/9c1f2f7baf6cf30a36bca26be0cc51d8 to your computer and use it in GitHub Desktop.
Pre-commit hook for git
if git rev-parse --verify HEAD
then
export against=HEAD
else
# Initial commit: diff against an empty tree object
export against=$(git hash-object -t tree /dev/null)
fi
files=$(git diff --name-only --diff-filter=ACTM $against)
for f in $files; do
#check if file is a TF file
if [ "grep -i \.tf '$f'" ]
then
#capture exit code from check
ret=`terraform fmt -check $f`
if [ $ret ]
then
echo "Formatting Error:"
exec terraform fmt -diff $f
fi
fi
done
@socketbox
Copy link
Author

Switched condition involving file extension check from a bash-specific test to something that isn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment