~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/log.py

  • Committer: IWATA Hidetaka
  • Date: 2010-12-26 13:19:11 UTC
  • mto: This revision was merged to the branch mainline in revision 5593.
  • Revision ID: iwata0303@gmail.com-20101226131911-o7txs0fnji5zekq1
add icon resources tbzrcommand(w)

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
    diff,
71
71
    errors,
72
72
    foreign,
 
73
    osutils,
73
74
    repository as _mod_repository,
74
75
    revision as _mod_revision,
75
76
    revisionspec,
85
86
    format_date,
86
87
    format_date_with_offset_in_original_timezone,
87
88
    get_terminal_encoding,
88
 
    re_compile_checked,
89
89
    terminal_width,
90
90
    )
91
91
from bzrlib.symbol_versioning import (
220
220
    'direction': 'reverse',
221
221
    'levels': 1,
222
222
    'generate_tags': True,
 
223
    'exclude_common_ancestry': False,
223
224
    '_match_using_deltas': True,
224
225
    }
225
226
 
226
227
 
227
228
def make_log_request_dict(direction='reverse', specific_fileids=None,
228
 
    start_revision=None, end_revision=None, limit=None,
229
 
    message_search=None, levels=1, generate_tags=True, delta_type=None,
230
 
    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,
 
231
                          delta_type=None,
 
232
                          diff_type=None, _match_using_deltas=True,
 
233
                          exclude_common_ancestry=False,
 
234
                          ):
231
235
    """Convenience function for making a logging request dictionary.
232
236
 
233
237
    Using this function may make code slightly safer by ensuring
271
275
      algorithm used for matching specific_fileids. This parameter
272
276
      may be removed in the future so bzrlib client code should NOT
273
277
      use it.
 
278
 
 
279
    :param exclude_common_ancestry: Whether -rX..Y should be interpreted as a
 
280
      range operator or as a graph difference.
274
281
    """
275
282
    return {
276
283
        'direction': direction,
283
290
        'generate_tags': generate_tags,
284
291
        'delta_type': delta_type,
285
292
        'diff_type': diff_type,
 
293
        'exclude_common_ancestry': exclude_common_ancestry,
286
294
        # Add 'private' attributes for features that may be deprecated
287
295
        '_match_using_deltas': _match_using_deltas,
288
296
    }
424
432
        else:
425
433
            specific_files = None
426
434
        s = StringIO()
 
435
        path_encoding = osutils.get_diff_header_encoding()
427
436
        diff.show_diff_trees(tree_1, tree_2, s, specific_files, old_label='',
428
 
            new_label='')
 
437
            new_label='', path_encoding=path_encoding)
429
438
        return s.getvalue()
430
439
 
431
440
    def _create_log_revision_iterator(self):
455
464
        generate_merge_revisions = rqst.get('levels') != 1
456
465
        delayed_graph_generation = not rqst.get('specific_fileids') and (
457
466
                rqst.get('limit') or self.start_rev_id or self.end_rev_id)
458
 
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
459
 
            self.end_rev_id, rqst.get('direction'), generate_merge_revisions,
460
 
            delayed_graph_generation=delayed_graph_generation)
 
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'))
461
473
 
462
474
        # Apply the other filters
463
475
        return make_log_rev_iterator(self.branch, view_revisions,
470
482
        # Note that we always generate the merge revisions because
471
483
        # filter_revisions_touching_file_id() requires them ...
472
484
        rqst = self.rqst
473
 
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
474
 
            self.end_rev_id, rqst.get('direction'), True)
 
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'))
475
489
        if not isinstance(view_revisions, list):
476
490
            view_revisions = list(view_revisions)
477
491
        view_revisions = _filter_revisions_touching_file_id(self.branch,
482
496
 
483
497
 
484
498
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
485
 
    generate_merge_revisions, delayed_graph_generation=False):
 
499
                         generate_merge_revisions,
 
500
                         delayed_graph_generation=False,
 
501
                         exclude_common_ancestry=False,
 
502
                         ):
486
503
    """Calculate the revisions to view.
487
504
 
488
505
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
489
506
             a list of the same tuples.
490
507
    """
 
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)
491
513
    br_revno, br_rev_id = branch.last_revision_info()
492
514
    if br_revno == 0:
493
515
        return []
494
516
 
495
 
    # If a single revision is requested, check we can handle it
496
 
    generate_single_revision = (end_rev_id and start_rev_id == end_rev_id and
497
 
        (not generate_merge_revisions or not _has_merges(branch, end_rev_id)))
