Skip to content

Commit 81290dd

Browse files
add Git hooks
1 parent b59a839 commit 81290dd

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

.bin/git/hooks-wrapper

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
# Runs all executable pre-commit-* hooks and exits after,
4+
# if any of them was not successful.
5+
#
6+
# Based on
7+
# https://github.com/ELLIOTTCABLE/Paws.js/blob/Master/Scripts/git-hooks/chain-hooks.sh
8+
# http://osdir.com/ml/git/2009-01/msg00308.html
9+
#
10+
# assumes your scripts are located at <repo-root>/bin/git/hooks
11+
12+
exitcodes=()
13+
hookname=`basename $0`
14+
# our special hooks folder
15+
CUSTOM_HOOKS_DIR=$(git rev-parse --show-toplevel)/bin/git/hooks
16+
# find gits native hooks folder
17+
NATIVE_HOOKS_DIR=$(git rev-parse --show-toplevel)/.git/hooks
18+
19+
# Run each hook, passing through STDIN and storing the exit code.
20+
# We don't want to bail at the first failure, as the user might
21+
# then bypass the hooks without knowing about additional issues.
22+
23+
for hook in $CUSTOM_HOOKS_DIR/$(basename $0)-*; do
24+
test -x "$hook" || continue
25+
26+
echo "Running custom hook '$hookname' ..."
27+
out=`$hook "$@"`
28+
exitcodes+=($?)
29+
echo "$out"
30+
done
31+
32+
# check if there was a local hook that was moved previously
33+
if [ -f "$NATIVE_HOOKS_DIR/$hookname.local" ]; then
34+
echo "Running native hook '$hookname' ..."
35+
out=`$NATIVE_HOOKS_DIR/$hookname.local "$@"`
36+
exitcodes+=($?)
37+
echo "$out"
38+
fi
39+
40+
# If any exit code isn't 0, bail.
41+
for i in "${exitcodes[@]}"; do
42+
[ "$i" == 0 ] || exit $i
43+
done
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
REPO_ROOT_DIR=$(git rev-parse --show-toplevel)
4+
CHANGE_COUNT=$(cd ${REPO_ROOT_DIR}; git diff --name-only origin/HEAD..HEAD -- resources/ src/ test/ Dockerfile scripts.sh |wc -l)
5+
6+
if [[ "0" -ne "${CHANGE_COUNT}" ]]; then
7+
(cd ${REPO_ROOT_DIR}; ./scripts.sh rebuild_nginx rebuild_test_runner test)
8+
else
9+
HOOK_NAME=$(basename $0)
10+
11+
echo "Skipping hook '${HOOK_NAME}' -- no changes detected which would require tests to be run."
12+
fi

.bin/git/init-hooks

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
# based on http://stackoverflow.com/a/3464399/1383268
3+
# assumes that the hooks-wrapper script is located at <repo-root>/bin/git/hooks-wrapper
4+
5+
HOOK_NAMES="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-receive update post-receive post-update pre-auto-gc pre-push"
6+
# find git's native hooks folder
7+
REPO_ROOT_DIR=$(git rev-parse --show-toplevel)
8+
HOOKS_DIR=$(git rev-parse --show-toplevel)/.git/hooks
9+
10+
for hook in $HOOK_NAMES; do
11+
# If the hook already exists, is a file, and is not a symlink
12+
if [ ! -h $HOOKS_DIR/$hook ] && [ -f $HOOKS_DIR/$hook ]; then
13+
mv $HOOKS_DIR/$hook $HOOKS_DIR/$hook.local
14+
fi
15+
# create the symlink, overwriting the file if it exists
16+
# probably the only way this would happen is if you're using an old version of git
17+
# -- back when the sample hooks were not executable, instead of being named ____.sample
18+
ln -s -f $REPO_ROOT_DIR/bin/git/hooks-wrapper $HOOKS_DIR/$hook
19+
done

.bin/init

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
source $(dirname $0)/git/init-hooks

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@ auth_jwt_validate_email off;
144144

145145
## Contributing
146146

147-
If you'd like to contribute to this repository, please first initiate the Git hooks by running `./bin/init` -- this will ensure that tests are run before you push your changes.
147+
If you'd like to contribute to this repository, please first initiate the Git hooks by running `./.bin/init` (note the `.` before `bin`) -- this will ensure that tests are run before you push your changes.

0 commit comments

Comments
 (0)