Created
October 16, 2020 15:40
-
-
Save horpto/ace4d991b1112c917424962d4f0479e6 to your computer and use it in GitHub Desktop.
Adding automatic commit message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
import sys | |
NOTICKET_PREFIX = 'NOTICKET' | |
ISSUE_PREFIX = 'ZVN' | |
TICKET_BRANCH = re.compile(f'f/({ISSUE_PREFIX}-\d+).*?', re.I) | |
MESSAGE_RE = re.compile(f'((f/)?({ISSUE_PREFIX}-\d+)|{NOTICKET_PREFIX}) .+?', re.I) | |
def read_line(filename): | |
with open(filename) as f: | |
head = f.readline().strip() | |
return head | |
def current_branch(): | |
head = read_line('.git/HEAD') | |
short_head = head.replace('ref: refs/heads/', '') | |
return short_head | |
def main(): | |
commit_msg_filename = sys.argv[1] | |
commit_source = sys.argv[2] if len(sys.argv) > 2 else None | |
sha1 = sys.argv[3] if len(sys.argv) > 3 else None | |
if sha1 or commit_source in ('merge', 'squash', 'template'): | |
return | |
branch_name = current_branch() | |
match = TICKET_BRANCH.match(branch_name) | |
ticket_prefix = match.group(1).upper() if match else NOTICKET_PREFIX | |
with open(commit_msg_filename, 'r+') as f: | |
lines = f.readlines() or [''] | |
first_line = lines[0].strip() | |
if MESSAGE_RE.match(first_line): | |
return | |
lines[0] = f'{ticket_prefix} {first_line}\n' | |
f.truncate(0) | |
f.seek(0) | |
f.writelines(lines) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment