Last active
May 23, 2022 22:30
-
-
Save socketbox/9c1f2f7baf6cf30a36bca26be0cc51d8 to your computer and use it in GitHub Desktop.
Pre-commit hook for git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switched condition involving file extension check from a bash-specific test to something that isn't