Skip to content

Commit 03dda26

Browse files
committed
Add mechanism for installing useful git hooks
Run install-git-hooks.sh to install all pre-commit hooks (in the hooks subdirectory) into the git repository at the CWD. An existing pre-commit script is not overwritten, but appended to. A non-existent pre-commit script is created. Already-installed hooks are not installed again.
1 parent e16bb12 commit 03dda26

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

hooks/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder contains hooks installed by the bin/install-git-hooks.sh script.

install-git-hooks.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
#
4+
# install-git-hooks.sh
5+
#
6+
7+
# A script to install recommended git hooks into your .git/hooks folder.
8+
# It will append them without overwriting any existing hooks you have.
9+
10+
HOOK_DIR="$(dirname "$0")/hooks"
11+
12+
PRE_COMMIT=.git/hooks/pre-commit
13+
14+
# must be run from toplevel of repository
15+
if [ ! -d .git ];
16+
then
17+
echo "Must be run from toplevel of a git repository!"
18+
exit 1
19+
fi
20+
21+
# create pre-commit hook if it does not already exist
22+
if [ ! -e "$PRE_COMMIT" ];
23+
then
24+
echo "Creating: $PRE_COMMIT"
25+
echo '#!/bin/sh' >> "$PRE_COMMIT"
26+
echo >> "$PRE_COMMIT"
27+
chmod +x "$PRE_COMMIT"
28+
fi
29+
30+
# inject bin/hooks calls into relevant git hook scripts
31+
for f in "$HOOK_DIR/pre-commit-"*;
32+
do
33+
if ! grep -q "$f" "$PRE_COMMIT"
34+
then
35+
echo "Installing: $f"
36+
echo "$f" >> "$PRE_COMMIT"
37+
fi
38+
done

0 commit comments

Comments
 (0)