Last active
August 29, 2015 14:09
-
-
Save hockeybuggy/3f5a02c05f599b32085f to your computer and use it in GitHub Desktop.
Ensures that commit messages start with "VA-XXXX"
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 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