Repository navigation
Expand file tree
/
Copy pathcommit-msg
More file actions
executable file
·141 lines (116 loc) · 3.85 KB
/
Copy pathcommit-msg
File metadata and controls
executable file
·141 lines (116 loc) · 3.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
set -euo pipefail
message_file=${1:?missing commit message file}
large_commit_file_threshold=${CRIEW_LARGE_COMMIT_FILE_THRESHOLD:-6}
large_commit_line_threshold=${CRIEW_LARGE_COMMIT_LINE_THRESHOLD:-150}
cleaned_message="$(git stripspace --strip-comments < "${message_file}")"
subject="$(
printf '%s\n' "${cleaned_message}" | awk '
NF {
print
exit
}
'
)"
if [[ -z "${subject}" ]]; then
printf '%s\n' "commit message subject is empty" >&2
exit 1
fi
if [[ "${subject}" =~ ^Merge\ ]]; then
exit 0
fi
skip_conventional_subject_check=0
skip_large_commit_body_check=0
if [[ "${subject}" =~ ^Revert\ ]]; then
skip_conventional_subject_check=1
skip_large_commit_body_check=1
fi
if [[ "${subject}" =~ ^(fixup\!\ |squash\!\ ) ]]; then
skip_large_commit_body_check=1
subject=${subject#*! }
fi
conventional_subject_pattern='^(feat|fix|docs|refactor|test|chore)(\([^)[:space:]]+\))?: .+'
if (( ! skip_conventional_subject_check )) && [[ ! "${subject}" =~ ${conventional_subject_pattern} ]]; then
cat >&2 <<'EOF'
commit message subject must use one of:
feat: summary
feat(scope): summary
fix: summary
docs: summary
refactor: summary
test: summary
chore: summary
Rules from docs/code-guildline-cn.md:
- keep one logical change per commit
- use an imperative subject after the prefix
- keep the subject line within 72 characters when possible
EOF
exit 1
fi
subject_length=${#subject}
if (( subject_length > 72 )); then
printf '%s\n' \
"warning: commit subject is ${subject_length} characters; keep it within 72 when possible" \
>&2
fi
trailers="$(printf '%s\n' "${cleaned_message}" | git interpret-trailers --parse)"
signed_off_by_pattern='^Signed-off-by: .+ <.+>$'
if ! grep -Eq "${signed_off_by_pattern}" <<< "${trailers}"; then
cat >&2 <<'EOF'
commit message must include a Signed-off-by trailer.
Use `git commit -s` (or `git commit --signoff`) so Git appends:
Signed-off-by: Your Name <[email protected]>
EOF
exit 1
fi
commit_file_count=${CRIEW_COMMIT_FILE_COUNT:-}
commit_line_count=${CRIEW_COMMIT_LINE_COUNT:-}
if [[ -z "${commit_file_count}" || -z "${commit_line_count}" ]]; then
if [[ -n "${CRIEW_COMMIT_SHA:-}" ]]; then
diff_numstat="$(git show --format= --numstat --find-renames "${CRIEW_COMMIT_SHA}")"
elif git rev-parse --verify --quiet HEAD >/dev/null; then
diff_numstat="$(git diff --cached --numstat --find-renames HEAD)"
else
diff_numstat="$(git diff --cached --numstat --find-renames --root)"
fi
read -r commit_file_count commit_line_count <<< "$(
printf '%s\n' "${diff_numstat}" | awk '
NF >= 3 {
files += 1
if ($1 != "-") {
lines += $1
}
if ($2 != "-") {
lines += $2
}
}
END {
print files + 0, lines + 0
}
'
)"
fi
is_large_commit=0
if (( commit_file_count >= large_commit_file_threshold || commit_line_count >= large_commit_line_threshold )); then
is_large_commit=1
fi
body_lines="$(printf '%s\n' "${cleaned_message}" | sed '1d')"
has_bullet_body=0
if grep -Eq '^[[:space:]]*[-*][[:space:]]+\S' <<< "${body_lines}"; then
has_bullet_body=1
fi
if (( is_large_commit )) && (( ! skip_large_commit_body_check )); then
if (( ! has_bullet_body )); then
cat >&2 <<EOF
larger commits must include a body with bullet points that summarize the main changes.
Current threshold:
- at least ${large_commit_file_threshold} changed files, or
- at least ${large_commit_line_threshold} added/deleted lines
Example:
feat: improve inbox sync startup behavior
- split IMAP startup sync scheduling from periodic auto-sync
- add regression coverage for inbox fallback and config defaults
EOF
exit 1
fi
fi