From ccf2c55e2d8f0d7d0fbf553c6de8b8ea08aca35f Mon Sep 17 00:00:00 2001 From: Emily Bourke Date: Fri, 25 Oct 2024 12:35:42 +0100 Subject: [PATCH] Update pre-commit hook to check scala formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/guardian/datawrapper-import/pull/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. --- scripts/git-hooks/pre-commit | 60 ++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/scripts/git-hooks/pre-commit b/scripts/git-hooks/pre-commit index f58f1581dd..1e502a7c54 100755 --- a/scripts/git-hooks/pre-commit +++ b/scripts/git-hooks/pre-commit @@ -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 "" @@ -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 @@ -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