-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·29 lines (26 loc) · 1.05 KB
/
pre-commit
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
#!/usr/bin/env bash
# pre-commit hook to run astyle on all changed files
# create list of .h , .c, .cpp files staged tracked or modified
files=$(git status --porcelain | grep -E "^(M|A|MM|AM|MA|) " | grep -E "^(M|A|MM|AM|MA) " | grep -E "\.(h|c|cpp)$" | awk '{print $2}')
echo "Checking Code style on files:"
echo $files
# dry run astyle on all files with astylerc config file, if astyle says Formatted, then raise an error
if [ -n "$files" ]; then
# check if astyle is installed
if ! [ -x "$(command -v astyle)" ]; then
echo "Error: astyle is not installed." >&2
exit 1
fi
result=$(astyle --options=astylerc --dry-run $files | grep -E "^(Formatted|Error)")
if [[ $result == *"Error"* ]]; then
echo "Astyle failed $result"
exit 1
fi
# check result of astyle, does it say Formatted?
if [[ $result == *"Formatted"* ]]; then
echo "Astyle failed, please run astyle on these files:"
# strip out the "Formatted" from the result
echo $result | sed 's/Formatted //g'
exit 1
fi
fi