-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·31 lines (26 loc) · 1021 Bytes
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# Usage: ln -sf ../../pre-commit .git/hooks
# Exit at first error
set -Eeu
# To handle partially committed files, we must copy the staged changes to a
# separate location
# See also https://stackoverflow.com/a/36793330
TEMPDIR=$(mktemp -d)
trap "rm -rf $TEMPDIR" EXIT SIGHUP SIGINT SIGQUIT SIGTERM
git checkout-index --prefix=$TEMPDIR/ -af
# keep using the same node_modules/ directory, not a new one in the temporary
# directory this avoids re-parsing everything from scratch every time we run
# the script
GIT_ROOT=$(git rev-parse --show-toplevel)
# prevent committing any file with a "NOCOMMIT" marker
if git diff --cached --diff-filter=AM -- ':!pre-commit' | grep -C3 'NOCOMMIT' --color; then
echo "Commit contains NOCOMMIT markers. Aborting commit."
exit 1
fi
# lint
if ! git diff --cached --name-only --diff-filter=AM --quiet -- src/lib.js; then
if ! [ -e node_modules ]; then
npm ci
fi
(cd $TEMPDIR; ln -sf $GIT_ROOT/node_modules node_modules; npm run lint)
fi