forked from DafeCpp/spring_alogorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-git-hooks.sh
More file actions
executable file
·43 lines (34 loc) · 1.24 KB
/
setup-git-hooks.sh
File metadata and controls
executable file
·43 lines (34 loc) · 1.24 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
#!/bin/bash
# Установка pre-commit хука для автоформатирования C++ через clang-format.
# Выполните этот скрипт один раз: ./setup-git-hooks.sh
# Create .git/hooks directory if it doesn't exist
mkdir -p .git/hooks
# Create pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# Get the list of staged C++ files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(cpp|h|hpp)$")
if [ -z "$STAGED_FILES" ]; then
exit 0
fi
# Check if clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo "Error: clang-format is not installed. Please install it first."
echo "On Ubuntu/Debian: sudo apt-get install clang-format"
echo "On macOS: brew install clang-format"
exit 1
fi
# Format each staged file
echo "Running clang-format on staged files..."
for file in $STAGED_FILES; do
echo "Formatting $file"
clang-format -i -style=file "$file"
git add "$file"
done
echo "Formatting complete!"
exit 0
EOF
# Make the pre-commit hook executable
chmod +x .git/hooks/pre-commit
echo "Git pre-commit hook for code formatting has been set up successfully!"
echo "Now your code will be automatically formatted before each commit."