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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
#!/usr/bin/python -u
# vim: set fileencoding=utf-8 :
#
# (C) 2007, 2008, 2009, 2010 Guido Guenther <agx@sigxcpu.org>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Generate Debian changelog entries from git commit messages"""
import ConfigParser
import os.path
import re
import sys
import shutil
import subprocess
import gbp.command_wrappers as gbpc
from gbp.git import (GitRepositoryError, GitRepository, build_tag)
from gbp.config import GbpOptionParser, GbpOptionGroup
from gbp.errors import GbpError
from gbp.deb import parse_changelog, NoChangelogError
from gbp.command_wrappers import (Command, CommandExecFailed)
snapshot_re = re.compile("\s*\*\* SNAPSHOT build @(?P<commit>[a-z0-9]+)\s+\*\*")
author_re = re.compile('Author: (?P<author>.*) <(?P<email>.*)>')
bug_r = r'(?:bug)?\#?\s?\d+'
bug_re = re.compile(bug_r, re.I)
def system(cmd):
try:
Command(cmd, shell=True)()
except CommandExecFailed:
raise GbpError
def escape_commit(msg):
return msg.replace('"','\\\"').replace("$","\$").replace("`","\`")
def ispunct(ch):
return not ch.isalnum() and not ch.isspace()
def punctuate_commit(msg):
"""Terminate the first line of a commit message with a '.' if it's a
multiline commit"""
lines = msg.split('\n', 1)
if len(lines) <= 1:
return msg
first, rest = lines
# Don't add a dot on one line commit messages
if not rest:
return msg
if first and ispunct(first[-1]):
return msg
if ispunct(rest[0]) or rest[0].islower():
return msg
return "%s.\n%s" % (first, rest)
def spawn_dch(msg='', author=None, email=None, newversion=False, version=None,
release=False, distribution=None, dch_options=''):
"""
Spawn dch
param author: committers name
param email: committers email
param newversion: start a new version
version: the verion to use
release: finalize changelog for releaze
distribution: distribution to use
dch_options: options passed verbatim to dch
"""
distopt = ""
versionopt = ""
env = ""
if newversion:
if version:
try:
versionopt = version['increment']
except KeyError:
versionopt = '--newversion=%s' % version['version']
else:
versionopt = '-i'
elif release:
versionopt = "--release --no-force-save-on-release"
msg = None
if msg:
msg = punctuate_commit(msg)
if author and email:
env = """DEBFULLNAME="%s" DEBEMAIL="%s" """ % (author, email)
if distribution:
distopt = "--distribution=%s" % distribution
cmd = '%(env)s dch --no-auto-nmu %(distopt)s %(versionopt)s %(dch_options)s ' % locals()
if type(msg) == type(''):
cmd += '-- "%s"' % escape_commit(msg)
system(cmd)
def add_changelog_entry(msg, author, email, dch_options):
"add a single changelog entry"
spawn_dch(msg=msg, author=author, email=email, dch_options=dch_options)
def add_changelog_section(msg, distribution, author=None, email=None, version=None, dch_options=''):
"add a new changelog section"
spawn_dch(msg=msg, newversion=True, version=version, author=author, email=email,
distribution=distribution, dch_options=dch_options)
def get_author_email(repo, use_git_config):
"""Get author and email from git configuration"""
author = email = None
if use_git_config:
try: author = repo.get_config('user.name')
except KeyError: pass
try: email = repo.get_config('user.email')
except KeyError: pass
return author, email
def fixup_trailer(repo, git_author, dch_options):
"""fixup the changelog trailer's comitter and email address - it might
otherwise point to the last git committer instead of the person creating
the changelog"""
author, email = get_author_email(repo, git_author)
spawn_dch(msg='', author=author, email=email, dch_options=dch_options)
def head_commit(repo):
"""get the full sha1 of the last commit on HEAD"""
return repo.rev_parse('HEAD')
def snapshot_version(version):
"""
get the current release and snapshot version
Format is <debian-version>~<release>.gbp<short-commit-id>
"""
try:
(release, suffix) = version.rsplit('~', 1)
(snapshot, commit) = suffix.split('.', 1)
if not commit.startswith('gbp'):
raise ValueError
else:
snapshot = int(snapshot)
except ValueError: # not a snapshot release
release = version
snapshot = 0
return release, snapshot
def mangle_changelog(changelog, cp, snapshot=''):
"""
Mangle changelog to either add or remove snapshot markers
@param snapshot: SHA1 if snapshot header should be added/maintained, empty if it should be removed
@type snapshot: str
"""
try:
tmpfile = '%s.%s' % (changelog, snapshot)
cw = file(tmpfile, 'w')
cr = file(changelog, 'r')
cr.readline() # skip version and empty line
cr.readline()
print >>cw, "%(Source)s (%(MangledVersion)s) %(Distribution)s; urgency=%(urgency)s\n" % cp
line = cr.readline()
if snapshot_re.match(line):
cr.readline() # consume the empty line after the snapshot header
line = ''
if snapshot:
print >>cw, " ** SNAPSHOT build @%s **\n" % snapshot
if line:
print >>cw, line.rstrip()
shutil.copyfileobj(cr, cw)
cw.close()
cr.close()
os.unlink(changelog)
os.rename(tmpfile, changelog)
except OSError, e:
raise GbpError, "Error mangling changelog %s" % e
def do_release(changelog, repo, cp, git_author, dch_options):
"remove the snapshot header and set the distribution"
author, email = get_author_email(repo, git_author)
(release, snapshot) = snapshot_version(cp['Version'])
if snapshot:
cp['MangledVersion'] = release
mangle_changelog(changelog, cp)
spawn_dch(release=True, author=author, email=email, dch_options=dch_options)
def do_snapshot(changelog, repo, next_snapshot):
"""
Add new snapshot banner to most recent changelog section. The next snapshot
number is calculated by eval()'ing next_snapshot
"""
commit = head_commit(repo)
cp = parse_changelog(changelog)
(release, snapshot) = snapshot_version(cp['Version'])
snapshot = int(eval(next_snapshot))
suffix = "%d.gbp%s" % (snapshot, "".join(commit[0:6]))
cp['MangledVersion'] = "%s~%s" % (release, suffix)
mangle_changelog(changelog, cp, commit)
return snapshot, commit
def get_author(commit):
"""get the author from a commit message"""
for line in commit:
m = author_re.match(line)
if m:
return m.group('author'), m.group('email')
def parse_commit(repo, commitid, options):
"""parse a commit and return message and author"""
msg = ''
thanks = ''
closes = ''
git_dch = ''
bugs = {}
bts_closes = re.compile(r'(?P<bts>%s):\s+%s' % (options.meta_closes, bug_r), re.I)
if options.ignore_regex: # Ignore r'' since it matches everything
ignore_re = re.compile(options.ignore_regex)
else:
ignore_re = None
commit = repo.show(commitid)
author, email = get_author(commit)
if not author:
raise GbpError, "can't parse author of commit %s" % commit
for line in commit:
if line.startswith(' '): # commit body
line = line[4:]
m = bts_closes.match(line)
if m:
bug_nums = [ bug.strip() for bug in bug_re.findall(line, re.I) ]
try:
bugs[m.group('bts')] += bug_nums
except KeyError:
bugs[m.group('bts')] = bug_nums
elif line.startswith('Thanks: '):
thanks = line.split(' ', 1)[1].strip()
elif line.startswith('Git-Dch: '):
git_dch = line.split(' ', 1)[1].strip()
else: # normal commit message
if msg and not options.full:
continue
if ignore_re and ignore_re.match(line):
continue
if line.strip(): # don't add all whitespace lines
msg += line
# start of diff output:
elif line.startswith('diff '):
break
if options.meta:
if git_dch == 'Ignore':
return None
if git_dch == 'Short':
msg = msg.split('\n')[0]
for bts in bugs:
closes += '(%s: %s) ' % (bts, ', '.join(bugs[bts]))
if thanks:
thanks = '- thanks to %s' % thanks
msg += closes + thanks
if options.idlen:
msg = "[%s] " % commitid[0:options.idlen] + msg
return msg, (author, email)
def guess_snapshot_commit(cp, repo, options):
"""
guess the last commit documented in the changelog from the snapshot banner
or the last point the changelog was touched.
"""
sr = re.search(snapshot_re, cp['Changes'])
if sr:
return sr.group('commit')
# If the current topmost changelog entry has already been tagged rely on
# the version information only. The upper level relies then on the version
# info anyway:
if repo.find_version(options.debian_tag, cp['Version']):
return None
# If we didn't find a snapshot header we look at the point the changelog
# was last touched.
last = repo.commits(paths="debian/changelog", options=["-1"])
if last:
print "Changelog last touched at '%s'" % last[0]
return last[0]
return None
def main(argv):
ret = 0
changelog = 'debian/changelog'
until = 'HEAD'
found_snapshot_header = False
first_commit = None
version_change = {}
try:
parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='',
usage='%prog [options] paths')
except ConfigParser.ParsingError, err:
print >>sys.stderr, err
return 1
range_group = GbpOptionGroup(parser, "commit range options", "which commits to add to the changelog")
version_group = GbpOptionGroup(parser, "release & version number options", "what version number and release to use")
commit_group = GbpOptionGroup(parser, "commit message formatting", "howto format the changelog entries")
naming_group = GbpOptionGroup(parser, "branch and tag naming", "branch names and tag formats")
parser.add_option_group(range_group)
parser.add_option_group(version_group)
parser.add_option_group(commit_group)
parser.add_option_group(naming_group)
parser.add_boolean_config_file_option(option_name = "ignore-branch", dest="ignore_branch")
naming_group.add_config_file_option(option_name="debian-branch", dest="debian_branch")
naming_group.add_config_file_option(option_name="upstream-tag", dest="upstream_tag")
naming_group.add_config_file_option(option_name="debian-tag", dest="debian_tag")
naming_group.add_config_file_option(option_name="snapshot-number", dest="snapshot_number",
help="expression to determine the next snapshot number, default is '%(snapshot-number)s'")
parser.add_config_file_option(option_name="git-log", dest="git_log",
help="options to pass to git-log, default is '%(git-log)s'")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
range_group.add_option("-s", "--since", dest="since", help="commit to start from (e.g. HEAD^^^, debian/0.4.3)")
range_group.add_option("-a", "--auto", action="store_true", dest="auto", default=False,
help="autocomplete changelog from last snapshot or tag")
version_group.add_option("-R", "--release", action="store_true", dest="release", default=False,
help="mark as release")
version_group.add_option("-S", "--snapshot", action="store_true", dest="snapshot", default=False,
help="mark as snapshot build")
version_group.add_option("-N", "--new-version", dest="new_version",
help="use this as base for the new version number")
version_group.add_option("--bpo", dest="bpo", action="store_true", default=False,
help="Increment the Debian release number for an upload to lenny-backports, and add a backport upload changelog comment.")
version_group.add_option("--nmu", dest="nmu", action="store_true", default=False,
help="Increment the Debian release number for a non-maintainer upload")
version_group.add_option("--qa", dest="qa", action="store_true", default=False,
help="Increment the Debian release number for a Debian QA Team upload, and add a QA upload changelog comment.")
version_group.add_boolean_config_file_option(option_name="git-author", dest="git_author")
commit_group.add_boolean_config_file_option(option_name="meta", dest="meta")
commit_group.add_config_file_option(option_name="meta-closes", dest="meta_closes",
help="Meta tags for the bts close commands, default is '%(meta-closes)s'")
commit_group.add_boolean_config_file_option(option_name="full", dest="full")
commit_group.add_config_file_option(option_name="id-length", dest="idlen",
help="include N digits of the commit id in the changelog entry, default is '%(id-length)s'",
type="int", metavar="N")
commit_group.add_config_file_option(option_name="ignore-regex", dest="ignore_regex",
help="Ignore commit lines matching regex, default is '%(ignore-regex)s'")
commit_group.add_boolean_config_file_option(option_name="multimaint-merge", dest="multimaint_merge")
(options, args) = parser.parse_args(argv[1:])
if options.snapshot and options.release:
parser.error("'--snapshot' and '--release' are incompatible options")
if options.since and options.auto:
parser.error("'--since' and '--auto' are incompatible options")
if options.multimaint_merge:
dch_options = "--multimaint-merge"
else:
dch_options = "--nomultimaint-merge"
try:
if options.verbose:
gbpc.Command.verbose = True
try:
repo = GitRepository('.')
except GitRepositoryError:
raise GbpError, "%s is not a git repository" % (os.path.abspath('.'))
branch = repo.get_branch()
if options.debian_branch != branch and not options.ignore_branch:
print >>sys.stderr, "You are not on branch '%s' but on '%s'" % (options.debian_branch, branch)
raise GbpError, "Use --ignore-branch to ignore or --debian-branch to set the branch name."
cp = parse_changelog(changelog)
if options.since:
since = options.since
else:
since = ''
if options.auto:
since = guess_snapshot_commit(cp, repo, options)
if since:
print "Continuing from commit '%s'" % since
found_snapshot_header = True
else:
print "Couldn't find snapshot header, using version info"
if not since:
since = repo.find_version(options.debian_tag, cp['Version'])
if not since:
raise GbpError, "Version %s not found" % cp['Version']
if args:
print "Only looking for changes on '%s'" % " ".join(args)
commits = repo.commits(since=since, until=until, paths=" ".join(args), options=options.git_log.split(" "))
# add a new changelog section if:
if options.new_version or options.bpo or options.nmu or options.qa:
if options.bpo:
version_change['increment'] = '--bpo'
elif options.nmu:
version_change['increment'] = '--nmu'
elif options.qa:
version_change['increment'] = '--qa'
else:
version_change['version'] = options.new_version
# the user wants to force a new version
add_section = True
elif cp['Distribution'] != "UNRELEASED" and not found_snapshot_header and commits:
# the last version was a release and we have pending commits
add_section = True
elif options.snapshot and not found_snapshot_header:
# the user want to switch to snapshot mode
add_section = True
else:
add_section = False
for c in commits:
parsed = parse_commit(repo, c, options)
if not parsed:
# Some commits can be ignored
continue
commit_msg, (commit_author, commit_email) = parsed
if add_section:
# Add a section containing just this message (we can't
# add an empty section with dch)
add_changelog_section(distribution="UNRELEASED", msg=commit_msg,
version=version_change,
author=commit_author,
email=commit_email,
dch_options=dch_options)
# Adding a section only needs to happen once.
add_section = False
else:
add_changelog_entry(commit_msg, commit_author, commit_email, dch_options)
# Show a message if there were no commits (not even ignored
# commits).
if not commits:
print "No changes detected from %s to %s." % (since, until)
if add_section:
# If we end up here, then there were no commits to include,
# so we put a dummy message in the new section.
commit_msg = "UNRELEASED"
add_changelog_section(distribution="UNRELEASED", msg="UNRELEASED",
version=version_change,
dch_options=dch_options)
fixup_trailer(repo, git_author=options.git_author,
dch_options=dch_options)
if options.release:
do_release(changelog, repo, cp, git_author=options.git_author,
dch_options=dch_options)
elif options.snapshot:
(snap, version) = do_snapshot(changelog, repo, options.snapshot_number)
print "Changelog has been prepared for snapshot #%d at %s" % (snap, version)
except (GbpError, GitRepositoryError, NoChangelogError), err:
if len(err.__str__()):
print >>sys.stderr, err
ret = 1
return ret
if __name__ == "__main__":
sys.exit(main(sys.argv))
# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·:
|