-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1-pre-commit
More file actions
61 lines (51 loc) · 1.58 KB
/
1-pre-commit
File metadata and controls
61 lines (51 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
#!/bin/bash
# Installation:
# Copy this script as pre-commit to .git/hooks folder
OPTIONS="--options=astyle-options.txt"
# Check if .gitignore file exists
if [ ! -f astyle-options.txt ]; then
echo "[!] astyle-options.txt file not found. Please make sure the astyle-options.txt file is present." >&2
exit 1
fi
RETURN=0
ASTYLE=$(command -v astyle)
if [ $? -ne 0 ]; then
echo "[!] AStyle is not installed. Unable to check source file format policy." >&2
exit 1
fi
# Check if .gitignore file exists
if [ ! -f .gitignore ]; then
echo "[!] .gitignore file not found. Please make sure the .gitignore file is present." >&2
exit 1
fi
# Check if README.md file exists
if [ ! -f README.md ]; then
echo "[!] README.md file not found. Please make sure the README.md file is present." >&2
exit 1
fi
# Check if .gitignore file exists
if [ ! -f Doxyfile ]; then
echo "[!] Doxyfile file not found. Please make sure the Doxyfile file is present." >&2
exit 1
fi
FILES=$(git diff --cached --name-only --diff-filter=ACMRTUXB | grep -E "\.(cs|java|c|cpp|h)$")
for FILE in $FILES; do
FORMATTED=$( "$ASTYLE" $OPTIONS < "$FILE" 2>&1 )
if [ $? -ne 0 ]; then
echo "[!] Error formatting $FILE:" >&2
echo "$FORMATTED" >&2
RETURN=1
elif [ -n "$FORMATTED" ]; then
echo "Formatted $FILE"
echo "$FORMATTED"
echo ""
echo "$FORMATTED" > "$FILE"
git add "$FILE"
fi
done
if [ $RETURN -eq 1 ]; then
echo "" >&2
echo "Make sure you have run AStyle with the following options:" >&2
echo $OPTIONS >&2
fi
exit $RETURN