-
Notifications
You must be signed in to change notification settings - Fork 12
/
git-changelog
executable file
·150 lines (125 loc) · 4.98 KB
/
git-changelog
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
#!/usr/bin/env python
#
# git-changelog - Output a rpm changelog
#
# Copyright (C) 2009-2010 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: David Cantrell <[email protected]>
# Author: Brian C. Lane <[email protected]>
import re
import subprocess
import textwrap
from argparse import ArgumentParser
class ChangeLog:
def __init__(self, name, version):
self.name = name
self.version = version
self.ignore = None
def _getCommitDetail(self, commit, field):
proc = subprocess.Popen(['git', 'log', '-1',
"--pretty=format:%s" % field, commit],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
ret = proc[0].strip('\n').split('\n')
if len(ret) == 1 and ret[0].find('@') != -1:
ret = ret[0].split('@')[0]
elif len(ret) == 1:
ret = ret[0]
else:
ret = filter(lambda x: x != '', ret)
return ret
def _extract_issue_ids(self, s):
prefix_pattern = re.compile(r'^(Fix|Fixes|Bug|Resolves?):? (.+)$', re.IGNORECASE)
prefix_match = prefix_pattern.match(s)
if prefix_match is None:
return
rest_s = prefix_match.groups()[1]
issue_ids = []
for match in re.findall(r'(BZ|RHBZ|bz|rhbz)(:|: | )?(#?\d+)', rest_s):
issue_ids.append('{0}{1}'.format(match[0], match[2]))
if issue_ids:
# Sometimes, "BZ #1234" could confuse next step of search. So,
# remove them once found to avoid such confusion.
rest_s = re.sub(r'(BZ|RHBZ|bz|rhbz)(:|: | )?(#?\d+)', '', rest_s)
issue_ids.extend(re.findall(r'#?\d+', rest_s))
issue_ids.sort()
return issue_ids
def _get_fixed_issues(self, commit):
"""Get fixed issue or bug IDs from commit message body
Both patterns matching pagure issue and Bugzilla bug are supported.
Examples,
Fix #1234
Fixes: #11234
Fixes: #11234 #9283
Bug 123456
Bug BZ123456 RHBZ123456
"""
body = self._getCommitDetail(commit, "%b")
ids = None
for line in body:
ids = self._extract_issue_ids(line)
if ids:
break
return ids
def getLog(self):
if not self.name:
range = "%s.." % (self.version)
else:
range = "%s-%s.." % (self.name, self.version)
proc = subprocess.Popen(['git', 'log', '--pretty=oneline', '--no-merges', range],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
lines = filter(lambda x: x.find('l10n: ') != 41 and
x.find('Merge commit') != 41 and
x.find('Merge branch') != 41,
proc[0].strip('\n').split('\n'))
if self.ignore and self.ignore != '':
for commit in self.ignore.split(','):
lines = filter(lambda x: not x.startswith(commit), lines)
log = []
for line in lines:
fields = line.split(' ')
commit = fields[0]
summary = self._getCommitDetail(commit, "%s")
author = self._getCommitDetail(commit, "%aE")
issue_ids = self._get_fixed_issues(commit)
if issue_ids:
log.append(("%s - %s (%s)" % (summary.strip(), ' '.join(issue_ids), author)))
else:
log.append(("%s (%s)" % (summary.strip(), author)))
return log
def formatLog(self):
s = ""
for msg in self.getLog():
sublines = textwrap.wrap(msg, 77)
s = s + "- %s\n" % sublines[0]
if len(sublines) > 1:
for subline in sublines[1:]:
s = s + " %s\n" % subline
return s
def main():
parser = ArgumentParser()
parser.add_argument("-n", "--name",
help="Name of package used in tags")
parser.add_argument("-v", "--version",
help="Last version, changelog is commits after this tag")
args = parser.parse_args()
cl = ChangeLog(args.name, args.version)
print(cl.formatLog())
if __name__ == "__main__":
main()