#!/usr/bin/env python
'''
this commit message hook ensures that a commit
message conforms to the following  commit message
guidlines:
  -ticket should be references in the first line (eg WEB-23)
  -2nd line should be blank
  -3rd line should not be empty
 
Commit message should be at least 3 lines long.

Exceptions to this include the default message
for merge commits, which are to be left in tact
''' 


import sys
import re

if len(sys.argv)!=2 :
    sys.exit(0)

f = open(sys.argv[1], "r")
lines = f.readlines()
f.close()

mergeCommit = re.compile(r"^Merge branch '")
# is this a merge commit? if so, dont do anything else
if mergeCommit.match(lines[0]) is not None:
    sys.exit(0)

# first line contains a ticket number
validFirstLine = re.compile(r".+[A-Z]{1,6}-[0-9]{1,5}.+")
elif validFirstLine.match(lines[0]) is None:
    print "The first line of the commit message must reference a JIRA ticket. ABORTING COMMIT!!!"
    sys.exit(1)

# second line is empty
if lines[1] != "\n" :
    print "The second line of the commit message must be an empty space. ABORTING COMMIT!!!"
    sys.exit(1)

#third is non-empty
validThirdLine = re.compile(r"^(#|\n)")
if validThirdLine.match(lines[2]) is not None:
    print "The third line of the commit message must contain more in-depth comments. ABORTING COMMIT!!!"
    sys.exit(1)

sys.exit(0)
