Skip to content

Instantly share code, notes, and snippets.

@hockeybuggy
Last active August 29, 2015 14:09
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 hockeybuggy/3f5a02c05f599b32085f to your computer and use it in GitHub Desktop.
Save hockeybuggy/3f5a02c05f599b32085f to your computer and use it in GitHub Desktop.
Ensures that commit messages start with "VA-XXXX"
#!/usr/bin/env python
# This commit-msg git hook takes a commit message as it first argument.
# It's purpose is to ensure that the message starts with "VA-XXXX"
import sys
import re
pattern = r"^[Vv][Aa]-\d+[\s\.]"
def print_commit_message(commit_message):
print ">>> START MESSAGE >>>"
print commit_message
print "<<< END MESSAGE <<< \n"
def get_choice():
sys.stdin = open("/dev/tty") # This allows raw_input in a hook
choice_str = raw_input("Are you sure you want this message? [y/(n)]: ")
sys.stdin.close()
return choice_str
def yes(choice_str):
return (re.compile("^[Yy]").match(choice_str) != None)
def use_message():
print "Using the message."
sys.exit(0)
def ditch_message():
print "Ditching the message."
sys.exit(1)
def main():
commit_message = open(sys.argv[1]).read()
match = re.compile(pattern).match(commit_message)
if not match:
print "The commit message does not start with: VA-XXXX"
print_commit_message(commit_message)
choice_str = get_choice()
if yes(choice_str):
use_message()
ditch_message()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment