-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck_commits.py
More file actions
196 lines (163 loc) · 5.92 KB
/
check_commits.py
File metadata and controls
196 lines (163 loc) · 5.92 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
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear
import os
import sys
import argparse
import subprocess
TRAILER_PREFIXES = (
"signed-off-by:",
"co-authored-by:",
"co-developed-by:",
"reviewed-by:",
"acked-by:",
"tested-by:",
)
def parse_arguments():
parser = argparse.ArgumentParser(
description="Validate commit messages using local Git history (no API/token)."
)
parser.add_argument(
"--base", required=True, help="Base SHA for the range (base..head)"
)
parser.add_argument(
"--head", required=True, help="Head SHA for the range (or single ref)"
)
parser.add_argument("--body-limit", type=int, default=72)
parser.add_argument("--sub-limit", type=int, default=50)
parser.add_argument("--check-blank-line", type=str, default="true")
parser.add_argument("--strict-line-length-check", type=str, default="true")
return parser.parse_args()
def fetch_commits(base, head):
"""
Fetch commits between base..head from local repository.
Returns a list of dicts with 'sha' and 'message' keys.
"""
if not head:
print("::error::Tokenless mode requires --head (and usually --base).")
sys.exit(2)
rev_range = f"{base}..{head}" if base else head
try:
shas = (
subprocess.check_output(
["git", "rev-list", "--no-merges", rev_range], text=True
)
.strip()
.splitlines()
)
if not shas:
return []
output = subprocess.check_output(
["git", "show", "-s", "--format=%H%x00%B%x00"] + shas, text=True
)
commits = []
parts = output.split("\x00")
for i in range(0, len(parts) - 1, 2):
sha = parts[i].strip()
message = parts[i + 1] if i + 1 < len(parts) else ""
if sha:
commits.append({"sha": sha, "message": message})
return commits
except subprocess.CalledProcessError as e:
print(f"::error::Failed to fetch commits with git: {e}")
sys.exit(2)
def validate_subject(subject, sub_char_limit):
"""Validate the commit subject line."""
errors = []
if len(subject.strip()) == 0:
errors.append("Commit message is missing subject!")
if len(subject) > sub_char_limit:
errors.append(f"Subject exceeds {sub_char_limit} characters!")
return errors
def validate_body(
lines, n, body_char_limit, check_blank_line, strict_line_length_check
):
"""Validate the commit body."""
errors = []
body_index = 1
if check_blank_line.lower() == "true":
# Check for blank line after subject
if n > 1 and lines[1].strip() != "":
errors.append("Subject and body must be separated by a blank line")
body_index = 2
body = [
line.strip()
for line in lines[body_index:n]
if line.strip() and not line.lower().startswith(TRAILER_PREFIXES)
]
if len(body) == 0:
errors.append("Commit message is missing a body!")
for line in body:
if len(line) > body_char_limit:
if strict_line_length_check.lower() == "true":
errors.append(f"Line exceeds {body_char_limit} characters: {line}")
else:
index_of_last_space = line.rfind(" ")
if index_of_last_space >= body_char_limit:
errors.append(f"Line exceeds {body_char_limit} characters: {line}")
return errors, body
def validate_trailers(lines, body, check_blank_line):
errors = []
trailer_indices = [
i for i, line in enumerate(lines) if line.lower().startswith(TRAILER_PREFIXES)
]
first_trailer_index = 0
if trailer_indices:
first_trailer_index = trailer_indices[0]
if check_blank_line.lower() == "true" and body:
if first_trailer_index > 0 and lines[first_trailer_index - 1].strip() != "":
errors.append("Body and trailers must be separated by a blank line")
return errors
def validate_commit_message(
commit, sub_char_limit, body_char_limit, check_blank_line, strict_line_length_check
):
sha = commit["sha"]
message = commit["message"]
lines = message.splitlines()
n = len(lines)
subject = lines[0] if n >= 1 else ""
errors = []
subject_errors = validate_subject(subject, sub_char_limit)
body_errors, body = validate_body(
lines, n, body_char_limit, check_blank_line, strict_line_length_check
)
trailer_errors = validate_trailers(lines, body, check_blank_line)
errors.extend(subject_errors + body_errors + trailer_errors)
return sha, errors
def process_commits(
commits, sub_limit, body_limit, check_blank_line, strict_line_length_check
):
failed_count = 0
for commit in commits:
sha, errors = validate_commit_message(
commit, sub_limit, body_limit, check_blank_line, strict_line_length_check
)
if errors:
print(f"::group:: ❌ Errors in commit {sha}")
failed_count += 1
for err in errors:
print(f"::error:: {err}")
print("::endgroup::")
else:
print(f"✅ Commit {sha} passed all checks.")
return failed_count
def main():
args = parse_arguments()
commits = fetch_commits(args.base, args.head)
failed_count = process_commits(
commits,
args.sub_limit,
args.body_limit,
args.check_blank_line,
args.strict_line_length_check,
)
summary_path = os.getenv("GITHUB_STEP_SUMMARY")
if summary_path:
with open(summary_path, "a") as f:
f.write("### Commit Validation Summary\n")
if failed_count:
f.write(f"- ❌ {failed_count} commit(s) failed validation.\n")
else:
f.write("- ✅ All commits passed validation.\n")
sys.exit(1 if failed_count else 0)
if __name__ == "__main__":
main()