Skip to content

Commit e4ea1a2

Browse files
committed
Add git hooks for local development
1 parent 53877ac commit e4ea1a2

File tree

7 files changed

+108
-1
lines changed

7 files changed

+108
-1
lines changed

.githooks/deploy.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
# https://gist.github.com/apexskier/efb7c1aaa6e77e8127a8
4+
# Deploy hooks stored in your git repo to everyone!
5+
#
6+
# I keep this in $ROOT/$HOOK_DIR/deploy
7+
# From the top level of your git repo, run ./hook/deploy (or equivalent) after
8+
# cloning or adding a new hook.
9+
# No output is good output.
10+
11+
BASE=`git rev-parse --git-dir`
12+
ROOT=`git rev-parse --show-toplevel`
13+
HOOK_DIR=.githooks
14+
HOOKS=$ROOT/$HOOK_DIR/*
15+
16+
if [ ! -d "$ROOT/$HOOK_DIR" ]
17+
then
18+
echo "Couldn't find hooks dir."
19+
exit 1
20+
fi
21+
22+
# Clean up existing hooks.
23+
rm -f $BASE/hooks/*
24+
25+
# Synlink new hooks.
26+
for HOOK in $HOOKS
27+
do
28+
(cd $BASE/hooks ; ln -s $HOOK `basename $HOOK` || echo "Failed to link $HOOK to `basename $HOOK`.")
29+
done
30+
31+
echo "Git hooks deployed to $BASE/hooks. The hooks automatically check your code on every commit."
32+
echo "To bypass them for a single commit, use: git commit --no-verify"
33+
34+
exit 0

.githooks/pre-commit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Fail on first line that fails.
4+
set -e
5+
6+
# Only keep staged files that are added (A), copied (C) or modified (M).
7+
STAGED=$(git --no-pager diff --name-only --cached --diff-filter=ACM)
8+
# Files which are only partly staged (eg. git add --patch).
9+
PATCH_STAGED=$(git --no-pager diff --name-only --diff-filter=ACM $STAGED)
10+
# Files which are fully staged.
11+
FULLY_STAGED=$(comm -23 <(echo "$STAGED") <(echo "$PATCH_STAGED"))
12+
13+
JS_STAGED=$(grep -E '.(js|jsx)$' <<< "$STAGED" || true)
14+
CSS_STAGED=$(grep -E '.(css|scss)$' <<< "$STAGED" || true)
15+
SNAPSHOT_STAGED=$(grep -E '.snap$' <<< "$STAGED" || true)
16+
PRETTIER_STAGED=$(grep -E '.(md|css|scss|js|json|yaml|yml|html)$' <<< "$STAGED" || true)
17+
PRETTIER_FULLY_STAGED=$(grep -E '.(md|css|scss|js|json|yaml|yml|html)$' <<< "$FULLY_STAGED" || true)
18+
19+
# Uncomment, and add more variables to the list, for debugging help.
20+
# tr ' ' '\n' <<< "STAGED $STAGED PATCH_STAGED $PATCH_STAGED FULLY_STAGED $FULLY_STAGED JS_STAGED $JS_STAGED
21+
22+
# Execute each pre-commit hook.
23+
PROJECT_ROOT=`git rev-parse --show-toplevel`
24+
GIT_ROOT=`git rev-parse --git-dir`
25+
HOOKS=$PROJECT_ROOT/$GIT_ROOT/hooks/pre-commit.*
26+
27+
for HOOK in $HOOKS
28+
do
29+
source $HOOK
30+
done

.githooks/pre-commit.0.whitespace.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
# Check if this is the initial commit
4+
if git rev-parse --verify HEAD >/dev/null 2>&1
5+
then
6+
against=HEAD
7+
else
8+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
9+
fi
10+
11+
# Use git diff-index to check for whitespace errors
12+
if ! git diff-index --check --cached $against -- ':!*.js.snap' .
13+
then
14+
echo "Aborting commit due to whitespace errors."
15+
exit 1
16+
fi

.githooks/pre-commit.5.prettier.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Format and re-stage fully staged files only.
4+
if [ -n "$PRETTIER_FULLY_STAGED" ];
5+
then
6+
npx prettier --write $PRETTIER_FULLY_STAGED
7+
git add $PRETTIER_FULLY_STAGED
8+
fi
9+
10+
if [ -n "$PRETTIER_STAGED" ];
11+
then
12+
npx prettier --check $PRETTIER_STAGED
13+
fi

.githooks/pre-commit.6.lint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -n "$JS_STAGED" ];
4+
then
5+
npx eslint --report-unused-disable-directives $JS_STAGED
6+
fi

.githooks/pre-commit.8.test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -n "$JS_STAGED" ] || [ -n "$SNAPSHOT_STAGED" ];
4+
then
5+
npm run test
6+
npm run test:rules
7+
fi

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242
"stylelint": "^10.1.0"
4343
},
4444
"scripts": {
45+
"prepare": "./.githooks/deploy.sh",
4546
"lint": "eslint --report-unused-disable-directives . && prettier --check '**/?(.)*.{md,css,scss,js,json,yaml,yml,html}'",
4647
"format": "prettier --write '**/?(.)*.{md,css,scss,js,json,yaml,yml,html}'",
4748
"build:docs": "node src/documentation.js && prettier --write README.md",
48-
"test": "jest --coverage",
49+
"test": "jest",
4950
"test:rules": "stylelint-find-new-rules src/unused.js --unused --deprecated --invalid",
5051
"test:watch": "jest --watch",
5152
"prepublishOnly": "npm run build:docs"

0 commit comments

Comments
 (0)