-
Notifications
You must be signed in to change notification settings - Fork 0
72 lines (57 loc) · 2.06 KB
/
git-validate.yml
File metadata and controls
72 lines (57 loc) · 2.06 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
name: Git Commit Validation
on:
push:
branches: [ master, main, 'feature/**' ]
pull_request:
branches: [ master, main ]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整提交历史
- name: Validate Git Commit Info
run: |
echo "========================================"
echo "CI Git 信息校验"
echo "========================================"
# 获取提交信息
USER_NAME=$(git log -1 --pretty=%an)
USER_EMAIL=$(git log -1 --pretty=%ae)
COMMIT_MSG=$(git log -1 --pretty=%B | head -n1)
echo "提交者: $USER_NAME <$USER_EMAIL>"
echo "提交信息: $COMMIT_MSG"
echo ""
# ===== 校验用户名 =====
is_valid=false
# 中文 2-4 个字符
if echo "$USER_NAME" | grep -qP '[\x{4e00}-\x{9fa5}]{2,4}'; then
is_valid=true
fi
# 英文 2-20 个字符
if echo "$USER_NAME" | grep -qE '^[a-zA-Z ]{2,20}$'; then
is_valid=true
fi
if [ "$is_valid" = false ]; then
echo "❌ 错误:用户名 '$USER_NAME' 格式不符合规范"
echo "规范:2-4个中文 或 2-20个英文字符"
exit 1
fi
echo "✓ 用户名校验通过: $USER_NAME"
# ===== 校验 Commit Message =====
msg_len=${#COMMIT_MSG}
if [ "$msg_len" -lt 10 ] || [ "$msg_len" -gt 100 ]; then
echo "❌ 错误:Commit message 长度应为 10-100 字符,当前: $msg_len"
exit 1
fi
if [[ "$COMMIT_MSG" =~ ^[[:space:]] ]] || [[ "$COMMIT_MSG" =~ [[:space:]]$ ]]; then
echo "❌ 错误:Commit message 不能以空格开头或结尾"
exit 1
fi
if [[ "$COMMIT_MSG" =~ [。.]$ ]]; then
echo "❌ 错误:Commit message 不能以句号结尾"
exit 1
fi
echo "✓ Commit message 校验通过 (${msg_len}字符)"
echo "========================================"