Skip to content

Commit a60b2be

Browse files
authored
Merge pull request #6558 from DougReeder/add-pre-commit
Adds pre-commit hook that runs automated tests & fixes lint errors
2 parents 6e8f8cc + 5b18adc commit a60b2be

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.githooks/pre-commit

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 --

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Read our [contributor guide](./CONTRIBUTING.md) to learn how you can submit bug
4545

4646
We're also looking for help with localization. The Hubs redesign has a lot of new text and we need help from people like you to translate it. Follow the [localization docs](./src/assets/locales/README.md) to get started.
4747

48+
A Git hook will run before each commit, to lint and test the code.
49+
Fix the issues it complains about, then the hook will allow your commit.
50+
The checks are also run in Continuous Integration, so you'll be requested to fix a Pull Request that fails these checks — the Git hook just gives you faster feedback.
51+
In unusual situations, you can suppress the checks by adding the flag `-n` to your commit command.
52+
53+
To run the checks *before* you commit, run `npm run test`.
54+
4855
Contributors are expected to abide by the project's [Code of Conduct](./CODE_OF_CONDUCT.md) and to be respectful of the project and people working on it.
4956

5057
## Additional Resources

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"scripts": {
1616
"info": "npm-scripts-info",
17+
"dependencies": "git config --local core.hooksPath .githooks/ || true",
1718
"start": "webpack serve --mode=development --env loadAppConfig=1",
1819
"dev": "webpack serve --mode=development --env remoteDev=1 --progress",
1920
"local": "webpack serve --mode=development --env localDev=1",

0 commit comments

Comments
 (0)