Skip to content

Instantly share code, notes, and snippets.

@tristan0x
Last active August 7, 2017 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristan0x/0bf6ef017e1e738b344e to your computer and use it in GitHub Desktop.
Save tristan0x/0bf6ef017e1e738b344e to your computer and use it in GitHub Desktop.
Git commit client hook

Git commit client hook

Provides Git hook enforcing commit message.

  • first line length must be less than 50 characters.
  • if applicable, second line may be empty.
  • length of every lines may be less than 72 characters.

Installation instructions

Setup hook for all repositories

Install git 2.9 or higher. Run the commands below:

cd /path/to/all/your/gists
git clone git@gist.github.com:0bf6ef017e1e738b344e.git
git config --global core.hooksPath $PWD

Setup hook in a specific repository

In terminal:

  1. Go to a Git repository root:
cd /path/to/my/git/repository
  1. Run the following command:
curl -s https://gist.githubusercontent.com/tristan0x/0bf6ef017e1e738b344e/raw/d71a6a0eb2cbebe2842fda86259593824110dda6/setup-commit-msg-hook | sh -e -

Example

$ git init /tmp/foo
$ cd /tmp/foo
$ curl -s https://gist.githubusercontent.com/tristan0x/0bf6ef017e1e738b344e/raw/d71a6a0eb2cbebe2842fda86259593824110dda6/setup-commit-msg-hook | sh -e -
# adding git/hooks/commit-msg
# adding git/hooks/validate-commit.py
$

Hint for effectiveness

You can add an alias in your shell configuration file:

alias git-setup-cogniteev-hook='curl -s https://gist.githubusercontent.com/tristan0x/0bf6ef017e1e738b344e/raw/d71a6a0eb2cbebe2842fda86259593824110dda6/setup-commit-msg-hook | sh -e -'

Resources

#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
exec < /dev/tty
"$DIR/validate-commit.py" $1
#!/bin/sh -e
gist_user=tristan0x
gist_id=0bf6ef017e1e738b344e
revision="8da1eb6e8deb6ae15963178945136e1b1c82c9ba"
gist_root="https://gist.githubusercontent.com/$gist_user/$gist_id/raw"
has_error=no
if ! [ -d .git/hooks ] ; then
echo "Error: cannot find directory .git/hooks" >&2
has_error=true
fi
for f in commit-msg validate-commit.py ; do
if [ -f ".git/hooks/$f" ] ; then
echo "Error: .git/hooks/$f already exists." >&2
has_error=true
fi
done
if [ "x$has_error" = xtrue ] ;then
echo "Abort." >&2
exit 1
fi
for f in commit-msg validate-commit.py ; do
echo "# adding git/hooks/$f"
curl -s "$gist_root/$revision/$f" > ".git/hooks/$f"
chmod +x ".git/hooks/$f"
done
#!/usr/bin/python
import sys
import os
from subprocess import call
editor = os.environ.get('EDITOR', 'vim')
message_file = sys.argv[1]
def check_format_rules(lineno, line):
real_lineno = lineno + 1
if lineno == 0:
if len(line) > 50:
return 'Error %d: First line should be less than 50 characters ' \
'in length.' % (real_lineno,)
if lineno == 1:
if line:
return 'Error %d: Second line should be empty.' % (real_lineno,)
if len(line) > 72:
return 'Error %d: No line should be over 72 characters long.' % (
real_lineno,)
return False
while True:
commit_msg = list()
errors = list()
with open(message_file) as commit_fd:
lineno = 0
for line in commit_fd:
is_comment = line.startswith('#')
stripped_line = line.strip()
commit_msg.append(line)
if is_comment:
continue
e = check_format_rules(lineno, stripped_line)
if e:
errors.append(e)
lineno += 1
if errors:
with open(message_file, 'a') as commit_fd:
commit_fd.write('#\n# GIT COMMIT MESSAGE FORMAT ERRORS:\n')
for error in errors:
commit_fd.write('# %s\n' % (error,))
re_edit = raw_input(
'Invalid git commit message format. '
'Press y to edit and n to cancel the commit. [y/n] ')
if re_edit.lower() in ('n', 'no'):
sys.exit(1)
call('%s %s' % (editor, message_file), shell=True)
continue
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment