Skip to content

Instantly share code, notes, and snippets.

@warvariuc
Last active January 25, 2016 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warvariuc/5697861 to your computer and use it in GitHub Desktop.
Save warvariuc/5697861 to your computer and use it in GitHub Desktop.
A hook for git that inserts at the beginning of the commit message name of the current branch, if the message is empty (contains empty or comment lines).
#!/usr/bin/env python3
import sys
import subprocess
with open(sys.argv[1], 'r+') as commit_message_file:
commit_message = list(commit_message_file)
for line in commit_message:
line = line.strip()
if line and not line.startswith('#'): # comment line
break # a non-empty line
else: # empty message
try:
branch_name = subprocess.check_output('git symbolic-ref --short HEAD', shell=True).strip().decode('utf-8')
except subprocess.CalledProcessError:
pass
else:
commit_message.insert(0, branch_name + ' ')
commit_message_file.seek(0)
for line in commit_message:
commit_message_file.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment