This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-cpthook.sh
executable file
·120 lines (102 loc) · 3.36 KB
/
update-cpthook.sh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# Rebuild hooks when cpthook configuration changes
CYAN="\e[0;36m"
YELLOW="\e[0;33m"
RED="\e[0;31m"
GREEN="\e[0;32m"
RESET="\e[0m"
function echo_colour {
local COLOUR=$1
shift
echo -e ${COLOUR}$*${RESET}
}
function echo_error {
echo_colour $RED $*
}
function echo_warning {
echo_colour $YELLOW $*
}
function echo_success {
echo_colour $GREEN $*
}
function echo_notice {
echo_colour $CYAN $*
}
hookdir=""
if [ -d "hooks" ]; then
hookdir="hooks"
elif [ -d ".git/hooks" ]; then
hookdir=".git/hooks"
fi
if [ "$hookdir" == "" ]; then
echo_warning Could not locate hooks.
exit 0
fi
# Attempt to cpthook path from wrapper script in repository.
cpthook=`grep --no-filename cpthook $hookdir/* 2>/dev/null | grep -- --config | cut -d\ -f1 | head -1`
if [ "$cpthook" == "" ]; then
echo_warning Could not locate cpthook.
exit 0
fi
if [ ! -x "$cpthook" ]; then
echo_warning cpthook is not executable.
exit 0
fi
# Attempt to extract hook config file path from wrapper script in repository.
hookcfg=`grep --no-filename cpthook $hookdir/* 2>/dev/null | grep -- --config | sed -e "s/.*--config=\(\S*\).*/\1/" | head -1`
if [ "$hookcfg" == "" ]; then
echo_warning Could not locate hook config.
exit 0
fi
if [ ! -f "$hookcfg" ]; then
echo_warning Hook config does not exist at $hookcfg.
exit 0
fi
hookbase=`basename $hookcfg`
# Look for a file with the same name as the config file that
# cpthook used to run this script, then examine the file contents
# to see if hook-cfg also matches.
while read oldrev newrev refname; do
for file in `git diff-tree --no-commit-id --name-only -r $newrev`; do
filebase=`basename $file`
if [ "$filebase" == "$hookbase" ]; then
tmpdir=`mktemp -d`
tmpfile="$tmpdir/$filebase"
git show $newrev:$file > $tmpfile
cfg=`grep '^\s*hook-cfg\s*=' $tmpfile | \
sed -e "s/^\s*hook-cfg\s*=\s*//"`
if [ "$cfg" == "$hookcfg" ]; then
# Filename in repo and hook-cfg config item match the
# config cpthook was invoked with.
# Check that config file is valid. If not, refuse commit.
$cpthook --config=$tmpfile --validate
ret=$?
if [ $ret -ne 0 ]; then
echo_notice Ran: $cpthook --config=$hookcfg --validate
echo_warning Invalid cpthook configuration.
echo_error Refusing commit.
rm -rf $tmpdir
exit 1
fi
# Attempt to update the active cpthook configuration.
cat $tmpfile > $hookcfg
ret=$?
if [ $? -eq 1 ]; then
echo_warning Could not write $hookcfg. Please investigate.
else
echo_notice Wrote cpthook config $hookcfg.
$cpthook --config=$hookcfg --init
ret=$?
if [ $ret -eq 0 ]; then
echo_success Successfully updated cpthook config
else
echo_notice Ran: $cpthook --config=$hookcfg --init
echo_error cpthook update failed. Please investigate.
fi
fi
fi
rm -rf $tmpdir
fi
done
done
exit 0