|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# READ THIS BEFORE MAKING CHANGES: |
| 4 | +# |
| 5 | +# If you want to add a new pre-commit check, here are the rules: |
| 6 | +# |
| 7 | +# 1. Create a bash function for your check (see e.g. ui_lint below). |
| 8 | +# NOTE: Each function will be called in a sub-shell so you can freely |
| 9 | +# change directory without worrying about interference. |
| 10 | +# 2. Add the name of the function to the CHECKS variable. |
| 11 | +# 3. If no changes relevant to your new check are staged, then |
| 12 | +# do not output anything at all - this would be annoying noise. |
| 13 | +# In this case, call 'return 0' from your check function to return |
| 14 | +# early without blocking the commit. |
| 15 | +# 4. If any non-trivial check-specific thing has to be invoked, |
| 16 | +# then output '==> [check description]' as the first line of |
| 17 | +# output. Each sub-check should output '--> [subcheck description]' |
| 18 | +# after it has run, indicating success or failure. |
| 19 | +# 5. Call 'block [reason]' to block the commit. This ensures the last |
| 20 | +# line of output calls out that the commit was blocked - which may not |
| 21 | +# be obvious from random error messages generated in 4. |
| 22 | +# |
| 23 | +# At the moment, there are no automated tests for this hook, so please run it |
| 24 | +# locally to check you have not broken anything - breaking this will interfere |
| 25 | +# with other peoples' workflows significantly, so be sure, check everything twice. |
| 26 | + |
| 27 | +set -euo pipefail |
| 28 | + |
| 29 | +# Call block to block the commit with a message. |
| 30 | +block() { |
| 31 | + echo "$@" |
| 32 | + echo "Commit blocked - see errors above." |
| 33 | + exit 1 |
| 34 | +} |
| 35 | + |
| 36 | +# Add all check functions to this space separated list. |
| 37 | +# They are executed in this order (see end of file). |
| 38 | +CHECKS="ui_lint backend_lint" |
| 39 | + |
| 40 | +# Run ui linter if changes in that dir detected. |
| 41 | +ui_lint() { |
| 42 | + local DIR=ui LINTER=node_modules/.bin/lint-staged |
| 43 | + |
| 44 | + # Silently succeed if no changes staged for $DIR |
| 45 | + if git diff --name-only --cached --exit-code -- $DIR/; then |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + |
| 49 | + # Silently succeed if the linter has not been installed. |
| 50 | + # We assume that if you're doing UI dev, you will have installed the linter |
| 51 | + # by running yarn. |
| 52 | + if [ ! -x $DIR/$LINTER ]; then |
| 53 | + return 0 |
| 54 | + fi |
| 55 | + |
| 56 | + echo "==> Changes detected in $DIR/: Running linter..." |
| 57 | + |
| 58 | + # Run the linter from the UI dir. |
| 59 | + cd $DIR |
| 60 | + $LINTER || block "UI lint failed" |
| 61 | +} |
| 62 | + |
| 63 | +backend_lint() { |
| 64 | + # Silently succeed if no changes staged for Go code files. |
| 65 | + staged=$(git diff --name-only --cached --exit-code -- '*.go') |
| 66 | + ret=$? |
| 67 | + if [ $ret -eq 0 ]; then |
| 68 | + return 0 |
| 69 | + fi |
| 70 | + |
| 71 | + # Only run fmtcheck on staged files |
| 72 | + ./scripts/gofmtcheck.sh "${staged}" || block "Backend linting failed; run 'make fmt' to fix." |
| 73 | +} |
| 74 | + |
| 75 | +for CHECK in $CHECKS; do |
| 76 | + # Force each check into a subshell to avoid crosstalk. |
| 77 | + ( $CHECK ) || exit $? |
| 78 | +done |
0 commit comments