498
 
    if generate_single_revision:
499
 
        return _generate_one_revision(branch, end_rev_id, br_rev_id, br_revno)
500
 
 
501
 
    # If we only want to see linear revisions, we can iterate ...
502
 
    if not generate_merge_revisions:
503
 
        return _generate_flat_revisions(branch, start_rev_id, end_rev_id,
504
 
            direction)
 
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,
 
522
                                           br_revno)
 
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)
505
529
    else:
506
 
        return _generate_all_revisions(branch, start_rev_id, end_rev_id,
507
 
            direction, delayed_graph_generation)
 
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)))
 
535
    return iter_revs
508
536
 
509
537
 
510
538
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno):
517
545
        return [(rev_id, revno_str, 0)]
518
546
 
519
547
 
520
 
def _generate_flat_revisions(branch, start_rev_id, end_rev_id, direction):
521
 
    result = _linear_view_revisions(branch, start_rev_id, end_rev_id)
 
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)
522
553
    # If a start limit was given and it's not obviously an
523
554
    # ancestor of the end limit, check it before outputting anything
524
555
    if direction == 'forward' or (start_rev_id
528
559
        except _StartNotLinearAncestor:
529
560
            raise errors.BzrCommandError('Start revision not found in'
530
561
                ' left-hand history of end revision.')
531
 
    if direction == 'forward':
532
 
        result = reversed(result)
533
562
    return result
534
563
 
535
564
 
536
565
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
537
 
                            delayed_graph_generation):
 
566
                            delayed_graph_generation,
 
567
                            exclude_common_ancestry=False):
538
568
    # On large trees, generating the merge graph can take 30-60 seconds
539
569
    # so we delay doing it until a merge is detected, incrementally
540
570
    # returning initial (non-merge) revisions while we can.
546
576
    if delayed_graph_generation:
547
577
        try:
548
578
            for rev_id, revno, depth in  _linear_view_revisions(
549
 
                branch, start_rev_id, end_rev_id):
 
579
                branch, start_rev_id, end_rev_id, exclude_common_ancestry):
550
580
                if _has_merges(branch, rev_id):
551
581
                    # The end_rev_id can be nested down somewhere. We need an
552
582
                    # explicit ancestry check. There is an ambiguity here as we
569
599
                    initial_revisions.append((rev_id, revno, depth))
570
600
            else:
571
601
                # No merged revisions found
572
 
                if direction == 'reverse':
573
 
                    return initial_revisions
574
 
                elif direction == 'forward':
575
 
                    return reversed(initial_revisions)
576
 
                else:
577
 
                    raise ValueError('invalid direction %r' % direction)
 
602
                return initial_revisions
578
603
        except _StartNotLinearAncestor:
579
604
            # A merge was never detected so the lower revision limit can't
580
605
            # be nested down somewhere
591
616
    # indented at the end seems slightly nicer in that case.
592
617
    view_revisions = chain(iter(initial_revisions),
593
618
        _graph_view_revisions(branch, start_rev_id, end_rev_id,
594
 
                              rebase_initial_depths=(direction == 'reverse')))
595
 
    if direction == 'reverse':
596
 
        return view_revisions
597
 
    elif direction == 'forward':
598
 
        # Forward means oldest first, adjusting for depth.
599
 
        view_revisions = reverse_by_depth(list(view_revisions))
600
 
        return _rebase_merge_depth(view_revisions)
601
 
    else:
602
 
        raise ValueError('invalid direction %r' % direction)
 
619
                              rebase_initial_depths=(direction == 'reverse'),
 
620
                              exclude_common_ancestry=exclude_common_ancestry))
 
621
    return view_revisions
603
622
 
604
623
 
605
624
def _has_merges(branch, rev_id):
628
647
    return True
629
648
 
630
649
 
631
 
def _linear_view_revisions(branch, start_rev_id, end_rev_id):
 
650
def _linear_view_revisions(branch, start_rev_id, end_rev_id,
 
651
                           exclude_common_ancestry=False):
632
652
    """Calculate a sequence of revisions to view, newest to oldest.
633
653
 
634
654
    :param start_rev_id: the lower revision-id
635
655
    :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.
636
658
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
637
659
    :raises _StartNotLinearAncestor: if a start_rev_id is specified but
638
 
      is not found walking the left-hand history
 
660
        is not found walking the left-hand history
639
661
    """
640
662
    br_revno, br_rev_id = branch.last_revision_info()
