Skip to content

Commit

Permalink
Abort git commit if pre-commit applies formatting (#737)
Browse files Browse the repository at this point in the history
In #660 we added a `.pre-commit` file that developers can use to
automatically apply formatting and update datestamps. It's a minor
annoyance that the commit still goes through even if changes were
applied. Then you either have separate "Update datestamps" /
"Formatting" commits or have to squash/fixup with `git rebase`.

This PR changes the pre-commit so that it aborts the commit if any
changes were made. Then you just re-run the `git commit` command with
the formatting/date changes included.
  • Loading branch information
jwallwork23 authored Dec 4, 2024
2 parents 8231348 + 6637be1 commit 177fd96
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 177fd96

Please # to comment.