Skip to content

Commit

Permalink
Update pre-commit hook to check scala formatting
Browse files Browse the repository at this point in the history
This commit updates the pre-commit hook to run scalafmt the same way it
already runs prettier. Apart from the exit code tweak, this basically
involved copying the pre-commit hook from
guardian/datawrapper-import#12

As with the prettier part of the hook, this should only run the
formatter on files with staged changes that don’t have unstaged changes,
and for any with both that fail formatting it should just report an
error and exit.
  • Loading branch information
emdash-ie committed Oct 25, 2024
1 parent 241199e commit ccf2c55
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions scripts/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

hookExitCode=0

if command -v fronts-client/node_modules/.bin/prettier > /dev/null; then
echo "Checking file formatting with prettier…"
echo ""
Expand All @@ -23,7 +25,6 @@ if command -v fronts-client/node_modules/.bin/prettier > /dev/null; then
| xargs -0 fronts-client/node_modules/.bin/prettier --list-different)
if test -z "$prettierFormattedOutput" -a -z "$prettierCheckedOutput" ; then
echo "All good"
exit 0
else
if test -n "$prettierFormattedOutput" ; then
echo "The following staged files needed formatting by prettier! Formatting has been
Expand All @@ -46,8 +47,63 @@ overwriting unstaged changes. Please format them yourself or bypass this hook wi
echo "$prettierCheckedOutput"
echo ""
fi
exit 1
hookExitCode=1
fi
else
echo "Note: fronts-client/node_modules/.bin/prettier not found, so skipping formatting check"
fi

if command -v scalafmt > /dev/null; then
echo "Checking file formatting with scalafmt…"
echo ""
# For files which have staged changes and no unstaged changes, let scalafmt format them
scalaFilesWithOnlyStagedChanges=$(
comm -1 -3 \
<(git diff --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.scala\$" | sort))
_scalafmtFormattedOutput=$(
comm -1 -3 \
<(git diff --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
| tr "\n" "\0" \
| xargs -0 scalafmt --reportError --no-stderr --debug)
scalafmtFormattedExit=$?
# For files which have both staged and unstaged changes, check them with scalafmt but don’t modify them
scalafmtCheckedOutput=$(
comm -1 -2 \
<(git diff --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
| tr "\n" "\0" \
| xargs -0 scalafmt --list --reportError --no-stderr)
scalafmtCheckedExit=$?
if test "$scalafmtFormattedExit" -eq 0 -a "$scalafmtCheckedExit" -eq 0 ; then
echo "All good"
else
if test "$scalafmtFormattedExit" -ne 0; then
echo "The following staged files needed formatting by scalafmt! Formatting has been
applied but the files have not been re-staged: please verify the changes, stage,
and commit again."
echo ""
scalaFilesWhichHaveBeenFormatted=$(
comm -1 -2 \
<(git diff --name-only --diff-filter=ACMR | grep "\.scala\$" | sort) \
<(echo "$scalaFilesWithOnlyStagedChanges"))
echo "$scalaFilesWhichHaveBeenFormatted"
echo ""
fi
if test "$scalafmtCheckedExit" -ne 0; then
echo "The following staged files with additional unstaged changes were reported by
scalafmt as needing formatting: these files have not been formatted, to avoid
clobbering unstaged changes. Please format them yourself or bypass this hook with
--no-verify."
echo ""
echo "$scalafmtCheckedOutput"
echo ""
fi
hookExitCode=1
fi
else
echo "Note: scalafmt not found, so skipping formatting check"
fi

exit $hookExitCode

0 comments on commit ccf2c55

Please sign in to comment.