1
# Copyright (C) 2005 Canonical Ltd
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.
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.
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
19
"""Code to show logs of changes.
21
Various flavors of log can be produced:
23
* for one file, or the whole tree, and (not done yet) for
24
files in a given directory
26
* in "verbose" mode with a description of what changed from one
29
* with file-ids and revision-ids shown
31
* from last to first or (not anymore) from first to last;
32
the default is "reversed" because it shows the likely most
33
relevant and interesting information first
35
* (not yet) in XML format
39
from bzrlib.tree import EmptyTree
40
from bzrlib.delta import compare_trees
41
from bzrlib.trace import mutter
44
def find_touching_revisions(branch, file_id):
45
"""Yield a description of revisions which affect the file_id.
47
Each returned element is (revno, revision_id, description)
49
This is the list of revisions where the file is either added,
50
modified, renamed or deleted.
52
TODO: Perhaps some way to limit this to only particular revisions,
53
or to traverse a non-mainline set of revisions?
58
for revision_id in branch.revision_history():
59
this_inv = branch.get_revision_inventory(revision_id)
60
if file_id in this_inv:
61
this_ie = this_inv[file_id]
62
this_path = this_inv.id2path(file_id)
64
this_ie = this_path = None
66
# now we know how it was last time, and how it is in this revision.
67
# are those two states effectively the same or not?
69
if not this_ie and not last_ie:
70
# not present in either
72
elif this_ie and not last_ie:
73
yield revno, revision_id, "added " + this_path
74
elif not this_ie and last_ie:
76
yield revno, revision_id, "deleted " + last_path
77
elif this_path != last_path:
78
yield revno, revision_id, ("renamed %s => %s" % (last_path, this_path))
79
elif (this_ie.text_size != last_ie.text_size
80
or this_ie.text_sha1 != last_ie.text_sha1):
81
yield revno, revision_id, "modified " + this_path
97
"""Write out human-readable log of commits to this branch.
100
LogFormatter object to show the output.
103
If true, list only the commits affecting the specified
104
file, rather than all commits.
107
If true show added/changed/deleted/renamed files.
110
'reverse' (default) is latest to earliest;
111
'forward' is earliest to latest.
114
If not None, only show revisions >= start_revision
117
If not None, only show revisions <= end_revision
119
from bzrlib.osutils import format_date
120
from bzrlib.errors import BzrCheckError
121
from bzrlib.textui import show_status
123
from warnings import warn
125
if not isinstance(lf, LogFormatter):
126
warn("not a LogFormatter instance: %r" % lf)
129
mutter('get log for file_id %r' % specific_fileid)
131
if search is not None:
133
searchRE = re.compile(search, re.IGNORECASE)
137
which_revs = branch.enum_history(direction)
138
which_revs = [x for x in which_revs if (
139
(start_revision is None or x[0] >= start_revision)
140
and (end_revision is None or x[0] <= end_revision))]
142
if not (verbose or specific_fileid):
143
# no need to know what changed between revisions
144
with_deltas = deltas_for_log_dummy(branch, which_revs)
145
elif direction == 'reverse':
146
with_deltas = deltas_for_log_reverse(branch, which_revs)
148
with_deltas = deltas_for_log_forward(branch, which_revs)
150
for revno, rev, delta in with_deltas:
152
if not delta.touches_file_id(specific_fileid):
156
# although we calculated it, throw it away without display
159
if searchRE is None or searchRE.search(rev.message):
160
lf.show(revno, rev, delta)
164
def deltas_for_log_dummy(branch, which_revs):
165
"""Return all the revisions without intermediate deltas.
167
Useful for log commands that won't need the delta information.
170
for revno, revision_id in which_revs:
171
yield revno, branch.get_revision(revision_id), None
174
def deltas_for_log_reverse(branch, which_revs):
175
"""Compute deltas for display in latest-to-earliest order.
181
Sequence of (revno, revision_id) for the subset of history to examine
184
Sequence of (revno, rev, delta)
186
The delta is from the given revision to the next one in the
187
sequence, which makes sense if the log is being displayed from
190
last_revno = last_revision_id = last_tree = None
191
for revno, revision_id in which_revs:
192
this_tree = branch.revision_tree(revision_id)
193
this_revision = branch.get_revision(revision_id)
196
yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
198
this_tree = EmptyTree(branch.get_root_id())
201
last_revision = this_revision
202
last_tree = this_tree
206
this_tree = EmptyTree(branch.get_root_id())
208
this_revno = last_revno - 1
209
this_revision_id = branch.revision_history()[this_revno]
210
this_tree = branch.revision_tree(this_revision_id)
211
yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
214
def deltas_for_log_forward(branch, which_revs):
215
"""Compute deltas for display in forward log.
217
Given a sequence of (revno, revision_id) pairs, return
220
The delta is from the given revision to the next one in the
221
sequence, which makes sense if the log is being displayed from
224
last_revno = last_revision_id = last_tree = None
225
prev_tree = EmptyTree(branch.get_root_id())
227
for revno, revision_id in which_revs:
228
this_tree = branch.revision_tree(revision_id)
229
this_revision = branch.get_revision(revision_id)
233
last_tree = EmptyTree(branch.get_root_id())
235
last_revno = revno - 1
236
last_revision_id = branch.revision_history()[last_revno]
237
last_tree = branch.revision_tree(last_revision_id)
239
yield revno, this_revision, compare_trees(last_tree, this_tree, False)
242
last_revision = this_revision
243
last_tree = this_tree
246
class LogFormatter(object):
247
"""Abstract class to display log messages."""
248
def __init__(self, to_file, show_ids=False, show_timezone='original'):
249
self.to_file = to_file
250
self.show_ids = show_ids
251
self.show_timezone = show_timezone
254
def show(self, revno, rev, delta):
255
raise NotImplementedError('not implemented in abstract base')
262
class LongLogFormatter(LogFormatter):
263
def show(self, revno, rev, delta):
264
from osutils import format_date
266
to_file = self.to_file
268
print >>to_file, '-' * 60
269
print >>to_file, 'revno:', revno
271
print >>to_file, 'revision-id:', rev.revision_id
272
print >>to_file, 'committer:', rev.committer
274
date_str = format_date(rev.timestamp,
277
print >>to_file, 'timestamp: %s' % date_str
279
print >>to_file, 'message:'
281
print >>to_file, ' (no message)'
283
for l in rev.message.split('\n'):
284
print >>to_file, ' ' + l
287
delta.show(to_file, self.show_ids)
291
class ShortLogFormatter(LogFormatter):
292
def show(self, revno, rev, delta):
293
from bzrlib.osutils import format_date
295
to_file = self.to_file
297
print >>to_file, "%5d %s\t%s" % (revno, rev.committer,
298
format_date(rev.timestamp, rev.timezone or 0,
301
print >>to_file, ' revision-id:', rev.revision_id
303
print >>to_file, ' (no message)'
305
for l in rev.message.split('\n'):
306
print >>to_file, ' ' + l
309
delta.show(to_file, self.show_ids)
314
FORMATTERS = {'long': LongLogFormatter,
315
'short': ShortLogFormatter,
319
def log_formatter(name, *args, **kwargs):
320
from bzrlib.errors import BzrCommandError
323
return FORMATTERS[name](*args, **kwargs)
325
raise BzrCommandError("unknown log formatter: %r" % name)
327
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
328
# deprecated; for compatability
329
lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
330
lf.show(revno, rev, delta)