-
Notifications
You must be signed in to change notification settings - Fork 0
/
installhooks
executable file
·53 lines (46 loc) · 1.67 KB
/
installhooks
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /bin/bash
# exit if an error occurs
set -e
# get repo path
echo "Please choose your git repository that you want to activate the hooks for."
# It is advided to use "read" with option -r, so backslashes do not
# escape characters, in case the repository contains a backslash.
read -e -p "Path to repository: " -r REPO
REPO=${REPO%/}
# check if REPO is a git repository
if [ ! -d "$REPO/.git/hooks" ] # please quote variables, otherwise word splitting
then
echo Error: "$REPO/ is not a valid git repository."
exit
fi
# Get path to THIS bash file == the directory of the hooks
# see https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
HOOKDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# activate commit-msg hook
if [ -f "$REPO/.git/hooks/commit-msg" -o -L "$REPO/.git/hooks/commit-msg" ]
then
echo "Delete existing hook?"
# "--" prevents the file name be misinterpreted as option
rm -i -- "$REPO/.git/hooks/commit-msg"
fi
if [ -f "$REPO/.git/hooks/commit-msg" -o -L "$REPO/.git/hooks/commit-msg" ]
then
echo "Left existing hook unmodified."
else
ln -s -- "$HOOKDIR/commit-msg" "$REPO/.git/hooks"
echo "Activated hook for repository in $REPO/ to check the format of your commit messages."
fi
# activate pre-commit hook
if [ -f "$REPO/.git/hooks/pre-commit" -o -L "$REPO/.git/hooks/pre-commit" ]
then
echo "Delete existing hook?"
rm -i -- "$REPO/.git/hooks/pre-commit"
fi
if [ -f "$REPO/.git/hooks/pre-commit" -o -L "$REPO/.git/hooks/pre-commit" ]
then
echo "Left existing hook unmodified."
else
ln -s -- "$HOOKDIR/pre-commit" "$REPO/.git/hooks"
echo "Activated hook for repository in $REPO/ to check your commit for trailing whitespaces."
fi
exit