Created
June 21, 2022 21:20
-
-
Save x1unix/b46631fa594152c6d53372735d9e68c6 to your computer and use it in GitHub Desktop.
Git flow commit message generator
This file contains hidden or 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/env python3 | |
# brew install pygit2 | |
# pip3 install pygit2 | |
from pygit2 import Repository | |
import sys | |
import re | |
regex = r"^([a-z]{1,5})/([A-Z]{1,5}-[0-9]{1,4})" | |
def main(): | |
repo = Repository('.') | |
branch_name = repo.head.shorthand | |
message = sys.argv[1:] | |
if not message: | |
raise Exception(f'Empty commit message. Usage: {sys.argv[0]} commit message...') | |
message = ' '.join(message) | |
pattern = re.compile(regex) | |
if not pattern.match(branch_name): | |
raise Exception(f'Branch name "{branch_name}" is not valid!') | |
branch_type, jira_ticket = pattern.findall(branch_name)[0] | |
status: Dict[str, int] = repo.status() | |
if not status: | |
raise Exception("Tree is empty, nothing to commit") | |
commit_msg = f"{branch_type}({jira_ticket}): {message}" | |
tree = repo.index.write_tree() | |
parent, ref = repo.resolve_refish(refish=repo.head.name) | |
repo.create_commit( | |
ref.name, | |
repo.default_signature, | |
repo.default_signature, | |
commit_msg, | |
tree, | |
[parent.oid], | |
) | |
print(f'Created commit: "{commit_msg}"') | |
try: | |
main() | |
except Exception as err: | |
print(f'Error: {err}') | |
sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment