Debian Codegraph @

bzr-plugins/gnulog.py

raw symbols SCIP
1 # Copyright (C) 2006-2009 Gustavo J. A. M. Carneiro <[email protected]>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 """
18 A bzr plugin that allows bzr log to output in GNU ChangeLog format.
19
20 Installation:
21 Copy this file to ~/.bazaar/plugins/
22
23 Usage:
24 bzr log -v --log-format 'gnu'
25
26 or,
27
28 BZR_GNULOG_SPLIT_ON_BLANK_LINES=0 bzr log -v --log-format 'gnu'
29
30 By default the log formatter recognizes paragraph boundaries as
31 empty lines (like in LaTeX). The environment variable
32 BZR_GNULOG_SPLIT_ON_BLANK_LINES=0 switches it to split paragraphs by
33 the newline character instead.
34
35 Changes:
36 - 2009-07-12: Missing supports_merge_revisions added, to also show
37 merged revisions from non-main branches
38 - 2009-06-24: handle get_apparent_author deprecation (based on patch by Zdenek Hatas)
39 - 2009-04-04: add option to recognize newlines as paragraph boundaries too
40 - 2009-04-04: recognize blank lines as paragraph boundaries, and don't mix paragraphs
41 - 2009-04-04: output text in default system encoding, not ascii
42 - 2008-12-08: include information of fixed bugs (bzr commit --fixes)
43 """
44
45 import time
46 import textwrap
47 from StringIO import StringIO # note: we can't use cStringIO; it does not handle unicode well.
48 import sys
49 import os
50
51 import bzrlib.log
52 #from bzrlib.osutils import format_date
53
54 SPLIT_ON_BLANK_LINES = bool(int(os.environ.get('BZR_GNULOG_SPLIT_ON_BLANK_LINES', "1")))
55
56 class GnuFormatter(bzrlib.log.LogFormatter):
57
58 def __init__(self, *args, **kwargs):
59 bzrlib.log.LogFormatter.__init__(self, *args, **kwargs)
60 self._changes_buffer = StringIO()
61 self._date_line = None
62 self.supports_tags = True
63 self.supports_delta = True
64 self.supports_merge_revisions = True
65
66 def log_revision(self, lr):
67 self.show(lr.revno, lr.rev, lr.delta, lr.tags)
68
69 def show(self, revno, rev, delta, tags=None):
70 to_file = self.to_file
71 if tags is not None:
72 self._flush()
73 print >> to_file, u"=== %s ===" % ', '.join(tags)
74 date_str = time.strftime("%Y-%m-%d", time.localtime(rev.timestamp))
75 if hasattr(rev, "get_apparent_authors"):
76 author = "\n\t ".join(rev.get_apparent_authors())
77 else:
78 author = rev.get_apparent_author().strip()
79 date_line = date_str + " " + author
80 if date_line != self._date_line:
81 self._flush()
82 self._date_line = date_line
83 self._show_changes(revno, rev, delta)
84
85 def _flush(self):
86 if self._date_line is None or not self._changes_buffer.getvalue():
87 return
88 to_file = self.to_file
89 print >> to_file, self._date_line
90 print >> to_file
91 s = self._changes_buffer.getvalue()
92 to_file.write(s)
93 self._changes_buffer.truncate(0)
94
95 def begin_log(self):
96 pass
97
98 def end_log(self):
99 self._flush()
100
101 def __del__(self): # this is evil, I need bzr to call a method on me when it's finished..
102 self._flush()
103
104 def _split_message(self, message):
105 """Split message into a shortlog plus list of paragraphs.
106 Paragraphs are identified by splitting the message where blank
107 lines appear, or simply where a newline appears, depending on
108 the SPLIT_ON_BLANK_LINES global variable.
109
110 returns: (shortlog, paragraph_list)
111 """
112 lines = message.split('\n')
113 if lines:
114 # the first line is always the shortlog
115 shortlog = lines.pop(0)
116 # the second line may be empty to separate the shortlog from the detailed message
117 if lines and not lines[0]:
118 lines.pop(0)
119 else:
120 shortlog = u'(no commit message)'
121 if SPLIT_ON_BLANK_LINES:
122 paragraphs = []
123 current_paragraph = []
124 for line in lines:
125 if not line: # empty line marks the end of a paragraph
126 paragraphs.append('\n'.join(current_paragraph))
127 current_paragraph = []
128 else:
129 current_paragraph.append(line)
130 if current_paragraph:
131 paragraphs.append('\n'.join(current_paragraph))
132 return shortlog, paragraphs
133 else:
134 return shortlog, lines
135
136 def _show_changes(self, revno, rev, delta):
137 to_file = self._changes_buffer
138 output_standalone_message = True
139 shortlog, paragraphs = self._split_message(rev.message)
140 shortlog = u"[%s] %s" % (revno, shortlog)
141 bugs_text = rev.properties.get('bugs', '')
142 if bugs_text:
143 if shortlog[-1] in '.;':
144 shortlog = shortlog[:-1]
145 shortlog = u"%s; %s" % (shortlog, bugs_text)
146
147 if delta is not None:
148 ## special case when only text modifications exist
149 if delta.modified and not (delta.added or delta.removed
150 or delta.renamed):
151 for num, (path, id, kind, text_modified, meta_modified) \
152 in enumerate(delta.modified):
153 if num == len(delta.modified) - 1:
154 for line in textwrap.wrap(
155 u"%s: %s" % (path, shortlog),
156 width=70,
157 initial_indent="* ",
158 subsequent_indent=""):
159 print >> to_file, u"\t" + line
160 else:
161 print >> to_file, u"\t* %s," % (path,)
162 if paragraphs:
163 print >> to_file
164 for parI, par in enumerate(paragraphs):
165 if par:
166 for l2 in textwrap.wrap(par, 70):
167 print >> to_file, u'\t' + l2
168 else:
169 if parI < (len(paragraphs)-1):
170 print >> to_file
171 if SPLIT_ON_BLANK_LINES:
172 print >> to_file
173 if not paragraphs or not SPLIT_ON_BLANK_LINES:
174 print >> to_file
175 output_standalone_message = False
176
177 else:
178
179 self._flush()
180
181 if delta.added:
182 for num, (path, id, kind) in enumerate(delta.added):
183 if num == len(delta.added) - 1:
184 print >> to_file, u"\t* %s: Added." % (path,)
185 print >> to_file
186 else:
187 print >> to_file, u"\t* %s," % (path,)
188
189 if delta.removed:
190 for num, (path, id, kind) in enumerate(delta.removed):
191 if num == len(delta.removed) - 1:
192 print >> to_file, u"\t* %s: Removed." % (path,)
193 print >> to_file
194 else:
195 print >> to_file, u"\t* %s," % (path,)
196
197 if delta.modified:
198 for num, (path, id, kind, text_modified, meta_modified) \
199 in enumerate(delta.modified):
200 if num == len(delta.modified) - 1:
201 print >> to_file, u"\t* %s: Modified." % (path,)
202 print >> to_file
203 else:
204 print >> to_file, u"\t* %s," % (path,)
205
206 if delta.renamed:
207 for (oldpath, newpath, id, kind,
208 text_modified, meta_modified) in delta.renamed:
209 if text_modified:
210 line = (u"%s: Renamed to %s and modified."\
211 % (oldpath, newpath))
212 else:
213 line = u"%s: Renamed to %s." % (oldpath, newpath)
214 for l1 in textwrap.wrap(line, 70,
215 initial_indent="* ",
216 subsequent_indent=" "):
217 print >> to_file, u"\t" + l1
218 print >> to_file
219
220 if output_standalone_message:
221 ## The log message, on a line by itself
222 paragraphs.insert(0, shortlog)
223 for parI, par in enumerate(paragraphs):
224 if par:
225 for l2 in textwrap.wrap(par, 70):
226 print >> to_file, u'\t' + l2
227 else:
228 if parI < (len(paragraphs)-1):
229 print >> to_file
230 if SPLIT_ON_BLANK_LINES:
231 print >> to_file
232 if not SPLIT_ON_BLANK_LINES:
233 print >> to_file
234 self._flush()
235
236
237 bzrlib.log.register_formatter('gnu', GnuFormatter)
238