2023-04-04 13:10:20 +00:00
|
|
|
#!/usr/bin/env python3
|
2024-09-13 17:22:05 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import re
|
2024-02-24 20:57:54 +00:00
|
|
|
from typing import Dict, Tuple, Callable, Optional
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
def call(cmd) -> str:
|
2024-09-15 00:43:15 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
ret = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
|
|
|
|
return ret.stdout
|
2023-04-04 13:10:20 +00:00
|
|
|
|
2024-02-24 20:57:54 +00:00
|
|
|
lint_rules: Dict[str, Tuple[Callable, str]] = {}
|
2023-04-04 13:10:20 +00:00
|
|
|
|
2024-09-23 17:49:01 +00:00
|
|
|
# A lint rule should return True if everything is okay
|
2023-04-04 13:10:20 +00:00
|
|
|
def lint_rule(description: str):
|
2024-09-15 00:43:15 +00:00
|
|
|
def f(func):
|
|
|
|
assert func.__name__ not in lint_rules.keys()
|
|
|
|
lint_rules[func.__name__] = (func, description)
|
|
|
|
return f
|
2023-04-04 13:10:20 +00:00
|
|
|
|
2024-02-24 20:57:54 +00:00
|
|
|
def get_commit_range() -> Optional[str]:
|
2024-09-15 00:43:15 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
return sys.argv[1]
|
|
|
|
# https://github.com/actions/runner/issues/342#issuecomment-590670059
|
|
|
|
event_name = os.environ["GITHUB_EVENT_NAME"]
|
|
|
|
with open(os.environ["GITHUB_EVENT_PATH"], "rb") as f:
|
|
|
|
event = json.load(f)
|
|
|
|
if event_name == "push":
|
|
|
|
if event["created"] or event["forced"]:
|
|
|
|
print("Skipping logic on branch creation or force-push")
|
|
|
|
return None
|
|
|
|
return event["before"] + "..." + event["after"]
|
|
|
|
elif event_name == "pull_request":
|
|
|
|
base = event["pull_request"]["base"]["sha"]
|
|
|
|
head = event["pull_request"]["head"]["sha"]
|
|
|
|
return f"{base}..{head}"
|
|
|
|
return None
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
def do_lint(commit_range: str) -> bool:
|
2024-09-15 00:43:15 +00:00
|
|
|
commits = call(["git", "log", "--pretty=format:%H %s", commit_range]).splitlines()
|
|
|
|
print(f"Linting {len(commits)} commit(s):")
|
|
|
|
any_failed = False
|
|
|
|
for commit in commits:
|
|
|
|
sha, _, _ = commit.partition(' ')
|
|
|
|
body = call(["git", "show", "-s", "--format=%B", sha]).splitlines()
|
|
|
|
failed = []
|
|
|
|
if len(body) == 0:
|
|
|
|
failed.append("* Commit message must not be empty")
|
|
|
|
else:
|
|
|
|
for k, v in lint_rules.items():
|
|
|
|
if not v[0](body):
|
|
|
|
failed.append(f"* {v[1]} [{k}]")
|
|
|
|
if failed:
|
|
|
|
any_failed = True
|
|
|
|
print("-" * 40)
|
|
|
|
sys.stdout.flush()
|
|
|
|
subprocess.run(["git", "-P", "show", "-s", sha])
|
|
|
|
print("\nhas the following issues:")
|
|
|
|
print("\n".join(failed))
|
|
|
|
print("-" * 40)
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
2024-09-15 00:43:15 +00:00
|
|
|
NO_PREFIX_WHITELIST = \
|
|
|
|
r"^Revert \"(.*)\"|^Reapply \"(.*)\"|^Release [0-9]|^Update MPV_VERSION$"
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("Subject line must contain a prefix identifying the sub system")
|
|
|
|
def subsystem_prefix(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return (re.search(NO_PREFIX_WHITELIST, body[0]) or
|
|
|
|
re.search(r"^[\w/\.{},-]+: ", body[0]))
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("First word after : must be lower case")
|
|
|
|
def description_lowercase(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
# Allow all caps for acronyms and options with --
|
|
|
|
return (re.search(NO_PREFIX_WHITELIST, body[0]) or
|
|
|
|
re.search(r": (?:[A-Z]{2,} |--[a-z]|[a-z0-9])", body[0]))
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("Subject line must not end with a full stop")
|
|
|
|
def no_dot(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return not body[0].rstrip().endswith('.')
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("There must be an empty line between subject and extended description")
|
|
|
|
def empty_line(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return len(body) == 1 or body[1].strip() == ""
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
# been seeing this one all over github lately, must be the webshits
|
|
|
|
@lint_rule("Do not use 'conventional commits' style")
|
|
|
|
def no_cc(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return not re.search(r"(?i)^(feat|fix|chore|refactor)[!:(]", body[0])
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("History must be linear, no merge commits")
|
|
|
|
def no_merge(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return not body[0].startswith("Merge ")
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
@lint_rule("Subject line should be shorter than 72 characters")
|
|
|
|
def line_too_long(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
revert = re.search(r"^Revert \"(.*)\"|^Reapply \"(.*)\"", body[0])
|
|
|
|
return revert or len(body[0]) <= 72
|
2023-04-04 13:10:20 +00:00
|
|
|
|
2024-09-15 00:43:15 +00:00
|
|
|
@lint_rule(
|
|
|
|
"Prefix should not include file extension (use `vo_gpu: ...` not `vo_gpu.c: ...`)"
|
|
|
|
)
|
2023-04-04 13:10:20 +00:00
|
|
|
def no_file_exts(body):
|
2024-09-15 00:43:15 +00:00
|
|
|
return not re.search(r"[a-z0-9]\.([chm]|cpp|swift|py): ", body[0])
|
2023-04-04 13:10:20 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-09-15 00:43:15 +00:00
|
|
|
commit_range = get_commit_range()
|
|
|
|
if commit_range is None:
|
|
|
|
exit(0)
|
|
|
|
print("Commit range:", commit_range)
|
|
|
|
any_failed = do_lint(commit_range)
|
|
|
|
exit(1 if any_failed else 0)
|