Skip to content

Instantly share code, notes, and snippets.

@rr-
Last active April 9, 2020 16:14
Show Gist options
  • Save rr-/855b77b807829e30b62fe6f6460952aa to your computer and use it in GitHub Desktop.
Save rr-/855b77b807829e30b62fe6f6460952aa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# copy me into $repository_root/.git/hooks/
import os
import sys
import typing as T
from pathlib import Path
from subprocess import PIPE, run
BRANCHES_TO_SKIP: T.List[str] = os.environ.get(
"BRANCHES_TO_SKIP", "master staging develop test production"
).split()
def main() -> None:
branch_name = (
run(["git", "symbolic-ref", "--short", "HEAD"], stdout=PIPE)
.stdout.decode()
.strip()
)
if not branch_name:
try:
branch_name = (
Path(".git/rebase-merge/head-name")
.read_text()
.strip()
.split("/")[-1]
)
except FileNotFoundError:
branch_name = ""
if branch_name in BRANCHES_TO_SKIP:
return
branch_name = branch_name.replace("_", "-")
branch_name = branch_name.upper()
if branch_name in BRANCHES_TO_SKIP:
return
path_to_commit_msg = Path(sys.argv[1])
commit_msg = path_to_commit_msg.read_text()
if commit_msg.startswith(branch_name):
return
if commit_msg.startswith("fixup! ") or commit_msg.startswith("squash! "):
return
path_to_commit_msg.write_text(f"{branch_name}: {commit_msg}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment