Skip to content

Instantly share code, notes, and snippets.

@szobov
Created March 6, 2023 10:32
Show Gist options
  • Save szobov/885f501284d27b95d95635938b4606fe to your computer and use it in GitHub Desktop.
Save szobov/885f501284d27b95d95635938b4606fe to your computer and use it in GitHub Desktop.
#!/home/szobov/.nix-profile/bin/python3
import re
import sys
from subprocess import check_output, CalledProcessError
JIRA_PROJECT_PREFIX = 'JIRA-'
def append_branch_number(prefix):
"""
Add a substring with the task number to commit the message if necessary.
Will added if:
* Branch starts with a prefix
* Constructed substring wasn't added previously.
"""
commit_msg_filepath = sys.argv[1]
try:
branch = check_output(
['git', 'symbolic-ref', '--short', 'HEAD']).strip().decode('utf-8')
except CalledProcessError:
print('Can not get branch name. Probably, you are doing rabase.')
return None
result = re.match('(?P<number>[0-9]*)-', branch)
if result is None:
return None
if not result.groupdict().get('number'):
return None
task_number = '{}{}'.format(
JIRA_PROJECT_PREFIX, result.groupdict().get('number'))
with open(commit_msg_filepath, 'r+') as f:
content = f.read()
if task_number in content.split('\n')[0]:
return None
f.seek(0, 0)
f.write("[%s] %s" % (task_number, content))
f.seek(len('[%s]' % (task_number, )), 0)
if __name__ == '__main__':
append_branch_number(JIRA_PROJECT_PREFIX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment