/
prepare-commit-msg
executable file
·86 lines (74 loc) · 3.54 KB
/
prepare-commit-msg
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
#!/bin/python3
# creates template for git-commit
from sys import argv, exit
from utils import *
commit_msg_file = open(argv[1], 'r+', encoding="utf8", errors='ignore')
commit_msg_split = readlines(commit_msg_file)
if commit_msg_split[0] != "": # don't change anything for git commit --amend
exit(0)
def last_dir_or_filename(filepath):
path_units = filepath.split('/')
#### job addition ↓
if filepath.startswith('api/python/core/') or filepath.startswith('api/python/cli/'):
return 'baum-manager'
elif 'tests/tests/' in filepath:
return f'tests/{path_units[-1].split(".")[0]}'
elif 'tests/api/' in filepath:
return 'tests/api'
elif 'tests/gui/' in filepath:
return 'tests/gui'
elif 'gitlab-ci/' in filepath:
return 'ci'
elif 'image/' in filepath:
return 'image'
elif 'web_server/' in filepath:
return 'web'
#### job addition ↑
list_to_prefer_filename = ['src', 'scripts']
last_dir = path_units[-2] if len(path_units) > 2 else None
# heuristics: if we have a path dir/src/file.x, then it's likely that
# the "dir" before "src" is the subsystem name. So use it.
if last_dir == 'src':
return path_units[-3]
return (last_dir if (last_dir and last_dir not in list_to_prefer_filename) \
else path_units[-1])
def insert_comment_w_prev_commit_title(commit_msg_split, prev_commit_title):
if not prev_commit_title:
return
i_fst_comment = find_i(commit_msg_split, lambda line: line.startswith('#'))
if i_fst_comment is None:
commit_msg_split[-1] = f'\n# {prev_commit_title}'
else:
commit_msg_split.insert(i_fst_comment, f'# {prev_commit_title}')
def get_prev_commit_stats():
(prev_commit_stdout, exitcode) = run_cmd_no_fail('git log -1 --format=%s') # exitcode != 0 means repo that has no commits yet
prev_commit_title = '' if exitcode != 0 else prev_commit_stdout[0]
if exitcode == 0:
(prev_commit_fst_changed_file, exitcode) = run_cmd_no_fail('git log -1 --name-only --pretty=format:')
if exitcode != 0:
raise Exception('git log failed!')
return (prev_commit_title, prev_commit_fst_changed_file[0])
return ('', '')
def maybe_repeat_title(fst_changed_file: str,
prev_commit_fst_changed_file: str,
prev_commit_title: str) -> str:
prefix_curr = last_dir_or_filename(fst_changed_file)
prefix_prev = prev_commit_title.split(':')[0]
if fst_changed_file == prev_commit_fst_changed_file \
and prefix_curr != prefix_prev:
# this means we didn't guess the prefix correctly, and user had to choose a
# different one. So take what user wrote back then
return prefix_prev
return prefix_curr
(prev_commit_title, prev_commit_fst_changed_file) = get_prev_commit_stats()
maybe_changed_files = run_cmd("git diff --name-only --cached") # an empty commits would lack changed files
fst_changed_file = maybe_changed_files[0] if maybe_changed_files else ''
# this one is a switch for my current job workflow
bug_number = (lambda: [maybe_bug := prev_commit_title.split(),
f' {maybe_bug[-1]}' if maybe_bug and maybe_bug[-1].startswith('#') else ''])()[-1]
prefix = maybe_repeat_title(fst_changed_file,
prev_commit_fst_changed_file,
prev_commit_title)
commit_msg_split[0] = prefix + f': {bug_number}'
insert_comment_w_prev_commit_title(commit_msg_split, prev_commit_title)
overwrite_file(commit_msg_file, commit_msg_split)