File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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
1017fi
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
1624fi
1725
18- # Auto-format and re-stage
26+ # Format each staged file
1927for 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
2432done
2533
26- exit 0
34+ echo " Formatted $( echo " $STAGED_FILES " | wc -w ) file(s) with $CLANG_FORMAT "
You can’t perform that action at this time.
0 commit comments