diff options
author | lovetox <philipp@hoerist.com> | 2022-03-27 02:19:11 +0300 |
---|---|---|
committer | lovetox <philipp@hoerist.com> | 2022-03-27 02:19:42 +0300 |
commit | 7db8a0800509002f35593a50dc02c3a6eb778799 (patch) | |
tree | 1396f8df41786b0fb273ea193c8ebc86e4adde0c /.githooks | |
parent | b059dfb254aabd7b1f28c6a49641e39293f3de00 (diff) |
Add git update server-side hook
Diffstat (limited to '.githooks')
-rwxr-xr-x | .githooks/update | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/.githooks/update b/.githooks/update new file mode 100755 index 000000000..8e29fceed --- /dev/null +++ b/.githooks/update @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import sys +import subprocess + + +ALLOWED_TAGS = ['feat', 'fix', 'perf', 'refactor', 'chore', 'other'] + + +def parse_args() -> tuple[str, str]: + old_ref = sys.argv[2] + new_ref = sys.argv[3] + return old_ref, new_ref + + +def get_commit_subject(sha: str) -> str: + data = subprocess.check_output( + ['git', 'log', '-1', '--pretty=format:%s', sha]) + return data.decode() + + +def get_commit_shas(start_ref: str, end_ref: str) -> list[str]: + arg = f'{start_ref}..{end_ref}' + data = subprocess.check_output(['git', 'rev-list', arg]) + text = data.decode() + text = text.strip() + return text.split('\n') + + +def enforce_message_rules(subject: str) -> None: + tag, subject = subject.split(': ', maxsplit=1) + if tag not in ALLOWED_TAGS: + print('Unknown commit message tag:', tag) + sys.exit(1) + + if not subject[0].isupper(): + print('First letter after tag must be uppercase') + sys.exit(1) + + +def main(args: tuple[str, str]) -> None: + shas = get_commit_shas(*args) + for sha in shas: + subject = get_commit_subject(sha) + enforce_message_rules(subject) + + +if __name__ == "__main__": + args = parse_args() + main(args) |