forked from Questie/Questie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.py
executable file
·82 lines (61 loc) · 2.21 KB
/
changelog.py
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
#!/usr/bin/env python3
import subprocess
# define the tags that should be shown and their order
commit_keys_and_header = (
('feature', '## New Features\n\n'),
('fix', '## General Fixes\n\n'),
('quest', '## Quest Fixes\n\n'),
('db', '## Database Fixes\n\n'),
('locale', '## Localisation Fixes\n\n'),
)
def get_commit_changelog():
last_tag = get_last_git_tag()
git_log = get_chronological_git_log(last_tag)
categories = get_sorted_categories(git_log)
return get_changelog_string(categories)
def get_last_git_tag():
return subprocess.run(
["git", "describe", "--abbrev=0", "--tags"],
capture_output=True
).stdout.decode().strip('\n')
def get_chronological_git_log(last_tag):
# get array of the first line of the commit messages since last tag
git_log = subprocess.run(
["git", "log", "--pretty=format:%s", f"{last_tag}..HEAD"],
capture_output=True
).stdout.decode().split('\n')
# reverse it so it's chronological
git_log.reverse()
return git_log
def get_sorted_categories(git_log):
categories = {}
for key_header in commit_keys_and_header:
categories[key_header[0]] = []
for line in git_log:
for key in categories.keys():
if f'[{key}]' in line:
line = line.replace(f'[{key}]', '').strip()
line = transform_lines_into_past_tense(line)
categories[key].append(line)
return categories
def transform_lines_into_past_tense(line):
line = line.replace('Add', 'Added')
line = line.replace('Fix', 'Fixed')
line = line.replace('Mark', 'Marked')
line = line.replace('Change', 'Changed')
line = line.replace('Update', 'Updated')
line = line.replace('Blacklist', 'Blacklisted')
return line
def get_changelog_string(categories):
changelog = ''
for key_header in commit_keys_and_header:
key = key_header[0]
if len(categories[key]) > 0:
header = key_header[1]
changelog += header
for line in categories[key]:
changelog += f'* {line}\n'.replace('[', '\\[')
changelog += '\n'
return changelog
if __name__ == "__main__":
print(get_commit_changelog())