220
214
'direction': 'reverse',
222
216
'generate_tags': True,
223
'exclude_common_ancestry': False,
224
217
'_match_using_deltas': True,
228
221
def make_log_request_dict(direction='reverse', specific_fileids=None,
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,
222
start_revision=None, end_revision=None, limit=None,
223
message_search=None, levels=1, generate_tags=True, delta_type=None,
224
diff_type=None, _match_using_deltas=True):
235
225
"""Convenience function for making a logging request dictionary.
237
227
Using this function may make code slightly safer by ensuring
397
383
:return: An iterator yielding LogRevision objects.
400
levels = rqst.get('levels')
401
limit = rqst.get('limit')
402
diff_type = rqst.get('diff_type')
404
387
revision_iterator = self._create_log_revision_iterator()
405
388
for revs in revision_iterator:
406
389
for (rev_id, revno, merge_depth), rev, delta in revs:
407
390
# 0 levels means show everything; merge_depth counts from 0
391
levels = rqst.get('levels')
408
392
if levels != 0 and merge_depth >= levels:
410
if diff_type is None:
413
diff = self._format_diff(rev, rev_id, diff_type)
394
diff = self._format_diff(rev, rev_id)
414
395
yield LogRevision(rev, revno, merge_depth, delta,
415
396
self.rev_tag_dict.get(rev_id), diff)
397
limit = rqst.get('limit')
418
400
if log_count >= limit:
421
def _format_diff(self, rev, rev_id, diff_type):
403
def _format_diff(self, rev, rev_id):
404
diff_type = self.rqst.get('diff_type')
405
if diff_type is None:
422
407
repo = self.branch.repository
423
408
if len(rev.parent_ids) == 0:
424
409
ancestor_id = _mod_revision.NULL_REVISION
464
448
generate_merge_revisions = rqst.get('levels') != 1
465
449
delayed_graph_generation = not rqst.get('specific_fileids') and (
466
450
rqst.get('limit') or self.start_rev_id or self.end_rev_id)
467
view_revisions = _calc_view_revisions(
468
self.branch, self.start_rev_id, self.end_rev_id,
469
rqst.get('direction'),
470
generate_merge_revisions=generate_merge_revisions,
471
delayed_graph_generation=delayed_graph_generation,
472
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
451
view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
452
self.end_rev_id, rqst.get('direction'), generate_merge_revisions,
453
delayed_graph_generation=delayed_graph_generation)
474
455
# Apply the other filters
475
456
return make_log_rev_iterator(self.branch, view_revisions,
482
463
# Note that we always generate the merge revisions because
483
464
# filter_revisions_touching_file_id() requires them ...
485
view_revisions = _calc_view_revisions(
486
self.branch, self.start_rev_id, self.end_rev_id,
487
rqst.get('direction'), generate_merge_revisions=True,
488
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
466
view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
467
self.end_rev_id, rqst.get('direction'), True)
489
468
if not isinstance(view_revisions, list):
490
469
view_revisions = list(view_revisions)
491
470
view_revisions = _filter_revisions_touching_file_id(self.branch,
498
477
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
499
generate_merge_revisions,
500
delayed_graph_generation=False,
501
exclude_common_ancestry=False,
478
generate_merge_revisions, delayed_graph_generation=False):
503
479
"""Calculate the revisions to view.
505
481
:return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
506
482
a list of the same tuples.
508
if (exclude_common_ancestry and start_rev_id == end_rev_id):
509
raise errors.BzrCommandError(
510
'--exclude-common-ancestry requires two different revisions')
511
if direction not in ('reverse', 'forward'):
512
raise ValueError('invalid direction %r' % direction)
513
484
br_revno, br_rev_id = branch.last_revision_info()
514
485
if br_revno == 0:
517
if (end_rev_id and start_rev_id == end_rev_id
518
and (not generate_merge_revisions
519
or not _has_merges(branch, end_rev_id))):
520
# If a single revision is requested, check we can handle it
521
iter_revs = _generate_one_revision(branch, end_rev_id, br_rev_id,
523
elif not generate_merge_revisions:
524
# If we only want to see linear revisions, we can iterate ...
525
iter_revs = _generate_flat_revisions(branch, start_rev_id, end_rev_id,
526
direction, exclude_common_ancestry)
527
if direction == 'forward':
528
iter_revs = reversed(iter_revs)
488
# If a single revision is requested, check we can handle it
489
generate_single_revision = (end_rev_id and start_rev_id == end_rev_id and
490
(not generate_merge_revisions or not _has_merges(branch, end_rev_id)))
491
if generate_single_revision:
492
return _generate_one_revision(branch, end_rev_id, br_rev_id, br_revno)
494
# If we only want to see linear revisions, we can iterate ...
495
if not generate_merge_revisions:
496
return _generate_flat_revisions(branch, start_rev_id, end_rev_id,
530
iter_revs = _generate_all_revisions(branch, start_rev_id, end_rev_id,
531
direction, delayed_graph_generation,
532
exclude_common_ancestry)
533
if direction == 'forward':
534
iter_revs = _rebase_merge_depth(reverse_by_depth(list(iter_revs)))
499
return _generate_all_revisions(branch, start_rev_id, end_rev_id,
500
direction, delayed_graph_generation)
538
503
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno):
545
510
return [(rev_id, revno_str, 0)]
548
def _generate_flat_revisions(branch, start_rev_id, end_rev_id, direction,
549
exclude_common_ancestry=False):
550
result = _linear_view_revisions(
551
branch, start_rev_id, end_rev_id,
552
exclude_common_ancestry=exclude_common_ancestry)
513
def _generate_flat_revisions(branch, start_rev_id, end_rev_id, direction):
514
result = _linear_view_revisions(branch, start_rev_id, end_rev_id)
553
515
# If a start limit was given and it's not obviously an
554
516
# ancestor of the end limit, check it before outputting anything
555
517
if direction == 'forward' or (start_rev_id
559
521
except _StartNotLinearAncestor:
560
522
raise errors.BzrCommandError('Start revision not found in'
561
523
' left-hand history of end revision.')
524
if direction == 'forward':
525
result = reversed(result)
565
529
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
566
delayed_graph_generation,
567
exclude_common_ancestry=False):
530
delayed_graph_generation):
568
531
# On large trees, generating the merge graph can take 30-60 seconds
569
532
# so we delay doing it until a merge is detected, incrementally
570
533
# returning initial (non-merge) revisions while we can.
572
# The above is only true for old formats (<= 0.92), for newer formats, a
573
# couple of seconds only should be needed to load the whole graph and the
574
# other graph operations needed are even faster than that -- vila 100201
575
534
initial_revisions = []
576
535
if delayed_graph_generation:
578
for rev_id, revno, depth in _linear_view_revisions(
579
branch, start_rev_id, end_rev_id, exclude_common_ancestry):
537
for rev_id, revno, depth in \
538
_linear_view_revisions(branch, start_rev_id, end_rev_id):
580
539
if _has_merges(branch, rev_id):
581
# The end_rev_id can be nested down somewhere. We need an
582
# explicit ancestry check. There is an ambiguity here as we
583
# may not raise _StartNotLinearAncestor for a revision that
584
# is an ancestor but not a *linear* one. But since we have
585
# loaded the graph to do the check (or calculate a dotted
586
# revno), we may as well accept to show the log... We need
587
# the check only if start_rev_id is not None as all
588
# revisions have _mod_revision.NULL_REVISION as an ancestor
590
graph = branch.repository.get_graph()
591
if (start_rev_id is not None
592
and not graph.is_ancestor(start_rev_id, end_rev_id)):
593
raise _StartNotLinearAncestor()
594
# Since we collected the revisions so far, we need to
596
540
end_rev_id = rev_id
599
543
initial_revisions.append((rev_id, revno, depth))
601
545
# No merged revisions found
602
return initial_revisions
546
if direction == 'reverse':
547
return initial_revisions
548
elif direction == 'forward':
549
return reversed(initial_revisions)
551
raise ValueError('invalid direction %r' % direction)
603
552
except _StartNotLinearAncestor:
604
553
# A merge was never detected so the lower revision limit can't
605
554
# be nested down somewhere
606
555
raise errors.BzrCommandError('Start revision not found in'
607
556
' history of end revision.')
609
# We exit the loop above because we encounter a revision with merges, from
610
# this revision, we need to switch to _graph_view_revisions.
612
558
# A log including nested merges is required. If the direction is reverse,
613
559
# we rebase the initial merge depths so that the development line is
614
560
# shown naturally, i.e. just like it is for linear logging. We can easily
616
562
# indented at the end seems slightly nicer in that case.
617
563
view_revisions = chain(iter(initial_revisions),
618
564
_graph_view_revisions(branch, start_rev_id, end_rev_id,
619
rebase_initial_depths=(direction == 'reverse'),
620
exclude_common_ancestry=exclude_common_ancestry))
621
return view_revisions
565
rebase_initial_depths=direction == 'reverse'))
566
if direction == 'reverse':
567
return view_revisions
568
elif direction == 'forward':
569
# Forward means oldest first, adjusting for depth.
570
view_revisions = reverse_by_depth(list(view_revisions))
571
return _rebase_merge_depth(view_revisions)
573
raise ValueError('invalid direction %r' % direction)
624
576
def _has_merges(branch, rev_id):
645
# if either start or end is not specified then we use either the first or
646
# the last revision and *they* are obvious ancestors.
650
def _linear_view_revisions(branch, start_rev_id, end_rev_id,
651
exclude_common_ancestry=False):
600
def _linear_view_revisions(branch, start_rev_id, end_rev_id):
652
601
"""Calculate a sequence of revisions to view, newest to oldest.
654
603
:param start_rev_id: the lower revision-id
655
604
:param end_rev_id: the upper revision-id
656
:param exclude_common_ancestry: Whether the start_rev_id should be part of
657
the iterated revisions.
658
605
:return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
659
606
:raises _StartNotLinearAncestor: if a start_rev_id is specified but
660
is not found walking the left-hand history
607
is not found walking the left-hand history
662
609
br_revno, br_rev_id = branch.last_revision_info()
663
610
repo = branch.repository
720
661
depth_adjustment = merge_depth
721
662
if depth_adjustment:
722
663
if merge_depth < depth_adjustment:
723
# From now on we reduce the depth adjustement, this can be
724
# surprising for users. The alternative requires two passes
725
# which breaks the fast display of the first revision
727
664
depth_adjustment = merge_depth
728
665
merge_depth -= depth_adjustment
729
666
yield rev_id, '.'.join(map(str, revno)), merge_depth
732
@deprecated_function(deprecated_in((2, 2, 0)))
733
669
def calculate_view_revisions(branch, start_revision, end_revision, direction,
734
670
specific_fileid, generate_merge_revisions):
735
671
"""Calculate the revisions to view.
1347
1290
preferred_levels = 0
1349
1292
def __init__(self, to_file, show_ids=False, show_timezone='original',
1350
delta_format=None, levels=None, show_advice=False,
1351
to_exact_file=None, author_list_handler=None):
1293
delta_format=None, levels=None, show_advice=False):
1352
1294
"""Create a LogFormatter.
1354
1296
:param to_file: the file to output to
1355
:param to_exact_file: if set, gives an output stream to which
1356
non-Unicode diffs are written.
1357
1297
:param show_ids: if True, revision-ids are to be displayed
1358
1298
:param show_timezone: the timezone to use
1359
1299
:param delta_format: the level of delta information to display
1362
1302
let the log formatter decide.
1363
1303
:param show_advice: whether to show advice at the end of the
1365
:param author_list_handler: callable generating a list of
1366
authors to display for a given revision
1368
1306
self.to_file = to_file
1369
1307
# 'exact' stream used to show diff, it should print content 'as is'
1370
1308
# and should not try to decode/encode it to unicode to avoid bug #328007
1371
if to_exact_file is not None:
1372
self.to_exact_file = to_exact_file
1374
# XXX: somewhat hacky; this assumes it's a codec writer; it's better
1375
# for code that expects to get diffs to pass in the exact file
1377
self.to_exact_file = getattr(to_file, 'stream', to_file)
1309
self.to_exact_file = getattr(to_file, 'stream', to_file)
1378
1310
self.show_ids = show_ids
1379
1311
self.show_timezone = show_timezone
1380
1312
if delta_format is None:
1424
1355
def short_author(self, rev):
1425
return self.authors(rev, 'first', short=True, sep=', ')
1427
def authors(self, rev, who, short=False, sep=None):
1428
"""Generate list of authors, taking --authors option into account.
1430
The caller has to specify the name of a author list handler,
1431
as provided by the author list registry, using the ``who``
1432
argument. That name only sets a default, though: when the
1433
user selected a different author list generation using the
1434
``--authors`` command line switch, as represented by the
1435
``author_list_handler`` constructor argument, that value takes
1438
:param rev: The revision for which to generate the list of authors.
1439
:param who: Name of the default handler.
1440
:param short: Whether to shorten names to either name or address.
1441
:param sep: What separator to use for automatic concatenation.
1443
if self._author_list_handler is not None:
1444
# The user did specify --authors, which overrides the default
1445
author_list_handler = self._author_list_handler
1447
# The user didn't specify --authors, so we use the caller's default
1448
author_list_handler = author_list_registry.get(who)
1449
names = author_list_handler(rev)
1451
for i in range(len(names)):
1452
name, address = config.parse_username(names[i])
1458
names = sep.join(names)
1356
name, address = config.parse_username(rev.get_apparent_authors()[0])
1461
1361
def merge_marker(self, revision):
1462
1362
"""Get the merge marker to include in the output or '' if none."""
1472
1372
If a registered handler raises an error it is propagated.
1474
for line in self.custom_properties(revision):
1475
self.to_file.write("%s%s\n" % (indent, line))
1477
def custom_properties(self, revision):
1478
"""Format the custom properties returned by each registered handler.
1480
If a registered handler raises an error it is propagated.
1482
:return: a list of formatted lines (excluding trailing newlines)
1484
lines = self._foreign_info_properties(revision)
1485
1374
for key, handler in properties_handler_registry.iteritems():
1486
lines.extend(self._format_properties(handler(revision)))
1489
def _foreign_info_properties(self, rev):
1490
"""Custom log displayer for foreign revision identifiers.
1492
:param rev: Revision object.
1494
# Revision comes directly from a foreign repository
1495
if isinstance(rev, foreign.ForeignRevision):
1496
return self._format_properties(
1497
rev.mapping.vcs.show_foreign_revid(rev.foreign_revid))
1499
# Imported foreign revision revision ids always contain :
1500
if not ":" in rev.revision_id:
1503
# Revision was once imported from a foreign repository
1505
foreign_revid, mapping = \
1506
foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
1507
except errors.InvalidRevisionId:
1510
return self._format_properties(
1511
mapping.vcs.show_foreign_revid(foreign_revid))
1513
def _format_properties(self, properties):
1515
for key, value in properties.items():
1516
lines.append(key + ': ' + value)
1375
for key, value in handler(revision).items():
1376
self.to_file.write(indent + key + ': ' + value + '\n')
1519
1378
def show_diff(self, to_file, diff, indent):
1520
1379
for l in diff.rstrip().split('\n'):
1521
1380
to_file.write(indent + '%s\n' % (l,))
1524
# Separator between revisions in long format
1525
_LONG_SEP = '-' * 60
1528
1383
class LongLogFormatter(LogFormatter):
1530
1385
supports_merge_revisions = True
1533
1388
supports_tags = True
1534
1389
supports_diff = True
1536
def __init__(self, *args, **kwargs):
1537
super(LongLogFormatter, self).__init__(*args, **kwargs)
1538
if self.show_timezone == 'original':
1539
self.date_string = self._date_string_original_timezone
1541
self.date_string = self._date_string_with_timezone
1543
def _date_string_with_timezone(self, rev):
1544
return format_date(rev.timestamp, rev.timezone or 0,
1547
def _date_string_original_timezone(self, rev):
1548
return format_date_with_offset_in_original_timezone(rev.timestamp,
1551
1391
def log_revision(self, revision):
1552
1392
"""Log a revision, either merged or not."""
1553
1393
indent = ' ' * revision.merge_depth
1394
to_file = self.to_file
1395
to_file.write(indent + '-' * 60 + '\n')
1555
1396
if revision.revno is not None:
1556
lines.append('revno: %s%s' % (revision.revno,
1397
to_file.write(indent + 'revno: %s%s\n' % (revision.revno,
1557
1398
self.merge_marker(revision)))
1558
1399
if revision.tags:
1559
lines.append('tags: %s' % (', '.join(revision.tags)))
1400
to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
1560
1401
if self.show_ids:
1561
lines.append('revision-id: %s' % (revision.rev.revision_id,))
1402
to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
1562
1404
for parent_id in revision.rev.parent_ids:
1563
lines.append('parent: %s' % (parent_id,))
1564
lines.extend(self.custom_properties(revision.rev))
1405
to_file.write(indent + 'parent: %s\n' % (parent_id,))
1406
self.show_properties(revision.rev, indent)
1566
1408
committer = revision.rev.committer
1567
authors = self.authors(revision.rev, 'all')
1409
authors = revision.rev.get_apparent_authors()
1568
1410
if authors != [committer]:
1569
lines.append('author: %s' % (", ".join(authors),))
1570
lines.append('committer: %s' % (committer,))
1411
to_file.write(indent + 'author: %s\n' % (", ".join(authors),))
1412
to_file.write(indent + 'committer: %s\n' % (committer,))
1572
1414
branch_nick = revision.rev.properties.get('branch-nick', None)
1573
1415
if branch_nick is not None:
1574
lines.append('branch nick: %s' % (branch_nick,))
1576
lines.append('timestamp: %s' % (self.date_string(revision.rev),))
1578
lines.append('message:')
1416
to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
1418
date_str = format_date(revision.rev.timestamp,
1419
revision.rev.timezone or 0,
1421
to_file.write(indent + 'timestamp: %s\n' % (date_str,))
1423
to_file.write(indent + 'message:\n')
1579
1424
if not revision.rev.message:
1580
lines.append(' (no message)')
1425
to_file.write(indent + ' (no message)\n')
1582
1427
message = revision.rev.message.rstrip('\r\n')
1583
1428
for l in message.split('\n'):
1584
lines.append(' %s' % (l,))
1586
# Dump the output, appending the delta and diff if requested
1587
to_file = self.to_file
1588
to_file.write("%s%s\n" % (indent, ('\n' + indent).join(lines)))
1429
to_file.write(indent + ' %s\n' % (l,))
1589
1430
if revision.delta is not None:
1590
# Use the standard status output to display changes
1591
from bzrlib.delta import report_delta
1592
report_delta(to_file, revision.delta, short_status=False,
1593
show_ids=self.show_ids, indent=indent)
1431
# We don't respect delta_format for compatibility
1432
revision.delta.show(to_file, self.show_ids, indent=indent,
1594
1434
if revision.diff is not None:
1595
1435
to_file.write(indent + 'diff:\n')
1597
1436
# Note: we explicitly don't indent the diff (relative to the
1598
1437
# revision information) so that the output can be fed to patch -p0
1599
1438
self.show_diff(self.to_exact_file, revision.diff, indent)
1600
self.to_exact_file.flush()
1602
1440
def get_advice_separator(self):
1603
1441
"""Get the text separating the log from the closing advice."""
1659
1497
to_file.write(indent + offset + '%s\n' % (l,))
1661
1499
if revision.delta is not None:
1662
# Use the standard status output to display changes
1663
from bzrlib.delta import report_delta
1664
report_delta(to_file, revision.delta,
1665
short_status=self.delta_format==1,
1666
show_ids=self.show_ids, indent=indent + offset)
1500
revision.delta.show(to_file, self.show_ids, indent=indent + offset,
1501
short_status=self.delta_format==1)
1667
1502
if revision.diff is not None:
1668
1503
self.show_diff(self.to_exact_file, revision.diff, ' ')
1669
1504
to_file.write('\n')
1816
1646
raise errors.BzrCommandError("unknown log formatter: %r" % name)
1819
def author_list_all(rev):
1820
return rev.get_apparent_authors()[:]
1823
def author_list_first(rev):
1824
lst = rev.get_apparent_authors()
1831
def author_list_committer(rev):
1832
return [rev.committer]
1835
author_list_registry = registry.Registry()
1837
author_list_registry.register('all', author_list_all,
1840
author_list_registry.register('first', author_list_first,
1843
author_list_registry.register('committer', author_list_committer,
1847
1649
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
1848
1650
# deprecated; for compatibility
1849
1651
lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
2010
1812
:param file_list: the list of paths given on the command line;
2011
1813
the first of these can be a branch location or a file path,
2012
1814
the remainder must be file paths
2013
:param add_cleanup: When the branch returned is read locked,
2014
an unlock call will be queued to the cleanup.
2015
1815
:return: (branch, info_list, start_rev_info, end_rev_info) where
2016
1816
info_list is a list of (relative_path, file_id, kind) tuples where
2017
1817
kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
2018
branch will be read-locked.
2020
from builtins import _get_revision_range
1819
from builtins import _get_revision_range, safe_relpath_files
2021
1820
tree, b, path = bzrdir.BzrDir.open_containing_tree_or_branch(file_list[0])
2022
add_cleanup(b.lock_read().unlock)
2023
1821
# XXX: It's damn messy converting a list of paths to relative paths when
2024
1822
# those paths might be deleted ones, they might be on a case-insensitive
2025
1823
# filesystem and/or they might be in silly locations (like another branch).
2105
1903
properties_handler_registry = registry.Registry()
2107
# Use the properties handlers to print out bug information if available
2108
def _bugs_properties_handler(revision):
2109
if revision.properties.has_key('bugs'):
2110
bug_lines = revision.properties['bugs'].split('\n')
2111
bug_rows = [line.split(' ', 1) for line in bug_lines]
2112
fixed_bug_urls = [row[0] for row in bug_rows if
2113
len(row) > 1 and row[1] == 'fixed']
2116
return {'fixes bug(s)': ' '.join(fixed_bug_urls)}
2119
properties_handler_registry.register('bugs_properties_handler',
2120
_bugs_properties_handler)
1904
properties_handler_registry.register_lazy("foreign",
1906
"show_foreign_properties")
2123
1909
# adapters which revision ids to log are filtered. When log is called, the