Last active
January 25, 2016 16:38
-
-
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).
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 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