-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
changes.py
283 lines (233 loc) · 10.5 KB
/
changes.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# coding=utf-8
import os
import string
import utils
from event import Event
import config
def encodedText(logText):
logText = logText.replace('&', '&')
logText = logText.replace(u'ä', u'ä')
logText = logText.replace(u'ö', u'ö')
logText = logText.replace(u'Ä', u'Ä')
logText = logText.replace(u'Ö', u'Ö')
logText = logText.encode('utf-8')
logText = logText.replace('<', '<')
logText = logText.replace('>', '>')
return logText
class Entry:
def __init__(self):
self.subject = ''
self.extra = ''
self.author = ''
self.date = ''
self.link = ''
self.hash = ''
self._message = ''
self.tags = []
self.guessedTags = []
def set_subject(self, subject):
self.extra = ''
# Remote tags from the subject.
pos = subject.find(':')
if subject[pos + 1] in string.whitespace:
for tag in subject[:pos].split('|'):
self.tags.append(tag.strip())
subject = subject[pos + 1:].strip()
if len(subject) == 0: subject = "Commit"
# Check that the subject lines are not too long.
MAX_SUBJECT = 100
if len(utils.collated(subject)) > MAX_SUBJECT:
# Find a suitable spot the break the subject line.
pos = MAX_SUBJECT
while subject[pos] not in string.whitespace: pos -= 1
self.extra = '...' + subject[pos+1:] + ' '
subject = subject[:pos] + '...'
else:
# If there is a single dot at the end of the subject, remove it.
if subject[-1] == '.' and subject[-2] != '.':
subject = subject[:-1]
self.subject = encodedText(subject)
def set_message(self, message):
self._message = message.strip()
if self.extra:
self._message = self.extra + ' ' + self._message
self._message = encodedText(self._message)
def message(self, encodeHtml=False):
if encodeHtml:
return self._message.replace('\n\n', '<br/><br/>').replace('\n', ' ').strip()
return self._message.strip()
class Changes:
def __init__(self, fromTag, toTag):
self.fromTag = fromTag
self.toTag = toTag
self.parse()
def should_ignore(self, subject):
if subject.startswith("Merge branch 'master' of"):
# master->master merges are not listed.
return True
return False
def parse(self):
tmpName = '__ctmp'
format = '[[Subject]]%s[[/Subject]]' + \
'[[Author]]%an[[/Author]]' + \
'[[Date]]%ai[[/Date]]' + \
'[[Link]]http://deng.git.sourceforge.net/git/gitweb.cgi?' + \
'p=deng/deng;a=commit;h=%H[[/Link]]' + \
'[[Hash]]%H[[/Hash]]' + \
'[[Message]]%b[[/Message]]'
os.system("git log %s..%s --format=\"%s\" >> %s" % (self.fromTag, self.toTag, format, tmpName))
logText = unicode(file(tmpName, 'rt').read(), 'utf-8')
os.remove(tmpName)
pos = 0
self.entries = []
self.debChangeEntries = []
while True:
entry = Entry()
pos = logText.find('[[Subject]]', pos)
if pos < 0: break # No more.
end = logText.find('[[/Subject]]', pos)
entry.set_subject(logText[pos+11:end])
# Debian changelog just gets the subjects.
if entry.subject not in self.debChangeEntries and not \
self.should_ignore(entry.subject):
self.debChangeEntries.append(entry.subject)
# Author.
pos = logText.find('[[Author]]', pos)
end = logText.find('[[/Author]]', pos)
entry.author = logText[pos+10:end]
# Date.
pos = logText.find('[[Date]]', pos)
end = logText.find('[[/Date]]', pos)
entry.date = logText[pos+8:end]
# Link.
pos = logText.find('[[Link]]', pos)
end = logText.find('[[/Link]]', pos)
entry.link = logText[pos+8:end]
# Hash.
pos = logText.find('[[Hash]]', pos)
end = logText.find('[[/Hash]]', pos)
entry.hash = logText[pos+8:end]
# Message.
pos = logText.find('[[Message]]', pos)
end = logText.find('[[/Message]]', pos)
entry.set_message(logText[pos+11:end])
if not self.should_ignore(entry.subject):
self.entries.append(entry)
self.deduce_tags()
def all_tags(self):
# These words are always considered to be valid tags.
tags = ['Cleanup', 'Fixed', 'Added', 'Refactor', 'Performance', 'Optimize', 'merge branch']
for e in self.entries:
for t in e.tags + e.guessedTags:
if t not in tags:
tags.append(t)
return tags
def deduce_tags(self):
# Look for known tags in untagged titles.
allTags = self.all_tags()
for entry in self.entries:
if entry.tags: continue
# This entry has no tags yet.
for tag in allTags:
p = entry.subject.lower().find(tag.lower())
if p < 0: continue
if p == 0 or entry.subject[p - 1] not in string.ascii_letters + '-_':
entry.guessedTags.append(tag)
def form_groups(self, allEntries):
groups = {}
for tag in self.all_tags():
groups[tag] = []
for e in allEntries:
if tag in e.tags:
groups[tag].append(e)
for e in allEntries:
if tag in e.guessedTags:
groups[tag].append(e)
for e in groups[tag]:
allEntries.remove(e)
groups['Miscellaneous'] = []
for e in allEntries:
groups['Miscellaneous'].append(e)
return groups
def pretty_group_list(self, tags):
listed = ''
if len(tags) > 1:
listed = string.join(tags[:-1], ', ')
listed += ' and ' + tags[-1]
elif len(tags) == 1:
listed = tags[0]
return listed
def generate(self, format):
fromTag = self.fromTag
toTag = self.toTag
if format == 'html':
out = file(Event(toTag).file_path('changes.html'), 'wt')
MAX_COMMITS = 100
entries = self.entries[:MAX_COMMITS]
if len(self.entries) > MAX_COMMITS:
print >> out, '<p>Showing %i of %i commits.' % (MAX_COMMITS, len(self.entries))
print >> out, 'The <a href="%s">oldest commit</a> is dated %s.</p>' % \
(self.entries[-1].link, self.entries[-1].date)
# Form groups.
groups = self.form_groups(entries)
keys = groups.keys()
# Sort case-insensitively by group name.
keys.sort(cmp=lambda a, b: cmp(str(a).lower(), str(b).lower()))
for group in keys:
if not len(groups[group]): continue
print >> out, '<h3>%s</h3>' % group
print >> out, '<ul>'
# Write a list entry for each commit.
for entry in groups[group]:
otherGroups = []
for tag in entry.tags + entry.guessedTags:
if tag != group:
otherGroups.append(tag)
others = self.pretty_group_list(otherGroups)
if others: others = ' <i>[→ %s]</i>' % others
print >> out, '<li><b>%s</b>%s' % (entry.subject, others)
print >> out, 'by <i>%s</i> on ' % entry.author
print >> out, '<a href="%s">%s</a>' % (entry.link, entry.date[:10])
print >> out, '<blockquote style="color:#666;">%s</blockquote>' % entry.message(encodeHtml=True)
print >> out, '</ul>'
out.close()
elif format == 'xml':
out = file(Event(toTag).file_path('changes.xml'), 'wt')
print >> out, '<commitCount>%i</commitCount>' % len(self.entries)
print >> out, '<commits>'
for entry in self.entries:
print >> out, '<commit>'
print >> out, '<submitDate>%s</submitDate>' % entry.date
print >> out, '<author>%s</author>' % entry.author
print >> out, '<repositoryUrl>%s</repositoryUrl>' % entry.link
print >> out, '<sha1>%s</sha1>' % entry.hash
if entry.tags or entry.guessedTags:
print >> out, '<tags>'
for t in entry.tags:
print >> out, '<tag guessed="false">%s</tag>' % t
for t in entry.guessedTags:
print >> out, '<tag guessed="true">%s</tag>' % t
print >> out, '</tags>'
print >> out, '<title>%s</title>' % entry.subject
if len(entry.message()):
print >> out, '<message>%s</message>' % entry.message()
print >> out, '</commit>'
print >> out, '</commits>'
out.close()
elif format == 'deb':
import build_version
build_version.find_version()
# Append the changes to the debian package changelog.
os.chdir(os.path.join(config.DISTRIB_DIR, 'linux'))
# First we need to update the version.
debVersion = build_version.DOOMSDAY_VERSION_FULL_PLAIN + '-' + Event().tag()
# Always make one entry.
print 'Marking new version...'
msg = 'New release: %s build %i.' % (build_version.DOOMSDAY_RELEASE_TYPE,
Event().number())
os.system("dch --check-dirname-level 0 -v %s \"%s\"" % (debVersion, msg))
for entry in self.debChangeEntries:
# Quote it for the command line.
qch = entry.replace('"', '\\"').replace('!', '\\!')
print ' *', qch
os.system("dch --check-dirname-level 0 -a \"%s\"" % qch)