| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
| 48 |
import sys |
| 49 |
import os |
| 50 |
|
| 51 |
import bzrlib.log |
| 52 |
|
| 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): |
| 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 |
|
| 115 |
shortlog = lines.pop(0) |
| 116 |
|
| 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: |
| 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 |
|
| 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 |
|
| 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 |
|