Last active
December 31, 2018 10:03
-
-
Save Vedrillan/e43cb73121f1b3df417d5c35705931c8 to your computer and use it in GitHub Desktop.
Git hooks
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/python3 | |
import sys | |
import re | |
def print_error(msg): | |
print("\033[91m" + "[error] " + "\033[0m" + msg, file=sys.stderr) | |
def find_tickets_in_commit(commit_msg_file_path): | |
match = [] | |
with open(commit_msg_file_path, 'r') as content_file: | |
for line in content_file: | |
if "# ------------------------ >8 ------------------------" in line: break | |
if line.startswith('#'): continue | |
match = match + re.findall(r'(?![a-z])([A-Z]+-\d+)(?!\w)', line) | |
return match | |
def unique(list): | |
unique_list = [] | |
for element in list: | |
if element not in unique_list: | |
unique_list.append(element) | |
return unique_list | |
def verify_commit(commit_msg_file): | |
tickets = unique(find_tickets_in_commit(commit_msg_file)) | |
if len(tickets) == 0: | |
result = False | |
print_error("You are not allowed to commit without a ticket number") | |
elif len(tickets) > 1: | |
result = False | |
print_error("The commit message must contain only 1 ticket number") | |
else: | |
result = True | |
return result | |
# Collect the parameters | |
commit_msg_file_arg = sys.argv[1] | |
if verify_commit(commit_msg_file_arg): | |
sys.exit(0) | |
else: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment