-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg_v2
More file actions
executable file
·99 lines (79 loc) · 2.74 KB
/
commit-msg_v2
File metadata and controls
executable file
·99 lines (79 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python3
# commit_msg hook v2
# git hook that appends the ticket code to every commit
# to install copy the file to .git/hooks/commit-msg
# author Pablo Renero Balgañón
from sys import argv
import io
import re
import subprocess
TAG_REGEX = r"^\[((?:t|[bB][Tt]#)\d+)\]"
BRANCH_REGEX = r"^.*?((?:t|[bB][Tt]#)\d+).*?$"
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def log_error(message):
print(f"{bcolors.FAIL}{bcolors.BOLD}ERROR{bcolors.ENDC}: " +
f"{message} ❌\n")
def log_warning(message):
print(f"{bcolors.WARNING}{bcolors.BOLD}WARNING{bcolors.ENDC}: " +
f"{message} ⚠️\n")
def log_success(message):
print(f"{bcolors.OKGREEN}{bcolors.BOLD}SUCCESS{bcolors.ENDC}: " +
f"{message} 🥳\n")
def read_file(file_path):
with io.open(file_path, mode="r", encoding="utf-8") as f:
return f.read()
def write_file(file_path, message):
with io.open(file_path, mode="w", encoding="utf-8") as f:
f.write(message)
def get_branch_name():
command_result = subprocess.run(
["git", "branch", "--show-current"], encoding="utf-8", capture_output=True)
if command_result.returncode != 0:
log_error("error while retrieving the branch name")
exit(-1)
return command_result.stdout.rstrip()
if __name__ == "__main__":
# Argv validation
if not len(argv) == 2:
log_error("missing file path argument")
exit(-1)
print("\n--- Commit message hook ---\n")
# Branch name retrieving
branch_name = get_branch_name()
ticket_code_match = re.match(BRANCH_REGEX, branch_name)
if not ticket_code_match:
log_success(f"{branch_name} is not a feature branch. Skipping...")
exit(0)
# Checking if commit message has the ticket code
ticket_code = ticket_code_match.group(1)
commit_message = ""
try:
commit_message = read_file(argv[1])
except:
log_error("unable to read commit message")
exit(-1)
commit_ticket_match = re.match(TAG_REGEX, commit_message)
if commit_ticket_match:
if ticket_code != commit_ticket_match.group(1):
log_warning(
"the ticket code in the commit message differs from the branch one")
else:
log_success(
"the ticket code is already in the commit message")
exit(0)
# Writing ticket code in the message
commit_message = f"[{ticket_code}] " + commit_message
try:
write_file(argv[1], commit_message)
log_success("added ticket code")
except:
log_error("unable to write commit message")
exit(-1)