Skip to content

Instantly share code, notes, and snippets.

@iamsilvio
Created July 10, 2013 10:19
Show Gist options
  • Save iamsilvio/5965206 to your computer and use it in GitHub Desktop.
Save iamsilvio/5965206 to your computer and use it in GitHub Desktop.
git commit-msg hook to reject bad formated messages
#!/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()
@iamsilvio
Copy link
Author

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

  • B: for a Bug
  • F: for a Feature
  • R: for Refactoring
  • S: for a Specification
  • and Merge for Merges

Setup global git hook

Unfortunately it's not as easy as on mercurial but here we go.

  1. you have to setup a custom template directory for git
    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 based
    now 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 windows
    git config --global init.templatedir ~/.git_template on unix based
  2. place the script 'commit-msg' in the ~\.git_template\hooks\ folder
    on Unix based systems use ~/.git_template/hooks/
  3. init a new or reinit a old git repository by putting git init to your shell at your target location
  4. there is no 4. point!

Possible fault

  • do not add a file extension to the file
  • make sure the file is executable on unix based systems chmod a+x [commit-msg]
  • if you get a env: python\r: No such file or directory on Unix based systems you have to run dos2unix on the script file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment