Last active
May 3, 2024 05:58
-
-
Save MichaelHolley/a9e5452e266ae929aeb025184f77f8d0 to your computer and use it in GitHub Desktop.
git-hook to include the ticket-number in the commit-message for feature- and fix-branches. Requiring the syntax of "feature/187-my-branch".
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/env python | |
import sys | |
import re | |
from subprocess import check_output | |
print("Starting commit message hook...") | |
supported_branch_types = ["fix", "feature"] | |
regex = '(?<=\/)[0-9]+' | |
commit_msg_filepath = sys.argv[1] | |
branch = check_output( | |
['git', 'symbolic-ref', '--short', 'HEAD']).strip().decode() | |
branch_type = branch.split('/')[0] | |
if branch_type in supported_branch_types: | |
regex_match = re.search(regex, branch) | |
if regex_match is not None: | |
ticket_number = regex_match.group(0) | |
print("using ticket-number:", ticket_number) | |
with open(commit_msg_filepath, 'r+') as file: | |
commit_msg = file.read() | |
if not commit_msg.startswith('%s - ' % (ticket_number)): | |
file.seek(0, 0) | |
file.write('%s - %s' % (ticket_number, commit_msg)) | |
else: | |
print("no ticket-number found in branch-name") | |
# raise Exception("it is required to include a ticket-number in branch-name") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment