pmalek
11/14/2015 - 1:04 PM

commit-msg hook checking whether there already exists a commit in git repository with passed in commit message

commit-msg hook checking whether there already exists a commit in git repository with passed in commit message

#!/usr/bin/python

from subprocess import check_output
import sys

class Colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

COMMIT_MSG = ""
for s in [(s) for s in sys.argv if s == ".git/COMMIT_EDITMSG"]:
    with open(s, 'r') as f:
        COMMIT_MSG = f.read().rstrip()

for line in check_output(["git", "log", "--pretty=format:%s"]).splitlines():
    if line.rstrip() == COMMIT_MSG:
        print Colors.FAIL, "[DUPLICATE]",
        print "There has already been a commit in this repository with '", line.rstrip(),
        print "' commit message!"
        exit(1)