200
166
direction='reverse',
201
167
start_revision=None,
202
168
end_revision=None,
205
170
"""Worker function for show_log - see show_log."""
171
from bzrlib.osutils import format_date
172
from bzrlib.errors import BzrCheckError
173
from bzrlib.textui import show_status
175
from warnings import warn
206
177
if not isinstance(lf, LogFormatter):
207
178
warn("not a LogFormatter instance: %r" % lf)
209
180
if specific_fileid:
210
mutter('get log for file_id %r', specific_fileid)
181
mutter('get log for file_id %r' % specific_fileid)
212
183
if search is not None:
213
185
searchRE = re.compile(search, re.IGNORECASE)
217
mainline_revs, rev_nos, start_rev_id, end_rev_id = \
218
_get_mainline_revs(branch, start_revision, end_revision)
219
if not mainline_revs:
222
if direction == 'reverse':
223
start_rev_id, end_rev_id = end_rev_id, start_rev_id
225
legacy_lf = getattr(lf, 'log_revision', None) is None
227
# pre-0.17 formatters use show for mainline revisions.
228
# how should we show merged revisions ?
229
# pre-0.11 api: show_merge
230
# 0.11-0.16 api: show_merge_revno
231
show_merge_revno = getattr(lf, 'show_merge_revno', None)
232
show_merge = getattr(lf, 'show_merge', None)
233
if show_merge is None and show_merge_revno is None:
234
# no merged-revno support
235
generate_merge_revisions = False
237
generate_merge_revisions = True
238
# tell developers to update their code
239
symbol_versioning.warn('LogFormatters should provide log_revision '
240
'instead of show and show_merge_revno since bzr 0.17.',
241
DeprecationWarning, stacklevel=3)
243
generate_merge_revisions = getattr(lf, 'supports_merge_revisions',
245
generate_single_revision = False
246
if ((not generate_merge_revisions)
247
and ((start_rev_id and (start_rev_id not in rev_nos))
248
or (end_rev_id and (end_rev_id not in rev_nos)))):
249
generate_single_revision = ((start_rev_id == end_rev_id)
250
and getattr(lf, 'supports_single_merge_revision', False))
251
if not generate_single_revision:
252
raise BzrCommandError('Selected log formatter only supports '
253
'mainline revisions.')
254
generate_merge_revisions = generate_single_revision
255
view_revs_iter = get_view_revisions(mainline_revs, rev_nos, branch,
256
direction, include_merges=generate_merge_revisions)
257
view_revisions = _filter_revision_range(list(view_revs_iter),
260
if view_revisions and generate_single_revision:
261
view_revisions = view_revisions[0:1]
263
view_revisions = _filter_revisions_touching_file_id(branch,
268
# rebase merge_depth - unless there are no revisions or
269
# either the first or last revision have merge_depth = 0.
270
if view_revisions and view_revisions[0][2] and view_revisions[-1][2]:
271
min_depth = min([d for r,n,d in view_revisions])
273
view_revisions = [(r,n,d-min_depth) for r,n,d in view_revisions]
276
generate_tags = getattr(lf, 'supports_tags', False)
278
if branch.supports_tags():
279
rev_tag_dict = branch.tags.get_reverse_tag_dict()
281
generate_delta = verbose and getattr(lf, 'supports_delta', False)
283
def iter_revisions():
284
# r = revision, n = revno, d = merge depth
285
revision_ids = [r for r, n, d in view_revisions]
287
repository = branch.repository
290
revisions = repository.get_revisions(revision_ids[:num])
292
deltas = repository.get_deltas_for_revisions(revisions)
293
cur_deltas = dict(izip((r.revision_id for r in revisions),
295
for revision in revisions:
296
yield revision, cur_deltas.get(revision.revision_id)
297
revision_ids = revision_ids[num:]
298
num = min(int(num * 1.5), 200)
300
# now we just print all the revisions
302
for ((rev_id, revno, merge_depth), (rev, delta)) in \
303
izip(view_revisions, iter_revisions()):
306
if not searchRE.search(rev.message):
310
lr = LogRevision(rev, revno, merge_depth, delta,
311
rev_tag_dict.get(rev_id))
314
# support for legacy (pre-0.17) LogFormatters
317
lf.show(revno, rev, delta, rev_tag_dict.get(rev_id))
319
lf.show(revno, rev, delta)
321
if show_merge_revno is None:
322
lf.show_merge(rev, merge_depth)
325
lf.show_merge_revno(rev, merge_depth, revno,
326
rev_tag_dict.get(rev_id))
328
lf.show_merge_revno(rev, merge_depth, revno)
331
if log_count >= limit:
335
def _get_mainline_revs(branch, start_revision, end_revision):
336
"""Get the mainline revisions from the branch.
338
Generates the list of mainline revisions for the branch.
340
:param branch: The branch containing the revisions.
342
:param start_revision: The first revision to be logged.
343
For backwards compatibility this may be a mainline integer revno,
344
but for merge revision support a RevisionInfo is expected.
346
:param end_revision: The last revision to be logged.
347
For backwards compatibility this may be a mainline integer revno,
348
but for merge revision support a RevisionInfo is expected.
350
:return: A (mainline_revs, rev_nos, start_rev_id, end_rev_id) tuple.
352
189
which_revs = _enumerate_history(branch)
354
return None, None, None, None
356
# For mainline generation, map start_revision and end_revision to
357
# mainline revnos. If the revision is not on the mainline choose the
358
# appropriate extreme of the mainline instead - the extra will be
360
# Also map the revisions to rev_ids, to be used in the later filtering
363
191
if start_revision is None:
366
if isinstance(start_revision,RevisionInfo):
367
start_rev_id = start_revision.rev_id
368
start_revno = start_revision.revno or 1
370
branch.check_real_revno(start_revision)
371
start_revno = start_revision
194
branch.check_real_revno(start_revision)
374
196
if end_revision is None:
375
end_revno = len(which_revs)
197
end_revision = len(which_revs)
377
if isinstance(end_revision,RevisionInfo):
378
end_rev_id = end_revision.rev_id
379
end_revno = end_revision.revno or len(which_revs)
381
branch.check_real_revno(end_revision)
382
end_revno = end_revision
384
if ((start_rev_id == NULL_REVISION)
385
or (end_rev_id == NULL_REVISION)):
386
raise BzrCommandError('Logging revision 0 is invalid.')
387
if start_revno > end_revno:
388
raise BzrCommandError("Start revision must be older than "
199
branch.check_real_revno(end_revision)
391
201
# list indexes are 0-based; revisions are 1-based
392
cut_revs = which_revs[(start_revno-1):(end_revno)]
394
return None, None, None, None
396
# convert the revision history to a dictionary:
397
rev_nos = dict((k, v) for v, k in cut_revs)
399
# override the mainline to look like the revision history.
400
mainline_revs = [revision_id for index, revision_id in cut_revs]
401
if cut_revs[0][0] == 1:
402
mainline_revs.insert(0, None)
202
cut_revs = which_revs[(start_revision-1):(end_revision)]
204
if direction == 'reverse':
206
elif direction == 'forward':
404
mainline_revs.insert(0, which_revs[start_revno-2][1])
405
return mainline_revs, rev_nos, start_rev_id, end_rev_id
408
def _filter_revision_range(view_revisions, start_rev_id, end_rev_id):
409
"""Filter view_revisions based on revision ranges.
411
:param view_revisions: A list of (revision_id, dotted_revno, merge_depth)
412
tuples to be filtered.
414
:param start_rev_id: If not NONE specifies the first revision to be logged.
415
If NONE then all revisions up to the end_rev_id are logged.
417
:param end_rev_id: If not NONE specifies the last revision to be logged.
418
If NONE then all revisions up to the end of the log are logged.
420
:return: The filtered view_revisions.
422
if start_rev_id or end_rev_id:
423
revision_ids = [r for r, n, d in view_revisions]
425
start_index = revision_ids.index(start_rev_id)
428
if start_rev_id == end_rev_id:
429
end_index = start_index
432
end_index = revision_ids.index(end_rev_id)
434
end_index = len(view_revisions) - 1
435
# To include the revisions merged into the last revision,
436
# extend end_rev_id down to, but not including, the next rev
437
# with the same or lesser merge_depth
438
end_merge_depth = view_revisions[end_index][2]
440
for index in xrange(end_index+1, len(view_revisions)+1):
441
if view_revisions[index][2] <= end_merge_depth:
442
end_index = index - 1
445
# if the search falls off the end then log to the end as well
446
end_index = len(view_revisions) - 1
447
view_revisions = view_revisions[start_index:end_index+1]
448
return view_revisions
451
def _filter_revisions_touching_file_id(branch, file_id, mainline_revisions,
453
"""Return the list of revision ids which touch a given file id.
455
The function filters view_revisions and returns a subset.
456
This includes the revisions which directly change the file id,
457
and the revisions which merge these changes. So if the
465
And 'C' changes a file, then both C and D will be returned.
467
This will also can be restricted based on a subset of the mainline.
469
:return: A list of (revision_id, dotted_revno, merge_depth) tuples.
471
# find all the revisions that change the specific file
472
file_weave = branch.repository.weave_store.get_weave(file_id,
473
branch.repository.get_transaction())
474
weave_modifed_revisions = set(file_weave.versions())
475
# build the ancestry of each revision in the graph
476
# - only listing the ancestors that change the specific file.
477
rev_graph = branch.repository.get_revision_graph(mainline_revisions[-1])
478
sorted_rev_list = topo_sort(rev_graph)
480
for rev in sorted_rev_list:
481
parents = rev_graph[rev]
482
if rev not in weave_modifed_revisions and len(parents) == 1:
483
# We will not be adding anything new, so just use a reference to
484
# the parent ancestry.
485
rev_ancestry = ancestry[parents[0]]
488
if rev in weave_modifed_revisions:
489
rev_ancestry.add(rev)
490
for parent in parents:
491
rev_ancestry = rev_ancestry.union(ancestry[parent])
492
ancestry[rev] = rev_ancestry
494
def is_merging_rev(r):
495
parents = rev_graph[r]
497
leftparent = parents[0]
498
for rightparent in parents[1:]:
499
if not ancestry[leftparent].issuperset(
500
ancestry[rightparent]):
504
# filter from the view the revisions that did not change or merge
506
return [(r, n, d) for r, n, d in view_revs_iter
507
if r in weave_modifed_revisions or is_merging_rev(r)]
510
def get_view_revisions(mainline_revs, rev_nos, branch, direction,
511
include_merges=True):
512
"""Produce an iterator of revisions to show
513
:return: an iterator of (revision_id, revno, merge_depth)
514
(if there is no revno for a revision, None is supplied)
516
if include_merges is False:
517
revision_ids = mainline_revs[1:]
518
if direction == 'reverse':
519
revision_ids.reverse()
520
for revision_id in revision_ids:
521
yield revision_id, str(rev_nos[revision_id]), 0
523
merge_sorted_revisions = merge_sort(
524
branch.repository.get_revision_graph(mainline_revs[-1]),
529
if direction == 'forward':
530
# forward means oldest first.
531
merge_sorted_revisions = reverse_by_depth(merge_sorted_revisions)
532
elif direction != 'reverse':
533
209
raise ValueError('invalid direction %r' % direction)
535
for sequence, rev_id, merge_depth, revno, end_of_merge in merge_sorted_revisions:
536
yield rev_id, '.'.join(map(str, revno)), merge_depth
539
def reverse_by_depth(merge_sorted_revisions, _depth=0):
540
"""Reverse revisions by depth.
542
Revisions with a different depth are sorted as a group with the previous
543
revision of that depth. There may be no topological justification for this,
544
but it looks much nicer.
547
for val in merge_sorted_revisions:
549
zd_revisions.append([val])
211
revision_history = branch.revision_history()
212
for revno, rev_id in cut_revs:
213
if verbose or specific_fileid:
214
delta = _get_revision_delta(branch, revno)
217
if not delta.touches_file_id(specific_fileid):
221
# although we calculated it, throw it away without display
224
rev = branch.get_revision(rev_id)
227
if not searchRE.search(rev.message):
230
lf.show(revno, rev, delta)
231
if hasattr(lf, 'show_merge'):
235
# revno is 1 based, so -2 to get back 1 less.
236
excludes = set(branch.get_ancestry(revision_history[revno - 2]))
237
pending = list(rev.parent_ids)
239
rev_id = pending.pop()
240
if rev_id in excludes:
242
# prevent showing merged revs twice if they multi-path.
245
rev = branch.get_revision(rev_id)
246
except errors.NoSuchRevision:
248
pending.extend(rev.parent_ids)
252
def deltas_for_log_dummy(branch, which_revs):
253
"""Return all the revisions without intermediate deltas.
255
Useful for log commands that won't need the delta information.
258
for revno, revision_id in which_revs:
259
yield revno, branch.get_revision(revision_id), None
262
def deltas_for_log_reverse(branch, which_revs):
263
"""Compute deltas for display in latest-to-earliest order.
269
Sequence of (revno, revision_id) for the subset of history to examine
272
Sequence of (revno, rev, delta)
274
The delta is from the given revision to the next one in the
275
sequence, which makes sense if the log is being displayed from
278
last_revno = last_revision_id = last_tree = None
279
for revno, revision_id in which_revs:
280
this_tree = branch.revision_tree(revision_id)
281
this_revision = branch.get_revision(revision_id)
284
yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
286
this_tree = EmptyTree(branch.get_root_id())
289
last_revision = this_revision
290
last_tree = this_tree
294
this_tree = EmptyTree(branch.get_root_id())
551
assert val[2] > _depth
552
zd_revisions[-1].append(val)
553
for revisions in zd_revisions:
554
if len(revisions) > 1:
555
revisions[1:] = reverse_by_depth(revisions[1:], _depth + 1)
556
zd_revisions.reverse()
558
for chunk in zd_revisions:
563
class LogRevision(object):
564
"""A revision to be logged (by LogFormatter.log_revision).
566
A simple wrapper for the attributes of a revision to be logged.
567
The attributes may or may not be populated, as determined by the
568
logging options and the log formatter capabilities.
296
this_revno = last_revno - 1
297
this_revision_id = branch.revision_history()[this_revno]
298
this_tree = branch.revision_tree(this_revision_id)
299
yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
302
def deltas_for_log_forward(branch, which_revs):
303
"""Compute deltas for display in forward log.
305
Given a sequence of (revno, revision_id) pairs, return
308
The delta is from the given revision to the next one in the
309
sequence, which makes sense if the log is being displayed from
571
def __init__(self, rev=None, revno=None, merge_depth=0, delta=None,
575
self.merge_depth = merge_depth
312
last_revno = last_revision_id = last_tree = None
313
prev_tree = EmptyTree(branch.get_root_id())
315
for revno, revision_id in which_revs:
316
this_tree = branch.revision_tree(revision_id)
317
this_revision = branch.get_revision(revision_id)
321
last_tree = EmptyTree(branch.get_root_id())
323
last_revno = revno - 1
324
last_revision_id = branch.revision_history()[last_revno]
325
last_tree = branch.revision_tree(last_revision_id)
327
yield revno, this_revision, compare_trees(last_tree, this_tree, False)
330
last_revision = this_revision
331
last_tree = this_tree
580
334
class LogFormatter(object):
581
"""Abstract class to display log messages.
583
At a minimum, a derived class must implement the log_revision method.
585
If the LogFormatter needs to be informed of the beginning or end of
586
a log it should implement the begin_log and/or end_log hook methods.
588
A LogFormatter should define the following supports_XXX flags
589
to indicate which LogRevision attributes it supports:
591
- supports_delta must be True if this log formatter supports delta.
592
Otherwise the delta attribute may not be populated.
593
- supports_merge_revisions must be True if this log formatter supports
594
merge revisions. If not, and if supports_single_merge_revisions is
595
also not True, then only mainline revisions will be passed to the
597
- supports_single_merge_revision must be True if this log formatter
598
supports logging only a single merge revision. This flag is
599
only relevant if supports_merge_revisions is not True.
600
- supports_tags must be True if this log formatter supports tags.
601
Otherwise the tags attribute may not be populated.
335
"""Abstract class to display log messages."""
604
336
def __init__(self, to_file, show_ids=False, show_timezone='original'):
605
337
self.to_file = to_file
606
338
self.show_ids = show_ids
607
339
self.show_timezone = show_timezone
609
# TODO: uncomment this block after show() has been removed.
610
# Until then defining log_revision would prevent _show_log calling show()
611
# in legacy formatters.
612
# def log_revision(self, revision):
615
# :param revision: The LogRevision to be logged.
617
# raise NotImplementedError('not implemented in abstract base')
619
@deprecated_method(zero_seventeen)
620
342
def show(self, revno, rev, delta):
621
343
raise NotImplementedError('not implemented in abstract base')
623
def short_committer(self, rev):
624
name, address = config.parse_username(rev.committer)
629
def short_author(self, rev):
630
name, address = config.parse_username(rev.get_apparent_author())
636
346
class LongLogFormatter(LogFormatter):
638
supports_merge_revisions = True
639
supports_delta = True
642
@deprecated_method(zero_seventeen)
643
def show(self, revno, rev, delta, tags=None):
644
lr = LogRevision(rev, revno, 0, delta, tags)
645
return self.log_revision(lr)
647
@deprecated_method(zero_seventeen)
648
def show_merge_revno(self, rev, merge_depth, revno, tags=None):
649
"""Show a merged revision rev, with merge_depth and a revno."""
650
lr = LogRevision(rev, revno, merge_depth, tags=tags)
651
return self.log_revision(lr)
653
def log_revision(self, revision):
654
"""Log a revision, either merged or not."""
655
indent = ' ' * revision.merge_depth
656
to_file = self.to_file
657
to_file.write(indent + '-' * 60 + '\n')
658
if revision.revno is not None:
659
to_file.write(indent + 'revno: %s\n' % (revision.revno,))
661
to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
663
to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
665
for parent_id in revision.rev.parent_ids:
666
to_file.write(indent + 'parent: %s\n' % (parent_id,))
668
author = revision.rev.properties.get('author', None)
669
if author is not None:
670
to_file.write(indent + 'author: %s\n' % (author,))
671
to_file.write(indent + 'committer: %s\n' % (revision.rev.committer,))
673
branch_nick = revision.rev.properties.get('branch-nick', None)
674
if branch_nick is not None:
675
to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
677
date_str = format_date(revision.rev.timestamp,
678
revision.rev.timezone or 0,
680
to_file.write(indent + 'timestamp: %s\n' % (date_str,))
682
to_file.write(indent + 'message:\n')
683
if not revision.rev.message:
684
to_file.write(indent + ' (no message)\n')
686
message = revision.rev.message.rstrip('\r\n')
687
for l in message.split('\n'):
688
to_file.write(indent + ' %s\n' % (l,))
689
if revision.delta is not None:
690
revision.delta.show(to_file, self.show_ids, indent=indent)
347
def show(self, revno, rev, delta):
348
from osutils import format_date
350
to_file = self.to_file
352
print >>to_file, '-' * 60
353
print >>to_file, 'revno:', revno
355
print >>to_file, 'revision-id:', rev.revision_id
357
for parent_id in rev.parent_ids:
358
print >>to_file, 'parent:', parent_id
360
print >>to_file, 'committer:', rev.committer
362
date_str = format_date(rev.timestamp,
365
print >>to_file, 'timestamp: %s' % date_str
367
print >>to_file, 'message:'
369
print >>to_file, ' (no message)'
371
for l in rev.message.split('\n'):
372
print >>to_file, ' ' + l
375
delta.show(to_file, self.show_ids)
377
def show_merge(self, rev):
378
from osutils import format_date
380
to_file = self.to_file
384
print >>to_file, indent+'-' * 60
385
print >>to_file, indent+'merged:', rev.revision_id
387
for parent_id in rev.parent_ids:
388
print >>to_file, indent+'parent:', parent_id
390
print >>to_file, indent+'committer:', rev.committer
392
date_str = format_date(rev.timestamp,
395
print >>to_file, indent+'timestamp: %s' % date_str
397
print >>to_file, indent+'message:'
399
print >>to_file, indent+' (no message)'
401
for l in rev.message.split('\n'):
402
print >>to_file, indent+' ' + l
693
405
class ShortLogFormatter(LogFormatter):
695
supports_delta = True
696
supports_single_merge_revision = True
698
@deprecated_method(zero_seventeen)
699
406
def show(self, revno, rev, delta):
700
lr = LogRevision(rev, revno, 0, delta)
701
return self.log_revision(lr)
407
from bzrlib.osutils import format_date
703
def log_revision(self, revision):
704
409
to_file = self.to_file
705
date_str = format_date(revision.rev.timestamp,
706
revision.rev.timezone or 0,
709
if len(revision.rev.parent_ids) > 1:
710
is_merge = ' [merge]'
711
to_file.write("%5s %s\t%s%s\n" % (revision.revno,
712
self.short_author(revision.rev),
713
format_date(revision.rev.timestamp,
714
revision.rev.timezone or 0,
715
self.show_timezone, date_fmt="%Y-%m-%d",
410
date_str = format_date(rev.timestamp, rev.timezone or 0,
412
print >>to_file, "%5d %s\t%s" % (revno, rev.committer,
413
format_date(rev.timestamp, rev.timezone or 0,
718
415
if self.show_ids:
719
to_file.write(' revision-id:%s\n' % (revision.rev.revision_id,))
720
if not revision.rev.message:
721
to_file.write(' (no message)\n')
416
print >>to_file, ' revision-id:', rev.revision_id
418
print >>to_file, ' (no message)'
723
message = revision.rev.message.rstrip('\r\n')
724
for l in message.split('\n'):
725
to_file.write(' %s\n' % (l,))
420
for l in rev.message.split('\n'):
421
print >>to_file, ' ' + l
727
423
# TODO: Why not show the modified files in a shorter form as
728
424
# well? rewrap them single lines of appropriate length
729
if revision.delta is not None:
730
revision.delta.show(to_file, self.show_ids)
426
delta.show(to_file, self.show_ids)
734
429
class LineLogFormatter(LogFormatter):
736
supports_single_merge_revision = True
738
def __init__(self, *args, **kwargs):
739
super(LineLogFormatter, self).__init__(*args, **kwargs)
740
self._max_chars = terminal_width() - 1
742
430
def truncate(self, str, max_len):
743
431
if len(str) <= max_len:
745
433
return str[:max_len-3]+'...'
747
435
def date_string(self, rev):
436
from bzrlib.osutils import format_date
748
437
return format_date(rev.timestamp, rev.timezone or 0,
749
438
self.show_timezone, date_fmt="%Y-%m-%d",
750
439
show_offset=False)