215
220
'direction': 'reverse',
217
222
'generate_tags': True,
223
'exclude_common_ancestry': False,
218
224
'_match_using_deltas': True,
222
228
def make_log_request_dict(direction='reverse', specific_fileids=None,
223
start_revision=None, end_revision=None, limit=None,
224
message_search=None, levels=1, generate_tags=True, delta_type=None,
225
diff_type=None, _match_using_deltas=True):
229
start_revision=None, end_revision=None, limit=None,
230
message_search=None, levels=1, generate_tags=True,
232
diff_type=None, _match_using_deltas=True,
233
exclude_common_ancestry=False,
226
235
"""Convenience function for making a logging request dictionary.
228
237
Using this function may make code slightly safer by ensuring
384
397
:return: An iterator yielding LogRevision objects.
400
levels = rqst.get('levels')
401
limit = rqst.get('limit')
402
diff_type = rqst.get('diff_type')
388
404
revision_iterator = self._create_log_revision_iterator()
389
405
for revs in revision_iterator:
390
406
for (rev_id, revno, merge_depth), rev, delta in revs:
391
407
# 0 levels means show everything; merge_depth counts from 0
392
levels = rqst.get('levels')
393
408
if levels != 0 and merge_depth >= levels:
395
diff = self._format_diff(rev, rev_id)
410
if diff_type is None:
413
diff = self._format_diff(rev, rev_id, diff_type)
396
414
yield LogRevision(rev, revno, merge_depth, delta,
397
415
self.rev_tag_dict.get(rev_id), diff)
398
limit = rqst.get('limit')
401
418
if log_count >= limit:
404
def _format_diff(self, rev, rev_id):
405
diff_type = self.rqst.get('diff_type')
406
if diff_type is None:
421
def _format_diff(self, rev, rev_id, diff_type):
408
422
repo = self.branch.repository
409
423
if len(rev.parent_ids) == 0:
410
424
ancestor_id = _mod_revision.NULL_REVISION
449
463
generate_merge_revisions = rqst.get('levels') != 1
450
464
delayed_graph_generation = not rqst.get('specific_fileids') and (
451
465
rqst.get('limit') or self.start_rev_id or self.end_rev_id)
452
view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
453
self.end_rev_id, rqst.get('direction'), generate_merge_revisions,
454
delayed_graph_generation=delayed_graph_generation)
466
view_revisions = _calc_view_revisions(
467
self.branch, self.start_rev_id, self.end_rev_id,
468
rqst.get('direction'),
469
generate_merge_revisions=generate_merge_revisions,
470
delayed_graph_generation=delayed_graph_generation,
471
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
456
473
# Apply the other filters
457
474
return make_log_rev_iterator(self.branch, view_revisions,
464
481
# Note that we always generate the merge revisions because
465
482
# filter_revisions_touching_file_id() requires them ...
467
view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
468
self.end_rev_id, rqst.get('direction'), True)
484
view_revisions = _calc_view_revisions(
485
self.branch, self.start_rev_id, self.end_rev_id,
486
rqst.get('direction'), generate_merge_revisions=True,
487
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
469
488
if not isinstance(view_revisions, list):
470
489
view_revisions = list(view_revisions)
471
490
view_revisions = _filter_revisions_touching_file_id(self.branch,
478
497
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
479
generate_merge_revisions, delayed_graph_generation=False):
498
generate_merge_revisions,
499
delayed_graph_generation=False,
500
exclude_common_ancestry=False,
480
502
"""Calculate the revisions to view.
482
504
:return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
483
505
a list of the same tuples.
507
if (exclude_common_ancestry and start_rev_id == end_rev_id):
508
raise errors.BzrCommandError(
509
'--exclude-common-ancestry requires two different revisions')
510
if direction not in ('reverse', 'forward'):
511
raise ValueError('invalid direction %r' % direction)
485
512
br_revno, br_rev_id = branch.last_revision_info()
486
513
if br_revno == 0:
489
# If a single revision is requested, check we can handle it
490
generate_single_revision = (end_rev_id and start_rev_id == end_rev_id and
491
(not generate_merge_revisions or not _has_merges(branch, end_rev_id)))
492
if generate_single_revision:
493
return _generate_one_revision(branch, end_rev_id, br_rev_id, br_revno)
495
# If we only want to see linear revisions, we can iterate ...
496
if not generate_merge_revisions:
497
return _generate_flat_revisions(branch, start_rev_id, end_rev_id,
516
if (end_rev_id and start_rev_id == end_rev_id
517
and (not generate_merge_revisions
518
or not _has_merges(branch, end_rev_id))):
519
# If a single revision is requested, check we can handle it
520
iter_revs = _generate_one_revision(branch, end_rev_id, br_rev_id,
522
elif not generate_merge_revisions:
523
# If we only want to see linear revisions, we can iterate ...
524
iter_revs = _generate_flat_revisions(branch, start_rev_id, end_rev_id,
526
if direction == 'forward':
527
iter_revs = reversed(iter_revs)
500
return _generate_all_revisions(branch, start_rev_id, end_rev_id,
501
direction, delayed_graph_generation)
529
iter_revs = _generate_all_revisions(branch, start_rev_id, end_rev_id,
530
direction, delayed_graph_generation,
531
exclude_common_ancestry)
532
if direction == 'forward':
533
iter_revs = _rebase_merge_depth(reverse_by_depth(list(iter_revs)))
504
537
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno):
522
555
except _StartNotLinearAncestor:
523
556
raise errors.BzrCommandError('Start revision not found in'
524
557
' left-hand history of end revision.')
525
if direction == 'forward':
526
result = reversed(result)
530
561
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
531
delayed_graph_generation):
562
delayed_graph_generation,
563
exclude_common_ancestry=False):
532
564
# On large trees, generating the merge graph can take 30-60 seconds
533
565
# so we delay doing it until a merge is detected, incrementally
534
566
# returning initial (non-merge) revisions while we can.
568
# The above is only true for old formats (<= 0.92), for newer formats, a
569
# couple of seconds only should be needed to load the whole graph and the
570
# other graph operations needed are even faster than that -- vila 100201
535
571
initial_revisions = []
536
572
if delayed_graph_generation:
538
for rev_id, revno, depth in \
539
_linear_view_revisions(branch, start_rev_id, end_rev_id):
574
for rev_id, revno, depth in _linear_view_revisions(
575
branch, start_rev_id, end_rev_id):
540
576
if _has_merges(branch, rev_id):
577
# The end_rev_id can be nested down somewhere. We need an
578
# explicit ancestry check. There is an ambiguity here as we
579
# may not raise _StartNotLinearAncestor for a revision that
580
# is an ancestor but not a *linear* one. But since we have
581
# loaded the graph to do the check (or calculate a dotted
582
# revno), we may as well accept to show the log... We need
583
# the check only if start_rev_id is not None as all
584
# revisions have _mod_revision.NULL_REVISION as an ancestor
586
graph = branch.repository.get_graph()
587
if (start_rev_id is not None
588
and not graph.is_ancestor(start_rev_id, end_rev_id)):
589
raise _StartNotLinearAncestor()
590
# Since we collected the revisions so far, we need to
541
592
end_rev_id = rev_id
544
595
initial_revisions.append((rev_id, revno, depth))
546
597
# No merged revisions found
547
if direction == 'reverse':
548
return initial_revisions
549
elif direction == 'forward':
550
return reversed(initial_revisions)
552
raise ValueError('invalid direction %r' % direction)
598
return initial_revisions
553
599
except _StartNotLinearAncestor:
554
600
# A merge was never detected so the lower revision limit can't
555
601
# be nested down somewhere
556
602
raise errors.BzrCommandError('Start revision not found in'
557
603
' history of end revision.')
605
# We exit the loop above because we encounter a revision with merges, from
606
# this revision, we need to switch to _graph_view_revisions.
559
608
# A log including nested merges is required. If the direction is reverse,
560
609
# we rebase the initial merge depths so that the development line is
561
610
# shown naturally, i.e. just like it is for linear logging. We can easily
563
612
# indented at the end seems slightly nicer in that case.
564
613
view_revisions = chain(iter(initial_revisions),
565
614
_graph_view_revisions(branch, start_rev_id, end_rev_id,
566
rebase_initial_depths=direction == 'reverse'))
567
if direction == 'reverse':
568
return view_revisions
569
elif direction == 'forward':
570
# Forward means oldest first, adjusting for depth.
571
view_revisions = reverse_by_depth(list(view_revisions))
572
return _rebase_merge_depth(view_revisions)
574
raise ValueError('invalid direction %r' % direction)
615
rebase_initial_depths=(direction == 'reverse'),
616
exclude_common_ancestry=exclude_common_ancestry))
617
return view_revisions
577
620
def _has_merges(branch, rev_id):
662
712
depth_adjustment = merge_depth
663
713
if depth_adjustment:
664
714
if merge_depth < depth_adjustment:
715
# From now on we reduce the depth adjustement, this can be
716
# surprising for users. The alternative requires two passes
717
# which breaks the fast display of the first revision
665
719
depth_adjustment = merge_depth
666
720
merge_depth -= depth_adjustment
667
721
yield rev_id, '.'.join(map(str, revno)), merge_depth
724
@deprecated_function(deprecated_in((2, 2, 0)))
670
725
def calculate_view_revisions(branch, start_revision, end_revision, direction,
671
726
specific_fileid, generate_merge_revisions):
672
727
"""Calculate the revisions to view.
1291
1340
preferred_levels = 0
1293
1342
def __init__(self, to_file, show_ids=False, show_timezone='original',
1294
delta_format=None, levels=None, show_advice=False):
1343
delta_format=None, levels=None, show_advice=False,
1344
to_exact_file=None):
1295
1345
"""Create a LogFormatter.
1297
1347
:param to_file: the file to output to
1348
:param to_exact_file: if set, gives an output stream to which
1349
non-Unicode diffs are written.
1298
1350
:param show_ids: if True, revision-ids are to be displayed
1299
1351
:param show_timezone: the timezone to use
1300
1352
:param delta_format: the level of delta information to display
1370
def show_foreign_info(self, rev, indent):
1428
def show_properties(self, revision, indent):
1429
"""Displays the custom properties returned by each registered handler.
1431
If a registered handler raises an error it is propagated.
1433
for line in self.custom_properties(revision):
1434
self.to_file.write("%s%s\n" % (indent, line))
1436
def custom_properties(self, revision):
1437
"""Format the custom properties returned by each registered handler.
1439
If a registered handler raises an error it is propagated.
1441
:return: a list of formatted lines (excluding trailing newlines)
1443
lines = self._foreign_info_properties(revision)
1444
for key, handler in properties_handler_registry.iteritems():
1445
lines.extend(self._format_properties(handler(revision)))
1448
def _foreign_info_properties(self, rev):
1371
1449
"""Custom log displayer for foreign revision identifiers.
1373
1451
:param rev: Revision object.
1375
1453
# Revision comes directly from a foreign repository
1376
1454
if isinstance(rev, foreign.ForeignRevision):
1377
self._write_properties(indent, rev.mapping.vcs.show_foreign_revid(
1455
return self._format_properties(
1456
rev.mapping.vcs.show_foreign_revid(rev.foreign_revid))
1381
1458
# Imported foreign revision revision ids always contain :
1382
1459
if not ":" in rev.revision_id:
1385
1462
# Revision was once imported from a foreign repository
1387
1464
foreign_revid, mapping = \
1388
1465
foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
1389
1466
except errors.InvalidRevisionId:
1392
self._write_properties(indent,
1469
return self._format_properties(
1393
1470
mapping.vcs.show_foreign_revid(foreign_revid))
1395
def show_properties(self, revision, indent):
1396
"""Displays the custom properties returned by each registered handler.
1398
If a registered handler raises an error it is propagated.
1400
for key, handler in properties_handler_registry.iteritems():
1401
self._write_properties(indent, handler(revision))
1403
def _write_properties(self, indent, properties):
1472
def _format_properties(self, properties):
1404
1474
for key, value in properties.items():
1405
self.to_file.write(indent + key + ': ' + value + '\n')
1475
lines.append(key + ': ' + value)
1407
1478
def show_diff(self, to_file, diff, indent):
1408
1479
for l in diff.rstrip().split('\n'):
1409
1480
to_file.write(indent + '%s\n' % (l,))
1483
# Separator between revisions in long format
1484
_LONG_SEP = '-' * 60
1412
1487
class LongLogFormatter(LogFormatter):
1414
1489
supports_merge_revisions = True
1417
1492
supports_tags = True
1418
1493
supports_diff = True
1495
def __init__(self, *args, **kwargs):
1496
super(LongLogFormatter, self).__init__(*args, **kwargs)
1497
if self.show_timezone == 'original':
1498
self.date_string = self._date_string_original_timezone
1500
self.date_string = self._date_string_with_timezone
1502
def _date_string_with_timezone(self, rev):
1503
return format_date(rev.timestamp, rev.timezone or 0,
1506
def _date_string_original_timezone(self, rev):
1507
return format_date_with_offset_in_original_timezone(rev.timestamp,
1420
1510
def log_revision(self, revision):
1421
1511
"""Log a revision, either merged or not."""
1422
1512
indent = ' ' * revision.merge_depth
1423
to_file = self.to_file
1424
to_file.write(indent + '-' * 60 + '\n')
1425
1514
if revision.revno is not None:
1426
to_file.write(indent + 'revno: %s%s\n' % (revision.revno,
1515
lines.append('revno: %s%s' % (revision.revno,
1427
1516
self.merge_marker(revision)))
1428
1517
if revision.tags:
1429
to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
1518
lines.append('tags: %s' % (', '.join(revision.tags)))
1430
1519
if self.show_ids:
1431
to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
1520
lines.append('revision-id: %s' % (revision.rev.revision_id,))
1433
1521
for parent_id in revision.rev.parent_ids:
1434
to_file.write(indent + 'parent: %s\n' % (parent_id,))
1435
self.show_foreign_info(revision.rev, indent)
1436
self.show_properties(revision.rev, indent)
1522
lines.append('parent: %s' % (parent_id,))
1523
lines.extend(self.custom_properties(revision.rev))
1438
1525
committer = revision.rev.committer
1439
1526
authors = revision.rev.get_apparent_authors()
1440
1527
if authors != [committer]:
1441
to_file.write(indent + 'author: %s\n' % (", ".join(authors),))
1442
to_file.write(indent + 'committer: %s\n' % (committer,))
1528
lines.append('author: %s' % (", ".join(authors),))
1529
lines.append('committer: %s' % (committer,))
1444
1531
branch_nick = revision.rev.properties.get('branch-nick', None)
1445
1532
if branch_nick is not None:
1446
to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
1448
date_str = format_date(revision.rev.timestamp,
1449
revision.rev.timezone or 0,
1451
to_file.write(indent + 'timestamp: %s\n' % (date_str,))
1453
to_file.write(indent + 'message:\n')
1533
lines.append('branch nick: %s' % (branch_nick,))
1535
lines.append('timestamp: %s' % (self.date_string(revision.rev),))
1537
lines.append('message:')
1454
1538
if not revision.rev.message:
1455
to_file.write(indent + ' (no message)\n')
1539
lines.append(' (no message)')
1457
1541
message = revision.rev.message.rstrip('\r\n')
1458
1542
for l in message.split('\n'):
1459
to_file.write(indent + ' %s\n' % (l,))
1543
lines.append(' %s' % (l,))
1545
# Dump the output, appending the delta and diff if requested
1546
to_file = self.to_file
1547
to_file.write("%s%s\n" % (indent, ('\n' + indent).join(lines)))
1460
1548
if revision.delta is not None:
1461
# We don't respect delta_format for compatibility
1462
revision.delta.show(to_file, self.show_ids, indent=indent,
1549
# Use the standard status output to display changes
1550
from bzrlib.delta import report_delta
1551
report_delta(to_file, revision.delta, short_status=False,
1552
show_ids=self.show_ids, indent=indent)
1464
1553
if revision.diff is not None:
1465
1554
to_file.write(indent + 'diff:\n')
1466
1556
# Note: we explicitly don't indent the diff (relative to the
1467
1557
# revision information) so that the output can be fed to patch -p0
1468
1558
self.show_diff(self.to_exact_file, revision.diff, indent)
1559
self.to_exact_file.flush()
1470
1561
def get_advice_separator(self):
1471
1562
"""Get the text separating the log from the closing advice."""
1528
1618
to_file.write(indent + offset + '%s\n' % (l,))
1530
1620
if revision.delta is not None:
1531
revision.delta.show(to_file, self.show_ids, indent=indent + offset,
1532
short_status=self.delta_format==1)
1621
# Use the standard status output to display changes
1622
from bzrlib.delta import report_delta
1623
report_delta(to_file, revision.delta,
1624
short_status=self.delta_format==1,
1625
show_ids=self.show_ids, indent=indent + offset)
1533
1626
if revision.diff is not None:
1534
1627
self.show_diff(self.to_exact_file, revision.diff, ' ')
1535
1628
to_file.write('\n')
1936
2033
properties_handler_registry = registry.Registry()
2035
# Use the properties handlers to print out bug information if available
2036
def _bugs_properties_handler(revision):
2037
if revision.properties.has_key('bugs'):
2038
bug_lines = revision.properties['bugs'].split('\n')
2039
bug_rows = [line.split(' ', 1) for line in bug_lines]
2040
fixed_bug_urls = [row[0] for row in bug_rows if
2041
len(row) > 1 and row[1] == 'fixed']
2044
return {'fixes bug(s)': ' '.join(fixed_bug_urls)}
2047
properties_handler_registry.register('bugs_properties_handler',
2048
_bugs_properties_handler)
1939
2051
# adapters which revision ids to log are filtered. When log is called, the
1940
2052
# log_rev_iterator is adapted through each of these factory methods.