Skip to content

Commit bc44e71

Browse files
authored
Merge pull request #303 from sysprog21/refine-git-hooks
Refine git hooks
2 parents 3c3fa14 + 064429e commit bc44e71

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

scripts/commit-msg.hook

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,29 @@ validate_commit_message() {
489489
# ------------------------------------------------------------------------------
490490

491491
# Alert if the commit message appears to be written in Chinese.
492-
# This pattern matches any Chinese character (common CJK Unified Ideographs).
493-
MISSPELLED_WORDS=$(echo "$FULL_COMMIT_MSG" | LC_ALL=C grep "[一-龥]")
494-
if [ -n "$MISSPELLED_WORDS" ]; then
495-
add_warning 1 "Commit message appears to be written in Chinese: $MISSPELLED_WORDS"
492+
# This pattern matches Chinese CJK Unified Ideographs range.
493+
# Use proper UTF-8 handling to avoid false positives with Unicode symbols like ✓ and ✗.
494+
if command -v python3 >/dev/null 2>&1; then
495+
CHINESE_TEXT=$(echo "$FULL_COMMIT_MSG" | python3 -c "
496+
import sys, re
497+
text = sys.stdin.read()
498+
chinese_chars = re.findall(r'[\u4e00-\u9fff]', text)
499+
if chinese_chars:
500+
print(''.join(chinese_chars))
501+
")
502+
else
503+
# Fallback: Only detect actual Chinese character patterns if python3 is not available
504+
CHINESE_TEXT=$(echo "$FULL_COMMIT_MSG" | grep -o "[一-龥]" 2>/dev/null || echo "")
505+
# Filter out false positives by checking if the detected text contains actual Chinese words
506+
if [ -n "$CHINESE_TEXT" ] && ! echo "$CHINESE_TEXT" | grep -q "[✓✗]"; then
507+
CHINESE_TEXT="$CHINESE_TEXT"
508+
else
509+
CHINESE_TEXT=""
510+
fi
511+
fi
512+
513+
if [ -n "$CHINESE_TEXT" ]; then
514+
add_warning 1 "Commit message appears to be written in Chinese: $CHINESE_TEXT"
496515
fi
497516

498517
MSG_FOR_SPELLCHECK_LINE_FINDING=$(echo "$FULL_COMMIT_MSG_WITH_SPACE" | sed -E \

scripts/common.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
RED=""
2+
GREEN=""
23
YELLOW=""
34
BLUE=""
45
WHITE=""
@@ -11,6 +12,7 @@ set_colors() {
1112
# If color is forced (always) or auto and we are on a tty, enable color.
1213
if [[ "$default_color" == "always" ]] || [[ "$default_color" == "auto" && -t 1 ]]; then
1314
RED='\033[1;31m'
15+
GREEN='\033[1;32m'
1416
YELLOW='\033[1;33m'
1517
BLUE='\033[1;34m'
1618
WHITE='\033[1;37m'

scripts/pre-commit.hook

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ report_result() {
3131
local status="$1"
3232
local message="$2"
3333
if [ "$status" -eq 0 ]; then
34-
printf " ${GREEN}${NC}\n"
34+
printf " [ ${GREEN}OK${NC} ]\n"
3535
else
36-
printf " ${RED}${NC}\n"
36+
printf " [ ${RED}FAIL${NC} ]\n"
3737
if [ -n "$message" ]; then
3838
ERRORS_FOUND+=("$message")
3939
fi
@@ -334,8 +334,8 @@ fi
334334
# Clear the progress line
335335
printf "\r%*s\r" 50 ""
336336

337-
# === SUMMARY ===
338-
printf "\n${CYAN}=== Pre-commit Check Summary ===${NC}\n\n"
337+
# Summary
338+
printf "\n"
339339

340340
# Show file changes
341341
printf "${CYAN}Files to be committed:${NC}\n"
@@ -359,7 +359,7 @@ done
359359
if [ ${#ERRORS_FOUND[@]} -gt 0 ]; then
360360
printf "\n${RED}Errors found:${NC}\n"
361361
for error in "${ERRORS_FOUND[@]}"; do
362-
printf " ${RED}${NC} %s\n" "$error"
362+
printf " [ ${RED}FAIL${NC} ] %s\n" "$error"
363363
done
364364
fi
365365

scripts/pre-push.hook

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ run_build_checks() {
6464
echo ""
6565

6666
# Clean previous build artifacts for fresh check
67-
echo -e "${YELLOW}Cleaning previous build...${NC}"
67+
printf "${YELLOW}Cleaning previous build...${NC}"
6868
make clean >/dev/null 2>&1 || true
69+
printf " [ ${GREEN}OK${NC} ]\n"
6970

70-
echo -e "${YELLOW}Building project...${NC}"
71+
printf "${YELLOW}Building project...${NC}"
7172

7273
# Capture build output for better error reporting
7374
build_output=$(make 2>&1)
7475
build_result=$?
7576

7677
if [ $build_result -ne 0 ]; then
77-
echo -e "${RED}Build failed!${NC}"
78+
printf " [ ${RED}FAIL${NC} ]\n"
7879
echo ""
7980
echo "Build output:"
8081
echo "============="
@@ -85,7 +86,7 @@ run_build_checks() {
8586
return 1
8687
fi
8788

88-
echo -e "${GREEN}✓ Build successful${NC}"
89+
printf " [ ${GREEN}OK${NC} ]\n"
8990

9091
# Additional checks could be added here
9192
# For example: basic tests, format checks, etc.

0 commit comments

Comments
 (0)