-
-
Save figadore/aa3a8c6105a75bd63bddd7e9d5758dac to your computer and use it in GitHub Desktop.
Git commit message hook
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
#!/bin/bash | |
# adapted from http://addamhardy.com/2013/06/05/good-commit-messages-and-enforcing-them-with-git-hooks.html | |
################################################################################ | |
# Store this file at ~/.git-hooks-global/commit-msg. You may need to make the | |
# script executable with 'chmod +x .git-hooks-global/commit-msg'. Finally, run | |
# 'git config --global core.hooksPath ~/.git-hooks-global/' | |
################################################################################ | |
exec < /dev/tty | |
~/.git-hooks-global/validate-msg.py $1 |
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/python | |
################################################################################ | |
# Store this file at ~/.git-hooks-global/validate-msg.py | |
################################################################################ | |
import sys | |
import os | |
from subprocess import call | |
print(os.environ.get('EDITOR')) | |
if os.environ.get('EDITOR') != 'none': | |
editor = os.environ['EDITOR'] | |
else: | |
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 not line.startswith('#'): | |
# if len(line) > 72: | |
# return "Error " + real_lineno + ": No line should be over 72 characters long." | |
return False | |
while True: | |
commit_msg = list() | |
errors = list() | |
with open(message_file) as commit_fd: | |
for lineno, line in enumerate(commit_fd): | |
stripped_line = line.strip() | |
commit_msg.append(line) | |
e = check_format_rules(lineno, stripped_line) | |
if e: | |
errors.append(e) | |
if errors: | |
with open(message_file, 'w') as commit_fd: | |
commit_fd.write('%s\n' % '# GIT COMMIT MESSAGE FORMAT ERRORS:') | |
for error in errors: | |
commit_fd.write('# %s\n' % (error,)) | |
for line in commit_msg: | |
commit_fd.write(line) | |
# python 2 and 3 compatibility hack | |
try: | |
input = raw_input | |
except NameError: | |
pass | |
re_edit = 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