641
663
    repo = branch.repository
652
674
            revno = branch.revision_id_to_dotted_revno(revision_id)
653
675
            revno_str = '.'.join(str(n) for n in revno)
654
676
            if not found_start and revision_id == start_rev_id:
655
 
                yield revision_id, revno_str, 0
 
677
                if not exclude_common_ancestry:
 
678
                    yield revision_id, revno_str, 0
656
679
                found_start = True
657
680
                break
658
681
            else:
663
686
 
664
687
 
665
688
def _graph_view_revisions(branch, start_rev_id, end_rev_id,
666
 
                          rebase_initial_depths=True):
 
689
                          rebase_initial_depths=True,
 
690
                          exclude_common_ancestry=False):
667
691
    """Calculate revisions to view including merges, newest to oldest.
668
692
 
669
693
    :param branch: the branch
673
697
      revision is found?
674
698
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
675
699
    """
 
700
    if exclude_common_ancestry:
 
701
        stop_rule = 'with-merges-without-common-ancestry'
 
702
    else:
 
703
        stop_rule = 'with-merges'
676
704
    view_revisions = branch.iter_merge_sorted_revisions(
677
705
        start_revision_id=end_rev_id, stop_revision_id=start_rev_id,
678
 
        stop_rule="with-merges")
 
706
        stop_rule=stop_rule)
679
707
    if not rebase_initial_depths:
680
708
        for (rev_id, merge_depth, revno, end_of_merge
681
709
             ) in view_revisions:
782
810
    """
783
811
    if search is None:
784
812
        return log_rev_iterator
785
 
    searchRE = re_compile_checked(search, re.IGNORECASE,
786
 
            'log message filter')
 
813
    searchRE = re.compile(search, re.IGNORECASE)
787
814
    return _filter_message_re(searchRE, log_rev_iterator)
788
815
 
789
816
 
1321
1348
 
1322
1349
    def __init__(self, to_file, show_ids=False, show_timezone='original',
1323
1350
                 delta_format=None, levels=None, show_advice=False,
1324
 
                 to_exact_file=None):
 
1351
                 to_exact_file=None, author_list_handler=None):
1325
1352
        """Create a LogFormatter.
1326
1353
 
1327
1354
        :param to_file: the file to output to
1335
1362
          let the log formatter decide.
1336
1363
        :param show_advice: whether to show advice at the end of the
1337
1364
          log or not
 
1365
        :param author_list_handler: callable generating a list of
 
1366
          authors to display for a given revision
1338
1367
        """
1339
1368
        self.to_file = to_file
1340
1369
        # 'exact' stream used to show diff, it should print content 'as is'
1355
1384
        self.levels = levels
1356
1385
        self._show_advice = show_advice
1357
1386
        self._merge_count = 0
 
1387
        self._author_list_handler = author_list_handler
1358
1388
 
1359
1389
    def get_levels(self):
1360
1390
        """Get the number of levels to display or 0 for all."""
1392
1422
        return address
1393
1423
 
1394
1424
    def short_author(self, rev):
1395
 
        name, address = config.parse_username(rev.get_apparent_authors()[0])
1396
 
        if name:
1397
 
            return name
1398
 
        return address
 
1425
        return self.authors(rev, 'first', short=True, sep=', ')
 
1426
 
 
1427
    def authors(self, rev, who, short=False, sep=None):
 
1428
        """Generate list of authors, taking --authors option into account.
 
1429
 
 
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
 
1436
        precedence.
 
1437
 
 
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.
 
