Skip to content

Instantly share code, notes, and snippets.

@gortok
Last active June 8, 2020 12:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gortok/b204ac4997e9916ec6c6ef0987251530 to your computer and use it in GitHub Desktop.
Save gortok/b204ac4997e9916ec6c6ef0987251530 to your computer and use it in GitHub Desktop.
Python Pre Receive hook for checking commit messages
#!/bin/python
import sys
import re
import subprocess
#Format: "oldref newref branch"
line = sys.stdin.read()
(base, commit, ref) = line.strip().split()
new_branch_push = re.match(r'[^1-9]+', base)
branch_deleted = re.match(r'[^1-9]+', commit)
contains_commit_msg = False
if not new_branch_push:
revs = base + "..." + commit
proc = subprocess.Popen(['git', 'rev-list','--oneline','--first-parent', revs], stdout=subprocess.PIPE)
lines = proc.stdout.readlines()
if lines:
for line in lines:
rev = str(line)
match = re.search(r'TICKET-[0-9]{2,5}|#TICKET-[0-9]{2,5}|HOTFIX|FORCE', rev)
if match is not None:
contains_commit_msg = True
if contains_commit_msg or new_branch_push or branch_deleted:
exit(0)
else:
print "Commit does not contain the story associated with the commit in the format: TICKET-123 or #TICKET-123"
exit(1)
@marcellodesales
Copy link

marcellodesales commented Feb 28, 2017

@gortok, I'm facing the problem of the new branch at the moment...

Since I need to read which files changed, when a new branch is added I get the list of all files.

    if "0000000000" not in base:
      (results, code) = GitRepo.git(('git', 'show', base + ".." + commit, '--pretty=format:', '--name-only'))

    else:
      # All files in the current revision. No way to know which files changed in new branch
      (results, code) = GitRepo.git(('git', 'ls-tree', '-r', 'HEAD', '--name-only'))

@frakman1
Copy link

Thank you for sharing this script. However, ignoring the new branch case is not 'handling' it. A user can still incorrectly format the commit message and push it to the repo and get away with it as long as he does it in a new branch. This is in fact what I just tested and observed.

@andrewt82
Copy link

thank's! very helpful script!!!

@kinglamer
Copy link

What is python version required?

@danlyh5288
Copy link

Learned so much from this little script, really appreciated.

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