File: lint-commit-msg.py

package info (click to toggle)
mpv 0.40.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,676 kB
  • sloc: ansic: 152,062; python: 1,228; sh: 646; javascript: 612; cpp: 461; objc: 302; pascal: 49; xml: 29; makefile: 19
file content (119 lines) | stat: -rwxr-xr-x 4,178 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
from collections.abc import Callable
from typing import Optional


def call(cmd) -> str:
    sys.stdout.flush()
    ret = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
    return ret.stdout

lint_rules: dict[str, tuple[Callable, str]] = {}

# A lint rule should return True if everything is okay
def lint_rule(description: str):
    def f(func):
        assert func.__name__ not in lint_rules
        lint_rules[func.__name__] = (func, description)
    return f

def get_commit_range() -> Optional[str]:
    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

def do_lint(commit_range: str) -> bool:
    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)
    return any_failed

################################################################################

NO_PREFIX_WHITELIST = \
    r"^Revert \"(.*)\"|^Reapply \"(.*)\"|^Release [0-9]|^Update MPV_VERSION$"

@lint_rule("Subject line must contain a prefix identifying the sub system")
def subsystem_prefix(body):
    return (re.search(NO_PREFIX_WHITELIST, body[0]) or
            re.search(r"^[\w/\.{},-]+: ", body[0]))

@lint_rule("First word after : must be lower case")
def description_lowercase(body):
    # 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]))

@lint_rule("Subject line must not end with a full stop")
def no_dot(body):
    return not body[0].rstrip().endswith(".")

@lint_rule("There must be an empty line between subject and extended description")
def empty_line(body):
    return len(body) == 1 or body[1].strip() == ""

# been seeing this one all over github lately, must be the webshits
@lint_rule("Do not use 'conventional commits' style")
def no_cc(body):
    return not re.search(r"(?i)^(feat|fix|chore|refactor)[!:(]", body[0])

@lint_rule("History must be linear, no merge commits")
def no_merge(body):
    return not body[0].startswith("Merge ")

@lint_rule("Subject line should be shorter than 72 characters")
def line_too_long(body):
    revert = re.search(r"^Revert \"(.*)\"|^Reapply \"(.*)\"", body[0])
    return revert or len(body[0]) <= 72

@lint_rule(
    "Prefix should not include file extension (use `vo_gpu: ...` not `vo_gpu.c: ...`)",
)
def no_file_exts(body):
    return not re.search(r"[a-z0-9]\.([chm]|cpp|swift|rst): ", body[0])

################################################################################

if __name__ == "__main__":
    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)