Coming from CVS, we have a policy that commit messages should be tagged with a bug number (simple suffix \"... [9999]\"). A CVS script checks this during commits and rejects
This is a python version of pre-receive
, which took me a while to finish, hope it could help others. I mainly use it with Trac, but it could be easily modified for other purposes.
I have also put down the instructions to modify back the historical commit message, which is a little more complicated than I thought.
#!/usr/bin/env python
import subprocess
import sys
import re
def main():
input = sys.stdin.read()
oldrev, newrev, refname = input.split(" ")
separator = "----****----"
proc = subprocess.Popen(["git", "log", "--format=%H%n%ci%n%s%b%n" + separator, oldrev + ".." + newrev], stdout=subprocess.PIPE)
message = proc.stdout.read()
commit_list = message.strip().split(separator)[:-1] #discard the last line
is_valid = True
print "Parsing message:"
print message
for commit in commit_list:
line_list = commit.strip().split("\n")
hash = line_list[0]
date = line_list[1]
content = " ".join(line_list[2:])
if not re.findall("refs *#[0-9]+", content): #check for keyword
is_valid = False
if not is_valid:
print "Please hook a trac ticket when commiting the source code!!!"
print "Use this command to change commit message (one commit at a time): "
print "1. run: git rebase --interactive " + oldrev + "^"
print "2. In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify"
print "3. run: git commit --amend"
print "4. modify the commit message"
print "5. run: git rebase --continue"
print "6. remember to add the ticket number next time!"
print "reference: http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit"
sys.exit(1)
main()