-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·63 lines (54 loc) · 1.58 KB
/
pre-commit
File metadata and controls
executable file
·63 lines (54 loc) · 1.58 KB
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
#!/bin/bash
function astyle {
for FILE in $FILES_TO_ADJUST; do
$ASTYLE $OPTIONS $FILE
git add $FILE
done
}
echo "running pre commit hook to check style guide!" >&2
OPTIONS="--options=astyle/.astylerc"
RETURN=0
ASTYLE=$(which astyle)
if [ $? -ne 0 ]; then
echo "[!] astyle not installed. Unable to check source file format policy." >&2
echo "If you wan't to bypass the check you can run 'git commit --no-verify'" >&2
exit 1
fi
FILES=`git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(cpp|h)$"`
FILES_TO_ADJUST=()
for FILE in $FILES; do
$ASTYLE $OPTIONS < $FILE > /tmp/diff
DIFF=$(diff -B $FILE /tmp/diff)
if [ "$DIFF" != "" ]; then
FILES_TO_ADJUST+="$FILE "
echo "[!] $FILE does not respect the agreed coding style." >&2
echo "diff is: " >&2
echo "$DIFF" >&2
RETURN=1
fi
rm /tmp/diff
done
if [ $RETURN -eq 1 ]; then
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
read -r -p "Do you wan't to run the automatic style adjustment? [Y/n]" yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] )
echo "adjusting files: $FILES_TO_ADJUST"
astyle
RETURN=0
;;
[Nn] )
echo "Make sure you have run:" >&2
echo "$ASTYLE $OPTIONS --recursive "biotracker/*.cpp"" >&2
echo "$ASTYLE $OPTIONS --recursive "biotracker/*.h"" >&2
;;
* )
echo "Please answer y or n for yes or no."
;;
esac
fi
exit $RETURN