Skip to content

Instantly share code, notes, and snippets.

@stucox
Forked from igniteflow/prepare-commit-msg
Created November 29, 2013 10:39
Show Gist options
  • Save stucox/7704057 to your computer and use it in GitHub Desktop.
Save stucox/7704057 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
To enable save as /yourproject/.git/hooks/prepare-commit-msg and make executable: chmod +rx prepare-commit-msg
"""
import sys
from subprocess import check_output
def get_ticket_number():
branch_name = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
return branch_name.split('-')[1]
def prepend_to_file(marker):
with open(sys.argv[1], 'r') as message_file:
lines = message_file.readlines()
lines[0] = marker + lines[0]
with open(sys.argv[1], 'w') as message_file:
message_file.write(''.join(lines))
if __name__ == '__main__':
marker = '\n\n#[touch:%s]' % get_ticket_number()
prepend_to_file(marker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment