diff --git a/.pre-commit b/.pre-commit index 551a1d9e3..55bdc4b1f 100644 --- a/.pre-commit +++ b/.pre-commit @@ -2,6 +2,9 @@ # An example pre-commit file for nextSIM-DG development +BEFORE=$(mktemp) +git diff > ${BEFORE} + for FILE in $(git diff --cached --name-only | grep -iE '\.(cpp|cc|h|hpp)$'); do # Apply clang-format for linting clang-format -i ${FILE} --verbose @@ -11,3 +14,12 @@ for FILE in $(git diff --cached --name-only | grep -iE '\.(cpp|cc|h|hpp)$'); do NEWLINE=$(printf "%s%$((SPACES))s%s\n" " @date" " " "$(date '+%d %b %Y')") sed -i~ "s/${LINE}/${NEWLINE}/" ${FILE} done + +# Abort commit if formatting was applied +AFTER=$(mktemp) +git diff > ${AFTER} +if [ "$(diff nextsim_precommit_before.patch nextsim_precommit_after.patch)" ]; then + echo "Aborting git commit because the pre-commit hook made changes." + rm ${BEFORE} ${AFTER} + exit 1 +fi