1442
        """
 
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
 
1446
        else:
 
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)
 
1450
        if short:
 
1451
            for i in range(len(names)):
 
1452
                name, address = config.parse_username(names[i])
 
1453
                if name:
 
1454
                    names[i] = name
 
1455
                else:
 
1456
                    names[i] = address
 
1457
        if sep is not None:
 
1458
            names = sep.join(names)
 
1459
        return names
1399
1460
 
1400
1461
    def merge_marker(self, revision):
1401
1462
        """Get the merge marker to include in the output or '' if none."""
1503
1564
        lines.extend(self.custom_properties(revision.rev))
1504
1565
 
1505
1566
        committer = revision.rev.committer
1506
 
        authors = revision.rev.get_apparent_authors()
 
1567
        authors = self.authors(revision.rev, 'all')
1507
1568
        if authors != [committer]:
1508
1569
            lines.append('author: %s' % (", ".join(authors),))
1509
1570
        lines.append('committer: %s' % (committer,))
1526
1587
        to_file = self.to_file
1527
1588
        to_file.write("%s%s\n" % (indent, ('\n' + indent).join(lines)))
1528
1589
        if revision.delta is not None:
1529
 
            # We don't respect delta_format for compatibility
1530
 
            revision.delta.show(to_file, self.show_ids, indent=indent,
1531
 
                                short_status=False)
 
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)
1532
1594
        if revision.diff is not None:
1533
1595
            to_file.write(indent + 'diff:\n')
1534
1596
            to_file.flush()
1597
1659
                to_file.write(indent + offset + '%s\n' % (l,))
1598
1660
 
1599
1661
        if revision.delta is not None:
1600
 
            revision.delta.show(to_file, self.show_ids, indent=indent + offset,
1601
 
                                short_status=self.delta_format==1)
 
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)
1602
1667
        if revision.diff is not None:
1603
1668
            self.show_diff(self.to_exact_file, revision.diff, '      ')
1604
1669
        to_file.write('\n')
1679
1744
                               self.show_timezone,
1680
1745
                               date_fmt='%Y-%m-%d',
1681
1746
                               show_offset=False)
1682
 
        committer_str = revision.rev.committer.replace (' <', '  <')
 
1747
        committer_str = self.authors(revision.rev, 'first', sep=', ')
 
1748
        committer_str = committer_str.replace(' <', '  <')
1683
1749
        to_file.write('%s  %s\n\n' % (date_str,committer_str))
1684
1750
 
1685
1751
        if revision.delta is not None and revision.delta.has_changed():
1750
1816
        raise errors.BzrCommandError("unknown log formatter: %r" % name)
1751
1817
 
1752
1818
 
 
1819
def author_list_all(rev):
 
1820
    return rev.get_apparent_authors()[:]
 
1821
 
 
1822
 
 
1823
def author_list_first(rev):
 
1824
    lst = rev.get_apparent_authors()
 
1825
    try:
 
1826
        return [lst[0]]
 
1827
    except IndexError:
 
1828
        return []
 
1829
 
 
1830
 
 
1831
def author_list_committer(rev):
 
1832
    return [rev.committer]
 
1833
 
 
1834
 
 
1835
author_list_registry = registry.Registry()
 
1836
 
 
1837
author_list_registry.register('all', author_list_all,
 
1838
                              'All authors')
 
1839
 
 
1840
author_list_registry.register('first', author_list_first,
 
1841
                              'The first author')
 
1842
 
 
1843
author_list_registry.register('committer', author_list_committer,
 
1844
                              'The committer')
 
1845
 
 
1846
 
1753
1847
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
1754
1848
    # deprecated; for compatibility
1755
1849
    lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
1906
2000
        lf.log_revision(lr)
1907
2001
 
1908
2002
 
1909
 
def _get_info_for_log_files(revisionspec_list, file_list):
 
2003
def _get_info_for_log_files(revisionspec_list, file_list, add_cleanup):
1910
2004
    """Find file-ids and kinds given a list of files and a revision range.
1911
2005
 
1912
2006
    We search for files at the end of the range. If not found there,
1916
2010
    :param file_list: the list of paths given on the command line;
1917
2011
      the first of these can be a branch location or a file path,
1918
2012
      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.
1919
2015
    :return: (branch, info_list, start_rev_info, end_rev_info) where
1920
2016
      info_list is a list of (relative_path, file_id, kind) tuples where
1921
2017
      kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
1922
2018
      branch will be read-locked.
1923
2019
    """
1924
 
    from builtins import _get_revision_range, safe_relpath_files
 
2020
    from builtins import _get_revision_range
1925
2021
    tree, b, path = bzrdir.BzrDir.open_containing_tree_or_branch(file_list[0])
1926
 
    b.lock_read()
 
2022
    add_cleanup(b.lock_read().unlock)
1927
2023
    # XXX: It's damn messy converting a list of paths to relative paths when
1928
2024
    # those paths might be deleted ones, they might be on a case-insensitive
1929
2025
    # filesystem and/or they might be in silly locations (like another branch).
1933
2029
    # case of running log in a nested directory, assuming paths beyond the
1934
2030
    # first one haven't been deleted ...
1935
2031
    if tree:
1936
 
        relpaths = [path] + safe_relpath_files(tree, file_list[1:])
 
2032
        relpaths = [path] + tree.safe_relpath_files(file_list[1:])
1937
2033
    else:
1938
2034
        relpaths = [path] + file_list[1:]
1939
2035
    info_list = []