-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·46 lines (37 loc) · 1.58 KB
/
pre-commit
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
#!/bin/bash
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Check if clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo -e "${YELLOW}Warning: clang-format is not installed.${NC}"
echo -e "${YELLOW}Your code will not be automatically formatted.${NC}"
echo -e "${YELLOW}To install clang-format:${NC}"
echo -e "${YELLOW} - macOS: brew install clang-format${NC}"
echo -e "${YELLOW} - Ubuntu/Debian: sudo apt-get install clang-format${NC}"
echo -e "${YELLOW} - Windows with Chocolatey: choco install llvm${NC}"
echo -e "${YELLOW}Commit will proceed, but please consider installing clang-format.${NC}"
exit 0
fi
# Get list of staged files that are C/C++ files
staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cpp|h|hpp)$')
# If there are no C/C++ files staged, exit early
if [ -z "$staged_files" ]; then
echo -e "${GREEN}No C/C++ files to format.${NC}"
exit 0
fi
echo -e "${GREEN}Running format.sh to format your code...${NC}"
./format.sh
# Check if formatting would change any files
echo -e "${GREEN}Checking if formatting is correct...${NC}"
if ! ./check_format.sh > /dev/null 2>&1; then
# If formatting failed, add the changes and show what was changed
echo -e "${YELLOW}Automatically fixing formatting issues...${NC}"
# Add the changes back to staging
echo "$staged_files" | xargs git add
echo -e "${GREEN}Formatting issues fixed and changes staged.${NC}"
fi
echo -e "${GREEN}Pre-commit hook completed successfully!${NC}"
exit 0