Skip to content

Commit 811840a

Browse files
committed
Add pre-commit hook for automatic clang-format
Adds .githooks/pre-commit that automatically formats staged C++ files before commit. This prevents formatting issues from reaching CI. To enable: git config core.hooksPath .githooks The hook: - Finds clang-format (prefers versioned like clang-format-19) - Formats only staged .cpp/.hpp/.h/.c files - Re-stages formatted files automatically
1 parent 879e6f6 commit 811840a

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

.githooks/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Git Hooks
2+
3+
This directory contains git hooks for the fastmcpp project.
4+
5+
## Installation
6+
7+
Run one of the following commands to enable the hooks:
8+
9+
```bash
10+
# Option 1: Configure git to use this directory for hooks
11+
git config core.hooksPath .githooks
12+
13+
# Option 2: Symlink (Linux/macOS)
14+
ln -sf ../../.githooks/pre-commit .git/hooks/pre-commit
15+
```
16+
17+
## Available Hooks
18+
19+
### pre-commit
20+
21+
Automatically formats staged C++ files with clang-format before commit.
22+
23+
- Finds clang-format (prefers versioned like clang-format-19)
24+
- Formats only staged `.cpp`, `.hpp`, `.h`, `.c` files
25+
- Re-stages formatted files automatically
26+
27+
This ensures all committed code follows the project's formatting style.

.githooks/pre-commit

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
#!/bin/bash
2-
# Pre-commit hook: auto-format staged C++ files
3-
#
4-
# Enable with: git config core.hooksPath .githooks
2+
# Pre-commit hook to automatically format C++ files with clang-format
3+
# Install: git config core.hooksPath .githooks
54

6-
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp)$')
5+
# Find clang-format (prefer versioned, fall back to unversioned)
6+
CLANG_FORMAT=""
7+
for cf in clang-format-19 clang-format-18 clang-format-17 clang-format; do
8+
if command -v "$cf" &> /dev/null; then
9+
CLANG_FORMAT="$cf"
10+
break
11+
fi
12+
done
713

8-
if [ -z "$STAGED_FILES" ]; then
14+
if [ -z "$CLANG_FORMAT" ]; then
15+
echo "Warning: clang-format not found, skipping formatting"
916
exit 0
1017
fi
1118

12-
# Check if clang-format is available
13-
if ! command -v clang-format &> /dev/null; then
14-
echo "Warning: clang-format not found, skipping auto-format"
19+
# Get staged C++ files
20+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp|h|c)$')
21+
22+
if [ -z "$STAGED_FILES" ]; then
1523
exit 0
1624
fi
1725

18-
# Auto-format and re-stage
26+
# Format each staged file
1927
for file in $STAGED_FILES; do
2028
if [ -f "$file" ]; then
21-
clang-format -i "$file"
29+
$CLANG_FORMAT -i "$file"
2230
git add "$file"
2331
fi
2432
done
2533

26-
exit 0
34+
echo "Formatted $(echo "$STAGED_FILES" | wc -w) file(s) with $CLANG_FORMAT"

0 commit comments

Comments
 (0)