From 187ed5f3ddd067c08a16650d83d02f55a020e660 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Mon, 18 Nov 2024 13:22:29 +0000 Subject: [PATCH 1/2] Compare git diff before and after formatting loop --- .pre-commit | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.pre-commit b/.pre-commit index 551a1d9e3..f47b71507 100644 --- a/.pre-commit +++ b/.pre-commit @@ -2,6 +2,8 @@ # An example pre-commit file for nextSIM-DG development +git diff >nextsim_precommit_before.patch + 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 +13,10 @@ 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 +git diff >nextsim_precommit_after.patch +if [ "$(diff nextsim_precommit_before.patch nextsim_precommit_after.patch)" ]; then + echo "Aborting git commit because the pre-commit hook made changes." + exit 1 +fi From 68569459df372e61db68d5a3b8649417ba880d0c Mon Sep 17 00:00:00 2001 From: Joe Wallwork <22053413+jwallwork23@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:40:23 +0000 Subject: [PATCH 2/2] Apply suggestions from @einola MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Einar Örn Ólason --- .pre-commit | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.pre-commit b/.pre-commit index f47b71507..55bdc4b1f 100644 --- a/.pre-commit +++ b/.pre-commit @@ -2,7 +2,8 @@ # An example pre-commit file for nextSIM-DG development -git diff >nextsim_precommit_before.patch +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 @@ -15,8 +16,10 @@ for FILE in $(git diff --cached --name-only | grep -iE '\.(cpp|cc|h|hpp)$'); do done # Abort commit if formatting was applied -git diff >nextsim_precommit_after.patch +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