|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Setup for the check for filenames with non-ASCII characters |
| 4 | +if git rev-parse --verify HEAD >/dev/null 2>&1 |
| 5 | +then |
| 6 | + against=HEAD |
| 7 | +else |
| 8 | + # Initial commit: diff against an empty tree object |
| 9 | + against=$(git hash-object -t tree /dev/null) |
| 10 | +fi |
| 11 | + |
| 12 | +# If you want to allow non-ASCII filenames set this variable to true. |
| 13 | +allownonascii=$(git config --type=bool hooks.allownonascii) |
| 14 | + |
| 15 | +# Redirect output to stderr. |
| 16 | +exec 1>&2 |
| 17 | + |
| 18 | +# Cross platform projects tend to avoid non-ASCII filenames; prevent |
| 19 | +# them from being added to the repository. We exploit the fact that the |
| 20 | +# printable range starts at the space character and ends with tilde. |
| 21 | +if [ "$allownonascii" != "true" ] && |
| 22 | + # Note that the use of brackets around a tr range is ok here, (it's |
| 23 | + # even required, for portability to Solaris 10's /usr/bin/tr), since |
| 24 | + # the square bracket bytes happen to fall in the designated range. |
| 25 | + test $(git diff-index --cached --name-only --diff-filter=A -z $against | |
| 26 | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 |
| 27 | +then |
| 28 | + cat <<\EOF |
| 29 | +Error: Attempt to add a non-ASCII file name. |
| 30 | +This can cause problems if you want to work with people on other platforms. |
| 31 | +To be portable it is advisable to rename the file. |
| 32 | +If you know what you are doing you can disable this check using: |
| 33 | + git config hooks.allownonascii true |
| 34 | +EOF |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# lints changed JavaScript and TypeScript |
| 39 | +changed_js=$(git diff-index --cached --name-only $against | grep -E "(.*\.(js|mjs|ts|mts)|scripts/.*\.(js|mjs|ts|mts)|src/.*\.(js|mjs|ts|mts)|test/.*\.(js|mjs|ts|mts)|admin/.*\.(js|mjs|ts|mts)|admin/src/.*\.(js|mjs|ts|mts))$") |
| 40 | +# /dev/null prevents eslint from checking everything |
| 41 | +./node_modules/.bin/eslint ${changed_js:-/dev/null} |
| 42 | +LINT_STATUS=$? |
| 43 | +if [ "$LINT_STATUS" != "0" ]; then |
| 44 | + echo 'Fix the errors above, then use `git add` to restage all fixed files' |
| 45 | + exit $LINT_STATUS |
| 46 | +fi |
| 47 | + |
| 48 | +# lints changed HTML |
| 49 | +changed_html=$(git diff-index --cached --name-only $against | grep -E "^src/.*\.html$") |
| 50 | +# src/tokens.html prevents htmlhint from checking everything |
| 51 | +node_modules/.bin/htmlhint ${changed_html:-src/tokens.html} |
| 52 | +LINT_STATUS=$? |
| 53 | +if [ "$LINT_STATUS" != "0" ]; then |
| 54 | + echo 'Fix the errors above, then use `git add` to restage all fixed files' |
| 55 | + exit $LINT_STATUS |
| 56 | +fi |
| 57 | + |
| 58 | +# runs all automated tests |
| 59 | +npm run test:unit |
| 60 | + |
| 61 | +# If there are whitespace errors, print the offending file names and fail. |
| 62 | +exec git diff-index --check --cached $against -- |
0 commit comments