This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenlog
executable file
·188 lines (167 loc) · 4.89 KB
/
genlog
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
"""Generate CHANGELOG
Idea by Matt DeBoard
http://mattdeboard.net/2014/01/14/automatic-changelog-generation-with-git/
"""
import click
import datetime
import jinja2
import json
import os
import re
import subprocess
GIT_FORMAT = (
'--pretty=format:{'
'STRXSEPrevisionSTRXSEP: STRXSEP%hSTRXSEP, '
'STRXSEPsubjectSTRXSEP: STRXSEP%sSTRXSEP, '
'STRXSEPbodySTRXSEP: STRXSEP%bSTRXSEP}EOFL'
)
EXTRACT_PR = re.compile("Merge pull request #(\d+) ")
TEMPLATE = """
{
'version' : "{{ version }}",
'date' : "{{ date }}",
'author' : "{{ author }}",
'changes' : [ {% for change in changes %}
"{{ change.subject }}",
[
{% if change.uri %}"{{ change.uri }}",
{% endif %}{% if change.body %}"{{ change.body }}",{% endif %}
],
{% endfor %}
],
},
"""
@click.command()
@click.option(
'-m',
'--merge',
is_flag=True,
help="Use this if you work with pull/merge-requests.",
)
@click.argument(
'project_uri',
nargs=1,
)
@click.argument(
'version_file',
nargs=1,
)
@click.argument(
'start_commit',
nargs=1,
)
@click.argument(
'end_commit',
nargs=1,
)
def genlog(merge, project_uri, version_file, start_commit, end_commit):
"""Generate CHANGELOG. Preprend the result to the CHANGELOG file and edit
it as you wish.
PROJECT_URI Reference to a github project
VERSION_FILE File to read the version from
START_COMMIT Generate the log from this revision or tag
END_COMMIT Generate the log to this revision or tag
"""
logs = list(get_commit_msgs(merge, start_commit, end_commit))
author = get_author()
date = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+0000")
version = get_version(version_file)
changes = []
template = jinja2.Template(TEMPLATE)
for log in logs:
subject = log['subject']
revision = log['revision']
match = EXTRACT_PR.findall(subject)
body = log['body']
bodysplit = body.split("LINEXSEP")
bs_len = len(bodysplit)
newbody = []
for x, line in enumerate(bodysplit):
if x < bs_len - 1:
next_line = bodysplit[x + 1]
else:
next_line = None
line = line.strip()
fix = False
if line.startswith("*"):
fix = True
elif line.startswith("-"):
fix = True
if fix:
line = line[1:]
line = line.strip()
fix = False
if next_line:
if next_line.startswith("*"):
fix = True
elif next_line.startswith("-"):
fix = True
if fix:
line = "%s." % line
newbody.append(line)
if match:
pr = match[0]
uri = "%s/pull/%s" % (project_uri, pr)
else:
uri = "%s/commit/%s" % (project_uri, revision)
change = {
'uri' : uri,
'subject' : subject,
'revision' : revision,
'body' : " ".join(newbody),
}
changes.append(change)
print(template.render(
version = version,
author = author,
date = date,
changes = changes,
))
def get_version(version_file):
"""Get the version"""
env = {}
with open(version_file) as f:
code = compile(f.read(), version_file, 'exec')
exec(code, env)
return env['__version__']
def get_commit_msgs(merge, start_commit, end_commit):
"""Get the commits in a parseable format"""
args = []
args.extend([
"git",
"log",
GIT_FORMAT,
])
if merge:
args.append("--merges")
args.append("%s...%s" % (start_commit, end_commit))
res = subprocess.check_output(args).decode("UTF-8").strip()[:-1]
# Escaping invalid json
res = "\\\"".join(res.split("\""))
res = "\"".join(res.split("STRXSEP"))
res = " ".join(res.split('\t'))
lines = res.split("EOFL%s" % os.linesep)
lines[-1] = lines[-1][:-3]
for line in lines:
if line:
line = "LINEXSEP".join(line.split(os.linesep))
# Clean all \n \r depending on platform we need one or the other
line = "".join(line.split("\r"))
line = "".join(line.split("\n"))
yield json.loads("%s" % line)
def get_author():
"""Get the author of the changelog (current git user"""
name = subprocess.check_output([
"git",
"config",
"user.name",
]).decode("UTF-8").strip()
email = subprocess.check_output([
"git",
"config",
"user.email",
]).decode("UTF-8").strip()
return "%s <%s>" % (name, email)
if __name__ == "__main__":
genlog()