|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Validates commit messages according to Sentry's commit message guidelines. |
| 4 | +https://develop.sentry.dev/engineering-practices/commit-messages/ |
| 5 | +""" |
| 6 | + |
| 7 | +import re |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def validate_commit_message(message): |
| 12 | + """ |
| 13 | + Validates a commit message according to Sentry's format: |
| 14 | + <type>(<scope>): <subject> |
| 15 | +
|
| 16 | + Returns tuple of (is_valid, error_message) |
| 17 | + """ |
| 18 | + # Valid commit types |
| 19 | + valid_types = [ |
| 20 | + "build", |
| 21 | + "ci", |
| 22 | + "docs", |
| 23 | + "feat", |
| 24 | + "fix", |
| 25 | + "perf", |
| 26 | + "ref", |
| 27 | + "style", |
| 28 | + "test", |
| 29 | + "meta", |
| 30 | + "license", |
| 31 | + "revert", |
| 32 | + ] |
| 33 | + |
| 34 | + # Skip validation for merge commits |
| 35 | + if message.startswith("Merge"): |
| 36 | + return True, None |
| 37 | + |
| 38 | + # Parse the first line (header) |
| 39 | + lines = message.strip().split("\n") |
| 40 | + |
| 41 | + header = lines[0].strip() |
| 42 | + |
| 43 | + # Pattern for the header: type(scope): subject or type: subject |
| 44 | + pattern = r"^(?P<type>[a-z]+)(?:\((?P<scope>[^)]+)\))?: (?P<subject>.+)$" |
| 45 | + match = re.match(pattern, header) |
| 46 | + |
| 47 | + if not match: |
| 48 | + return ( |
| 49 | + False, |
| 50 | + "Invalid format. Must be: <type>(<scope>): <subject> or <type>: <subject>", |
| 51 | + ) |
| 52 | + |
| 53 | + commit_type = match.group("type") |
| 54 | + scope = match.group("scope") |
| 55 | + subject = match.group("subject") |
| 56 | + |
| 57 | + # Validate type |
| 58 | + if commit_type not in valid_types: |
| 59 | + return ( |
| 60 | + False, |
| 61 | + f"Invalid type '{commit_type}'. Must be one of: {', '.join(valid_types)}", |
| 62 | + ) |
| 63 | + |
| 64 | + # Validate scope (if present) |
| 65 | + if scope and not scope.islower(): |
| 66 | + return False, f"Scope '{scope}' must be lowercase" |
| 67 | + |
| 68 | + # Validate subject |
| 69 | + if not subject: |
| 70 | + return False, "Subject cannot be empty" |
| 71 | + |
| 72 | + # Check first letter is capitalized |
| 73 | + if subject[0].islower(): |
| 74 | + return False, "Subject must start with a capital letter" |
| 75 | + |
| 76 | + # Check for trailing period |
| 77 | + if subject.endswith("."): |
| 78 | + return False, "Subject must not end with a period" |
| 79 | + |
| 80 | + # Check header length (max 70 characters) |
| 81 | + if len(header) > 70: |
| 82 | + return False, f"Header is {len(header)} characters, must be 70 or less" |
| 83 | + |
| 84 | + return True, None |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + """Main entry point for the commit message validator.""" |
| 89 | + # Read commit message from file (provided by git) |
| 90 | + if len(sys.argv) < 2: |
| 91 | + print("Error: No commit message file provided") |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + commit_msg_file = sys.argv[1] |
| 95 | + |
| 96 | + try: |
| 97 | + with open(commit_msg_file, "r", encoding="utf-8") as f: |
| 98 | + commit_message = f.read() |
| 99 | + except Exception as e: |
| 100 | + print(f"Error reading commit message file: {e}") |
| 101 | + sys.exit(1) |
| 102 | + |
| 103 | + # Validate the commit message |
| 104 | + is_valid, error_msg = validate_commit_message(commit_message) |
| 105 | + |
| 106 | + if not is_valid: |
| 107 | + print(f"❌ Commit message validation failed:\n{error_msg}") |
| 108 | + print("\nCommit message format: <type>(<scope>): <subject>") |
| 109 | + print("Example: feat(api): Add new authentication endpoint") |
| 110 | + print( |
| 111 | + "\nSee https://develop.sentry.dev/engineering-practices/commit-messages/ for details" |
| 112 | + ) |
| 113 | + sys.exit(1) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments