Repository navigation
Expand file tree
/
Copy pathcheck-commit
More file actions
executable file
·208 lines (167 loc) · 7.27 KB
/
Copy pathcheck-commit
File metadata and controls
executable file
·208 lines (167 loc) · 7.27 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
"""
Check commit messages against Penpot's commit guidelines.
Validates commit messages using the rules defined in:
- .github/workflows/commit-checker.yml (regex pattern)
- CONTRIBUTING.md (formatting rules, subject length, DCO)
By default, checks HEAD. Use --commit to specify a different commit.
Usage:
./scripts/check-commit
./scripts/check-commit --commit HEAD~1
./scripts/check-commit -c abc1234
"""
import argparse
import re
import subprocess
import sys
# ── Emoji list ───────────────────────────────────────────────────────────────
# Combined from commit-checker.yml AND CONTRIBUTING.md
VALID_EMOJIS = (
"lipstick|globe_with_meridians|wrench|books|"
"arrow_up|arrow_down|zap|ambulance|construction|"
"boom|fire|whale|bug|sparkles|paperclip|tada|"
"recycle|rewind|construction_worker|rocket"
)
# ── Regex from .github/workflows/commit-checker.yml ──────────────────────────
# Matches:
# 1) ":emoji: <Capitalized subject without trailing dot>"
# 2) "Merge|Revert|Reapply ... without trailing dot"
COMMIT_PATTERN = re.compile(
r"^((:(" + VALID_EMOJIS + r"):\s[A-Z].*[^.]))$"
)
MERGE_PATTERN = re.compile(r"^(Merge|Revert|Reapply).+[^.]$")
# ═══════════════════════════════════════════════════════════════════════════════
# Helpers
# ═══════════════════════════════════════════════════════════════════════════════
def run_git(args):
"""Run a git command and return (returncode, stdout, stderr)."""
try:
result = subprocess.run(
["git"] + args,
capture_output=True,
text=True,
check=False,
)
return result.returncode, result.stdout, result.stderr
except FileNotFoundError:
print("ERROR: git not found. Is it installed?", file=sys.stderr)
sys.exit(1)
def get_commit_message(commit_ref):
"""Return the full commit message for *commit_ref*."""
rc, out, err = run_git(["log", "--format=%B", "-n", "1", commit_ref])
if rc != 0:
print(f"ERROR: could not read commit {commit_ref}: {err.strip()}", file=sys.stderr)
sys.exit(1)
if not out.strip():
print(f"ERROR: commit {commit_ref} has no message", file=sys.stderr)
sys.exit(1)
return out.rstrip("\n")
# ═══════════════════════════════════════════════════════════════════════════════
# Validators
# ═══════════════════════════════════════════════════════════════════════════════
def check_regex(message):
"""Check the commit message against the CI regex pattern."""
# Normalise: strip trailing newlines for single-line matching
first_line = message.split("\n")[0]
if MERGE_PATTERN.match(first_line):
return True, None
if COMMIT_PATTERN.match(first_line):
return True, None
return False, (
"Commit subject must match one of:\n"
" :emoji: <Capitalized subject without trailing dot>\n"
" Merge|Revert|Reapply <rest without trailing dot>\n"
f"Got: {first_line!r}"
)
def check_subject_length(message):
"""Subject line must be ≤ 90 characters."""
first_line = message.split("\n")[0]
if len(first_line) > 90:
return False, (
f"Subject line exceeds 90 characters ({len(first_line)} chars):\n"
f" {first_line}"
)
return True, None
def check_subject_no_trailing_dot(message):
"""Subject line must not end with a period ('.')."""
first_line = message.split("\n")[0]
if first_line.endswith("."):
return False, (
"Subject line must not end with a period:\n"
f" {first_line}"
)
return True, None
def check_subject_capitalized(message):
"""Subject must be capitalized, but only if it's a regular commit (not Merge/Revert/Reapply)."""
first_line = message.split("\n")[0]
# Skip check for Merge/Revert/Reapply commits
if MERGE_PATTERN.match(first_line):
return True, None
# Strip emoji prefix before checking capitalization
emoji_match = re.match(r"^:([a-z_]+):\s+(.*)", first_line)
if emoji_match:
rest = emoji_match.group(2)
else:
rest = first_line
if rest and not rest[0].isupper():
return False, (
"Subject line must start with a capital letter "
"(after the emoji prefix):\n"
f" {first_line}"
)
return True, None
def check_body_blank_line(message):
"""If a body exists, there must be a blank line between subject and body."""
lines = message.split("\n")
if len(lines) >= 3 and lines[1] != "":
return False, (
"A blank line must separate the subject from the body."
)
return True, None
def check_signed_off_by(message):
"""Check for the DCO Signed-off-by line (required for code changes)."""
if "Signed-off-by:" not in message:
return False, (
"Missing 'Signed-off-by:' line in the commit footer.\n"
" Add it with 'git commit -s' or append it manually:\n"
" Signed-off-by: Your Real Name <your.email@example.com>"
)
return True, None
# ═══════════════════════════════════════════════════════════════════════════════
def main():
parser = argparse.ArgumentParser(
description="Check a commit message against Penpot commit guidelines."
)
parser.add_argument(
"-c", "--commit",
default="HEAD",
help="Commit to check (default: HEAD)",
)
args = parser.parse_args()
commit_ref = args.commit
message = get_commit_message(commit_ref)
print(f"Checking commit {commit_ref} ...\n")
validators = [
("Regex pattern", check_regex),
("Subject ≤ 90 chars", check_subject_length),
("No trailing period in subject", check_subject_no_trailing_dot),
("Subject capitalized", check_subject_capitalized),
("Blank line after subject", check_body_blank_line),
]
all_ok = True
for name, validator in validators:
ok, error_msg = validator(message)
status = "✓" if ok else "✗"
print(f" [{status}] {name}")
if not ok:
all_ok = False
print(f" {error_msg}", file=sys.stderr)
print()
if all_ok:
print("All checks passed.")
sys.exit(0)
else:
print("Some checks FAILED. See messages above.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()