-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpre-commit
More file actions
45 lines (38 loc) · 1.01 KB
/
pre-commit
File metadata and controls
45 lines (38 loc) · 1.01 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
#!/bin/sh
#
# Pre-commit hook to format C++ files with clang-format
# Excludes third-party library folders
# Folders to exclude (third-party sources)
EXCLUDE_DIRS="libs"
# Get list of staged C++ files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|h|c|cc|cxx|hpp)$')
if [ -z "$STAGED_FILES" ]; then
exit 0
fi
# Check if clang-format is available
if ! command -v clang-format >/dev/null 2>&1; then
echo "Warning: clang-format not found. Skipping formatting."
echo "Install clang-format to enable automatic formatting."
exit 0
fi
# Format each file that is not in excluded directories
for FILE in $STAGED_FILES; do
# Check if file is in excluded directory
EXCLUDED=0
for EXCLUDE_DIR in $EXCLUDE_DIRS; do
case "$FILE" in
$EXCLUDE_DIR/*)
EXCLUDED=1
break
;;
esac
done
if [ $EXCLUDED -eq 0 ]; then
echo "Formatting: $FILE"
clang-format -i "$FILE"
git add "$FILE"
else
echo "Skipping (third-party): $FILE"
fi
done
exit 0