Skip to content

Instantly share code, notes, and snippets.

@Teyik0
Created July 3, 2025 07:14
Show Gist options
  • Select an option

  • Save Teyik0/cccb9f91577ed355075bc0d5f0ab2e83 to your computer and use it in GitHub Desktop.

Select an option

Save Teyik0/cccb9f91577ed355075bc0d5f0ab2e83 to your computer and use it in GitHub Desktop.
commit_lint python quick script
def commit_lint() -> None:
commit_msg_file = None
if len(sys.argv) > 1:
commit_msg_file = sys.argv[1]
if commit_msg_file:
from pathlib import Path
commit_msg = Path(commit_msg_file).read_text(encoding="utf-8").strip()
else:
commit_msg = sys.stdin.read().strip()
first_line = commit_msg.split("\n")[0].strip()
if not first_line:
print("ERROR: Empty commit message")
sys.exit(1)
print(f"Validating commit message: {first_line}")
if first_line.startswith(("Merge ", "Revert ")):
print("Skipping validation for merge/revert commit")
return
pattern = r"^(feat|fix|docs|style|refactor|test|chore|perf|ci|build)(\([^)]+\))?!?: .+"
if not re.match(pattern, first_line):
print("")
print("ERROR: Invalid commit message format!")
print("=" * 60)
print("Expected format: type(scope): description")
print("")
print("Valid types:")
print(" feat, fix, docs, style, refactor, test, chore, perf, ci, build")
print("")
print("Examples:")
print(" feat: add user authentication system")
print(" fix(auth): resolve login timeout issue")
print(" docs: update API documentation")
print("")
print(f"Your commit message: '{first_line}'")
print("=" * 60)
sys.exit(1)
description = first_line.split(":", 1)[1].strip()
if len(description) < 5:
print("")
print("ERROR: Commit description too short!")
print("Description must be at least 5 characters long")
print(f"Your description: '{description}' ({len(description)} characters)")
sys.exit(1)
print("SUCCESS: Commit message is valid!")
type_match = re.match(r"^([^(:!]+)", first_line)
if type_match:
commit_type = type_match.group(1)
print(f" Type: {commit_type}")
if "!:" in first_line:
print(" WARNING: Breaking change detected")
scope_match = re.search(r"\(([^)]+)\)", first_line)
if scope_match:
scope = scope_match.group(1)
print(f" Scope: {scope}")
print(f" Description: {description}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment