Skip to content

Instantly share code, notes, and snippets.

@d30jeff
Created August 14, 2018 18:07
Show Gist options
  • Save d30jeff/20b80d778fba06784307e53e6baba835 to your computer and use it in GitHub Desktop.
Save d30jeff/20b80d778fba06784307e53e6baba835 to your computer and use it in GitHub Desktop.
Append branch name to commit message to make life easier
#!/usr/bin/env python
import sys, os, re
from subprocess import check_output
file_path = sys.argv[1]
# Get branch name
branch_name = check_output(['git', 'symbolic-ref', '--short', '-q', 'HEAD']).strip()
# Initialize prefix name
prefix = ""
# Strip each element that has been separated by '/' and cast to uppercase if it's not empty
chunk = [s.strip().upper() for s in branch_name.split('/') if s]
if len(chunk) > 1:
prefix = '/'.join(chunk[1:])
with open(file_path, 'r+') as f:
content = f.read()
f.seek(0, 0)
if prefix:
f.write(': '.join([prefix, content]))
else:
f.write(content)
@d30jeff
Copy link
Author

d30jeff commented Aug 14, 2018

Branch name => Commit prefix

  1. story/ticket-12 => TICKET-12: <commit-message>
  2. story/poop/ticket-13 => POOP/TICKET-13: <commit-message>
  3. bug/ticket-13 => TICKET-13: <commit-message>
  4. regular-branch-name => <commit-message>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment