Created
July 10, 2013 10:19
-
-
Save iamsilvio/5965206 to your computer and use it in GitHub Desktop.
git commit-msg hook to reject bad formated messages
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 | |
# -*- coding: utf-8 -*- | |
import sys | |
import re | |
def checkMessage(msg): | |
""" | |
Checks if the message content matches one of the message rules | |
B: <message> | |
R: <message> | |
F: <message> | |
S: <message> | |
Merge | |
Added tag v\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} for changeset \.{12} | |
""" | |
message_pattern = re.compile( | |
"^((B|F|S|R)(:\s)(.)*)|" | |
"^(Merge)$|" | |
"^(Added tag v\d{1,3}\.\d{1,3}\.\d{1,5}\.\d{1,5} for changeset .{12})$|" | |
"^(Removed tag v\d{1,3}\.\d{1,3}\.\d{1,5}\.\d{1,5})$", | |
re.IGNORECASE) | |
if message_pattern.match(msg): | |
return False | |
return True | |
def printUsage(msg, lineNumber): | |
""" | |
Print all allowed message formats | |
Added tag v1.4.3.6 for changeset 91ed85071cd4 | |
Removed tag v1.4.3.6 | |
""" | |
valid_messages = {'B: <message>': 'Bug', 'F: <message>': 'Feature', | |
'R: <message>': 'Refactoring', | |
'S: <message>': 'Specification'} | |
out = 'Your commit message: %s in line %d is not valid \n' \ | |
'the following message formats are allowed\n\n' % (msg, lineNumber + 1) | |
out += '{0:20} {1:20}\n'.format('Message Format', 'Description') | |
for msg, desc in valid_messages.items(): | |
out += '{0:20} = {1:20}\n'.format(msg, desc) | |
out += '{0:20} = {1:20}\n'.format('Merge', 'Merge') | |
print(out + '\n') | |
return | |
def main(): | |
lines = [] | |
with open(sys.argv[1], 'r') as msgfile: | |
lines = msgfile.readlines() | |
for i, line in enumerate(lines): | |
invalid = checkMessage(line) | |
if invalid: | |
printUsage(line, i) | |
sys.exit(1) | |
sys.exit(0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Git commit-msg hook
What is this script about?
This script checks every commit message before you commit your changes. The pattern that I use on commit messages looks like this
Setup global git hook
Unfortunately it's not as easy as on mercurial but here we go.
make a directory where you like to have it, I placed mine under my home folder
~\.git_template\hooks\
on windows~/.git_template/hooks/
on unix basednow let's tell git to take this folder as template folder by running the following command in a shell
git config --global init.templatedir ~\.git_template
on windowsgit config --global init.templatedir ~/.git_template
on unix based~\.git_template\hooks\
folderon Unix based systems use
~/.git_template/hooks/
git init
to your shell at your target locationPossible fault
chmod a+x [commit-msg]
env: python\r: No such file or directory
on Unix based systems you have to run dos2unix on the script file