/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/log.py

  • Committer: Ian Clatworthy
  • Date: 2009-07-13 06:58:49 UTC
  • mto: (4527.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4529.
  • Revision ID: ian.clatworthy@canonical.com-20090713065849-n7g2rsjyl6dt1mgv
Apply review feedback

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
83
83
    )
84
84
from bzrlib.osutils import (
85
85
    format_date,
86
 
    format_date_with_offset_in_original_timezone,
87
86
    get_terminal_encoding,
88
87
    re_compile_checked,
89
88
    terminal_width,
90
89
    )
91
 
from bzrlib.symbol_versioning import (
92
 
    deprecated_function,
93
 
    deprecated_in,
94
 
    )
95
90
 
96
91
 
97
92
def find_touching_revisions(branch, file_id):
109
104
    last_path = None
110
105
    revno = 1
111
106
    for revision_id in branch.revision_history():
112
 
        this_inv = branch.repository.get_inventory(revision_id)
 
107
        this_inv = branch.repository.get_revision_inventory(revision_id)
113
108
        if file_id in this_inv:
114
109
            this_ie = this_inv[file_id]
115
110
            this_path = this_inv.id2path(file_id)
220
215
    'direction': 'reverse',
221
216
    'levels': 1,
222
217
    'generate_tags': True,
223
 
    'exclude_common_ancestry': False,
224
218
    '_match_using_deltas': True,
225
219
    }
226
220
 
227
221
 
228
222
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,
231
 
                          delta_type=None,
232
 
                          diff_type=None, _match_using_deltas=True,
233
 
                          exclude_common_ancestry=False,
234
 
                          ):
 
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):
235
226
    """Convenience function for making a logging request dictionary.
236
227
 
237
228
    Using this function may make code slightly safer by ensuring
275
266
      algorithm used for matching specific_fileids. This parameter
276
267
      may be removed in the future so bzrlib client code should NOT
277
268
      use it.
278
 
 
279
 
    :param exclude_common_ancestry: Whether -rX..Y should be interpreted as a
280
 
      range operator or as a graph difference.
281
269
    """
282
270
    return {
283
271
        'direction': direction,
290
278
        'generate_tags': generate_tags,
291
279
        'delta_type': delta_type,
292
280
        'diff_type': diff_type,
293
 
        'exclude_common_ancestry': exclude_common_ancestry,
294
281
        # Add 'private' attributes for features that may be deprecated
295
282
        '_match_using_deltas': _match_using_deltas,
296
283
    }
316
303
 
317
304
 
318
305
class Logger(object):
319
 
    """An object that generates, formats and displays a log."""
 
306
    """An object the generates, formats and displays a log."""
320
307
 
321
308
    def __init__(self, branch, rqst):
322
309
        """Create a Logger.
397
384
        :return: An iterator yielding LogRevision objects.
398
385
        """
399
386
        rqst = self.rqst
400
 
        levels = rqst.get('levels')
401
 
        limit = rqst.get('limit')
402
 
        diff_type = rqst.get('diff_type')
403
387
        log_count = 0
404
388
        revision_iterator = self._create_log_revision_iterator()
405
389
        for revs in revision_iterator:
406
390
            for (rev_id, revno, merge_depth), rev, delta in revs:
407
391
                # 0 levels means show everything; merge_depth counts from 0
 
392
                levels = rqst.get('levels')
408
393
                if levels != 0 and merge_depth >= levels:
409
394
                    continue
410
 
                if diff_type is None:
411
 
                    diff = None
412
 
                else:
413
 
                    diff = self._format_diff(rev, rev_id, diff_type)
 
395
                diff = self._format_diff(rev, rev_id)
414
396
                yield LogRevision(rev, revno, merge_depth, delta,
415
397
                    self.rev_tag_dict.get(rev_id), diff)
 
398
                limit = rqst.get('limit')
416
399
                if limit:
417
400
                    log_count += 1
418
401
                    if log_count >= limit:
419
402
                        return
420
403
 
421
 
    def _format_diff(self, rev, rev_id, diff_type):
 
404
    def _format_diff(self, rev, rev_id):
 
405
        diff_type = self.rqst.get('diff_type')
 
406
        if diff_type is None:
 
407
            return None
422
408
        repo = self.branch.repository
423
409
        if len(rev.parent_ids) == 0:
424
410
            ancestor_id = _mod_revision.NULL_REVISION
463
449
        generate_merge_revisions = rqst.get('levels') != 1
464
450
        delayed_graph_generation = not rqst.get('specific_fileids') and (
465
451
                rqst.get('limit') or self.start_rev_id or self.end_rev_id)
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'))
 
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)
472
455
 
473
456
        # Apply the other filters
474
457
        return make_log_rev_iterator(self.branch, view_revisions,
481
464
        # Note that we always generate the merge revisions because
482
465
        # filter_revisions_touching_file_id() requires them ...
483
466
        rqst = self.rqst
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'))
 
467
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
 
468
            self.end_rev_id, rqst.get('direction'), True)
488
469
        if not isinstance(view_revisions, list):
489
470
            view_revisions = list(view_revisions)
490
471
        view_revisions = _filter_revisions_touching_file_id(self.branch,
495
476
 
496
477
 
497
478
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
498
 
                         generate_merge_revisions,
499
 
                         delayed_graph_generation=False,
500
 
                         exclude_common_ancestry=False,
501
 
                         ):
 
479
    generate_merge_revisions, delayed_graph_generation=False):
502
480
    """Calculate the revisions to view.
503
481
 
504
482
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
505
483
             a list of the same tuples.
506
484
    """
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)
512
485
    br_revno, br_rev_id = branch.last_revision_info()
513
486
    if br_revno == 0:
514
487
        return []
515
488
 
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,
521
 
                                           br_revno)
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,
525
 
                                             direction)
526
 
        if direction == 'forward':
527
 
            iter_revs = reversed(iter_revs)
 
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)
 
494
 
 
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,
 
498
            direction)
528
499
    else:
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)))
534
 
    return iter_revs
 
500
        return _generate_all_revisions(branch, start_rev_id, end_rev_id,
 
501
            direction, delayed_graph_generation)
535
502
 
536
503
 
537
504
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno):
555
522
        except _StartNotLinearAncestor:
556
523
            raise errors.BzrCommandError('Start revision not found in'
557
524
                ' left-hand history of end revision.')
 
525
    if direction == 'forward':
 
526
        result = reversed(result)
558
527
    return result
559
528
 
560
529
 
561
530
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
562
 
                            delayed_graph_generation,
563
 
                            exclude_common_ancestry=False):
 
531
    delayed_graph_generation):
564
532
    # On large trees, generating the merge graph can take 30-60 seconds
565
533
    # so we delay doing it until a merge is detected, incrementally
566
534
    # returning initial (non-merge) revisions while we can.
567
 
 
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
571
535
    initial_revisions = []
572
536
    if delayed_graph_generation:
573
537
        try:
574
 
            for rev_id, revno, depth in  _linear_view_revisions(
575
 
                branch, start_rev_id, end_rev_id):
 
538
            for rev_id, revno, depth in \
 
539
                _linear_view_revisions(branch, start_rev_id, end_rev_id):
576
540
                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
585
 
                    # -- vila 20100319
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
591
 
                    # adjust end_rev_id.
592
541
                    end_rev_id = rev_id
593
542
                    break
594
543
                else:
595
544
                    initial_revisions.append((rev_id, revno, depth))
596
545
            else:
597
546
                # No merged revisions found
598
 
                return initial_revisions
 
547
                if direction == 'reverse':
 
548
                    return initial_revisions
 
549
                elif direction == 'forward':
 
550
                    return reversed(initial_revisions)
 
551
                else:
 
552
                    raise ValueError('invalid direction %r' % direction)
599
553
        except _StartNotLinearAncestor:
600
554
            # A merge was never detected so the lower revision limit can't
601
555
            # be nested down somewhere
602
556
            raise errors.BzrCommandError('Start revision not found in'
603
557
                ' history of end revision.')
604
558
 
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.
607
 
 
608
559
    # A log including nested merges is required. If the direction is reverse,
609
560
    # we rebase the initial merge depths so that the development line is
610
561
    # shown naturally, i.e. just like it is for linear logging. We can easily
612
563
    # indented at the end seems slightly nicer in that case.
613
564
    view_revisions = chain(iter(initial_revisions),
614
565
        _graph_view_revisions(branch, start_rev_id, end_rev_id,
615
 
                              rebase_initial_depths=(direction == 'reverse'),
616
 
                              exclude_common_ancestry=exclude_common_ancestry))
617
 
    return view_revisions
 
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)
 
573
    else:
 
574
        raise ValueError('invalid direction %r' % direction)
618
575
 
619
576
 
620
577
def _has_merges(branch, rev_id):
638
595
        else:
639
596
            # not obvious
640
597
            return False
641
 
    # if either start or end is not specified then we use either the first or
642
 
    # the last revision and *they* are obvious ancestors.
643
598
    return True
644
599
 
645
600
 
678
633
 
679
634
 
680
635
def _graph_view_revisions(branch, start_rev_id, end_rev_id,
681
 
                          rebase_initial_depths=True,
682
 
                          exclude_common_ancestry=False):
 
636
    rebase_initial_depths=True):
683
637
    """Calculate revisions to view including merges, newest to oldest.
684
638
 
685
639
    :param branch: the branch
689
643
      revision is found?
690
644
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
691
645
    """
692
 
    if exclude_common_ancestry:
693
 
        stop_rule = 'with-merges-without-common-ancestry'
694
 
    else:
695
 
        stop_rule = 'with-merges'
696
646
    view_revisions = branch.iter_merge_sorted_revisions(
697
647
        start_revision_id=end_rev_id, stop_revision_id=start_rev_id,
698
 
        stop_rule=stop_rule)
 
648
        stop_rule="with-merges")
699
649
    if not rebase_initial_depths:
700
650
        for (rev_id, merge_depth, revno, end_of_merge
701
651
             ) in view_revisions:
712
662
                depth_adjustment = merge_depth
713
663
            if depth_adjustment:
714
664
                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
718
 
                    # though.
719
665
                    depth_adjustment = merge_depth
720
666
                merge_depth -= depth_adjustment
721
667
            yield rev_id, '.'.join(map(str, revno)), merge_depth
722
668
 
723
669
 
724
 
@deprecated_function(deprecated_in((2, 2, 0)))
725
670
def calculate_view_revisions(branch, start_revision, end_revision, direction,
726
671
        specific_fileid, generate_merge_revisions):
727
672
    """Calculate the revisions to view.
729
674
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
730
675
             a list of the same tuples.
731
676
    """
 
677
    # This method is no longer called by the main code path.
 
678
    # It is retained for API compatibility and may be deprecated
 
679
    # soon. IGC 20090116
732
680
    start_rev_id, end_rev_id = _get_revision_limits(branch, start_revision,
733
681
        end_revision)
734
682
    view_revisions = list(_calc_view_revisions(branch, start_rev_id, end_rev_id,
1084
1032
    return mainline_revs, rev_nos, start_rev_id, end_rev_id
1085
1033
 
1086
1034
 
1087
 
@deprecated_function(deprecated_in((2, 2, 0)))
1088
1035
def _filter_revision_range(view_revisions, start_rev_id, end_rev_id):
1089
1036
    """Filter view_revisions based on revision ranges.
1090
1037
 
1099
1046
 
1100
1047
    :return: The filtered view_revisions.
1101
1048
    """
 
1049
    # This method is no longer called by the main code path.
 
1050
    # It may be removed soon. IGC 20090127
1102
1051
    if start_rev_id or end_rev_id:
1103
1052
        revision_ids = [r for r, n, d in view_revisions]
1104
1053
        if start_rev_id:
1210
1159
    return result
1211
1160
 
1212
1161
 
1213
 
@deprecated_function(deprecated_in((2, 2, 0)))
1214
1162
def get_view_revisions(mainline_revs, rev_nos, branch, direction,
1215
1163
                       include_merges=True):
1216
1164
    """Produce an iterator of revisions to show
1217
1165
    :return: an iterator of (revision_id, revno, merge_depth)
1218
1166
    (if there is no revno for a revision, None is supplied)
1219
1167
    """
 
1168
    # This method is no longer called by the main code path.
 
1169
    # It is retained for API compatibility and may be deprecated
 
1170
    # soon. IGC 20090127
1220
1171
    if not include_merges:
1221
1172
        revision_ids = mainline_revs[1:]
1222
1173
        if direction == 'reverse':
1340
1291
    preferred_levels = 0
1341
1292
 
1342
1293
    def __init__(self, to_file, show_ids=False, show_timezone='original',
1343
 
                 delta_format=None, levels=None, show_advice=False,
1344
 
                 to_exact_file=None, author_list_handler=None):
 
1294
                 delta_format=None, levels=None, show_advice=False):
1345
1295
        """Create a LogFormatter.
1346
1296
 
1347
1297
        :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.
1350
1298
        :param show_ids: if True, revision-ids are to be displayed
1351
1299
        :param show_timezone: the timezone to use
1352
1300
        :param delta_format: the level of delta information to display
1355
1303
          let the log formatter decide.
1356
1304
        :param show_advice: whether to show advice at the end of the
1357
1305
          log or not
1358
 
        :param author_list_handler: callable generating a list of
1359
 
          authors to display for a given revision
1360
1306
        """
1361
1307
        self.to_file = to_file
1362
1308
        # 'exact' stream used to show diff, it should print content 'as is'
1363
1309
        # and should not try to decode/encode it to unicode to avoid bug #328007
1364
 
        if to_exact_file is not None:
1365
 
            self.to_exact_file = to_exact_file
1366
 
        else:
1367
 
            # XXX: somewhat hacky; this assumes it's a codec writer; it's better
1368
 
            # for code that expects to get diffs to pass in the exact file
1369
 
            # stream
1370
 
            self.to_exact_file = getattr(to_file, 'stream', to_file)
 
1310
        self.to_exact_file = getattr(to_file, 'stream', to_file)
1371
1311
        self.show_ids = show_ids
1372
1312
        self.show_timezone = show_timezone
1373
1313
        if delta_format is None:
1377
1317
        self.levels = levels
1378
1318
        self._show_advice = show_advice
1379
1319
        self._merge_count = 0
1380
 
        self._author_list_handler = author_list_handler
1381
1320
 
1382
1321
    def get_levels(self):
1383
1322
        """Get the number of levels to display or 0 for all."""
1415
1354
        return address
1416
1355
 
1417
1356
    def short_author(self, rev):
1418
 
        return self.authors(rev, 'first', short=True, sep=', ')
1419
 
 
1420
 
    def authors(self, rev, who, short=False, sep=None):
1421
 
        """Generate list of authors, taking --authors option into account.
1422
 
 
1423
 
        The caller has to specify the name of a author list handler,
1424
 
        as provided by the author list registry, using the ``who``
1425
 
        argument.  That name only sets a default, though: when the
1426
 
        user selected a different author list generation using the
1427
 
        ``--authors`` command line switch, as represented by the
1428
 
        ``author_list_handler`` constructor argument, that value takes
1429
 
        precedence.
1430
 
 
1431
 
        :param rev: The revision for which to generate the list of authors.
1432
 
        :param who: Name of the default handler.
1433
 
        :param short: Whether to shorten names to either name or address.
1434
 
        :param sep: What separator to use for automatic concatenation.
1435
 
        """
1436
 
        if self._author_list_handler is not None:
1437
 
            # The user did specify --authors, which overrides the default
1438
 
            author_list_handler = self._author_list_handler
1439
 
        else:
1440
 
            # The user didn't specify --authors, so we use the caller's default
1441
 
            author_list_handler = author_list_registry.get(who)
1442
 
        names = author_list_handler(rev)
1443
 
        if short:
1444
 
            for i in range(len(names)):
1445
 
                name, address = config.parse_username(names[i])
1446
 
                if name:
1447
 
                    names[i] = name
1448
 
                else:
1449
 
                    names[i] = address
1450
 
        if sep is not None:
1451
 
            names = sep.join(names)
1452
 
        return names
 
1357
        name, address = config.parse_username(rev.get_apparent_authors()[0])
 
1358
        if name:
 
1359
            return name
 
1360
        return address
1453
1361
 
1454
1362
    def merge_marker(self, revision):
1455
1363
        """Get the merge marker to include in the output or '' if none."""
1459
1367
        else:
1460
1368
            return ''
1461
1369
 
1462
 
    def show_properties(self, revision, indent):
1463
 
        """Displays the custom properties returned by each registered handler.
1464
 
 
1465
 
        If a registered handler raises an error it is propagated.
1466
 
        """
1467
 
        for line in self.custom_properties(revision):
1468
 
            self.to_file.write("%s%s\n" % (indent, line))
1469
 
 
1470
 
    def custom_properties(self, revision):
1471
 
        """Format the custom properties returned by each registered handler.
1472
 
 
1473
 
        If a registered handler raises an error it is propagated.
1474
 
 
1475
 
        :return: a list of formatted lines (excluding trailing newlines)
1476
 
        """
1477
 
        lines = self._foreign_info_properties(revision)
1478
 
        for key, handler in properties_handler_registry.iteritems():
1479
 
            lines.extend(self._format_properties(handler(revision)))
1480
 
        return lines
1481
 
 
1482
 
    def _foreign_info_properties(self, rev):
 
1370
    def show_foreign_info(self, rev, indent):
1483
1371
        """Custom log displayer for foreign revision identifiers.
1484
1372
 
1485
1373
        :param rev: Revision object.
1486
1374
        """
1487
1375
        # Revision comes directly from a foreign repository
1488
1376
        if isinstance(rev, foreign.ForeignRevision):
1489
 
            return self._format_properties(
1490
 
                rev.mapping.vcs.show_foreign_revid(rev.foreign_revid))
 
1377
            self._write_properties(indent, rev.mapping.vcs.show_foreign_revid(
 
1378
                rev.foreign_revid))
 
1379
            return
1491
1380
 
1492
1381
        # Imported foreign revision revision ids always contain :
1493
1382
        if not ":" in rev.revision_id:
1494
 
            return []
 
1383
            return
1495
1384
 
1496
1385
        # Revision was once imported from a foreign repository
1497
1386
        try:
1498
1387
            foreign_revid, mapping = \
1499
1388
                foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
1500
1389
        except errors.InvalidRevisionId:
1501
 
            return []
 
1390
            return
1502
1391
 
1503
 
        return self._format_properties(
 
1392
        self._write_properties(indent, 
1504
1393
            mapping.vcs.show_foreign_revid(foreign_revid))
1505
1394
 
1506
 
    def _format_properties(self, properties):
1507
 
        lines = []
 
1395
    def show_properties(self, revision, indent):
 
1396
        """Displays the custom properties returned by each registered handler.
 
1397
 
 
1398
        If a registered handler raises an error it is propagated.
 
1399
        """
 
1400
        for key, handler in properties_handler_registry.iteritems():
 
1401
            self._write_properties(indent, handler(revision))
 
1402
 
 
1403
    def _write_properties(self, indent, properties):
1508
1404
        for key, value in properties.items():
1509
 
            lines.append(key + ': ' + value)
1510
 
        return lines
 
1405
            self.to_file.write(indent + key + ': ' + value + '\n')
1511
1406
 
1512
1407
    def show_diff(self, to_file, diff, indent):
1513
1408
        for l in diff.rstrip().split('\n'):
1514
1409
            to_file.write(indent + '%s\n' % (l,))
1515
1410
 
1516
1411
 
1517
 
# Separator between revisions in long format
1518
 
_LONG_SEP = '-' * 60
1519
 
 
1520
 
 
1521
1412
class LongLogFormatter(LogFormatter):
1522
1413
 
1523
1414
    supports_merge_revisions = True
1526
1417
    supports_tags = True
1527
1418
    supports_diff = True
1528
1419
 
1529
 
    def __init__(self, *args, **kwargs):
1530
 
        super(LongLogFormatter, self).__init__(*args, **kwargs)
1531
 
        if self.show_timezone == 'original':
1532
 
            self.date_string = self._date_string_original_timezone
1533
 
        else:
1534
 
            self.date_string = self._date_string_with_timezone
1535
 
 
1536
 
    def _date_string_with_timezone(self, rev):
1537
 
        return format_date(rev.timestamp, rev.timezone or 0,
1538
 
                           self.show_timezone)
1539
 
 
1540
 
    def _date_string_original_timezone(self, rev):
1541
 
        return format_date_with_offset_in_original_timezone(rev.timestamp,
1542
 
            rev.timezone or 0)
1543
 
 
1544
1420
    def log_revision(self, revision):
1545
1421
        """Log a revision, either merged or not."""
1546
1422
        indent = '    ' * revision.merge_depth
1547
 
        lines = [_LONG_SEP]
 
1423
        to_file = self.to_file
 
1424
        to_file.write(indent + '-' * 60 + '\n')
1548
1425
        if revision.revno is not None:
1549
 
            lines.append('revno: %s%s' % (revision.revno,
 
1426
            to_file.write(indent + 'revno: %s%s\n' % (revision.revno,
1550
1427
                self.merge_marker(revision)))
1551
1428
        if revision.tags:
1552
 
            lines.append('tags: %s' % (', '.join(revision.tags)))
 
1429
            to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
1553
1430
        if self.show_ids:
1554
 
            lines.append('revision-id: %s' % (revision.rev.revision_id,))
 
1431
            to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
 
1432
            to_file.write('\n')
1555
1433
            for parent_id in revision.rev.parent_ids:
1556
 
                lines.append('parent: %s' % (parent_id,))
1557
 
        lines.extend(self.custom_properties(revision.rev))
 
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)
1558
1437
 
1559
1438
        committer = revision.rev.committer
1560
 
        authors = self.authors(revision.rev, 'all')
 
1439
        authors = revision.rev.get_apparent_authors()
1561
1440
        if authors != [committer]:
1562
 
            lines.append('author: %s' % (", ".join(authors),))
1563
 
        lines.append('committer: %s' % (committer,))
 
1441
            to_file.write(indent + 'author: %s\n' % (", ".join(authors),))
 
1442
        to_file.write(indent + 'committer: %s\n' % (committer,))
1564
1443
 
1565
1444
        branch_nick = revision.rev.properties.get('branch-nick', None)
1566
1445
        if branch_nick is not None:
1567
 
            lines.append('branch nick: %s' % (branch_nick,))
1568
 
 
1569
 
        lines.append('timestamp: %s' % (self.date_string(revision.rev),))
1570
 
 
1571
 
        lines.append('message:')
 
1446
            to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
 
1447
 
 
1448
        date_str = format_date(revision.rev.timestamp,
 
1449
                               revision.rev.timezone or 0,
 
1450
                               self.show_timezone)
 
1451
        to_file.write(indent + 'timestamp: %s\n' % (date_str,))
 
1452
 
 
1453
        to_file.write(indent + 'message:\n')
1572
1454
        if not revision.rev.message:
1573
 
            lines.append('  (no message)')
 
1455
            to_file.write(indent + '  (no message)\n')
1574
1456
        else:
1575
1457
            message = revision.rev.message.rstrip('\r\n')
1576
1458
            for l in message.split('\n'):
1577
 
                lines.append('  %s' % (l,))
1578
 
 
1579
 
        # Dump the output, appending the delta and diff if requested
1580
 
        to_file = self.to_file
1581
 
        to_file.write("%s%s\n" % (indent, ('\n' + indent).join(lines)))
 
1459
                to_file.write(indent + '  %s\n' % (l,))
1582
1460
        if revision.delta is not None:
1583
 
            # Use the standard status output to display changes
1584
 
            from bzrlib.delta import report_delta
1585
 
            report_delta(to_file, revision.delta, short_status=False, 
1586
 
                         show_ids=self.show_ids, indent=indent)
 
1461
            # We don't respect delta_format for compatibility
 
1462
            revision.delta.show(to_file, self.show_ids, indent=indent,
 
1463
                                short_status=False)
1587
1464
        if revision.diff is not None:
1588
1465
            to_file.write(indent + 'diff:\n')
1589
 
            to_file.flush()
1590
1466
            # Note: we explicitly don't indent the diff (relative to the
1591
1467
            # revision information) so that the output can be fed to patch -p0
1592
1468
            self.show_diff(self.to_exact_file, revision.diff, indent)
1593
 
            self.to_exact_file.flush()
1594
1469
 
1595
1470
    def get_advice_separator(self):
1596
1471
        """Get the text separating the log from the closing advice."""
1640
1515
                            self.show_timezone, date_fmt="%Y-%m-%d",
1641
1516
                            show_offset=False),
1642
1517
                tags, self.merge_marker(revision)))
 
1518
        self.show_foreign_info(revision.rev, indent+offset)
1643
1519
        self.show_properties(revision.rev, indent+offset)
1644
1520
        if self.show_ids:
1645
1521
            to_file.write(indent + offset + 'revision-id:%s\n'
1652
1528
                to_file.write(indent + offset + '%s\n' % (l,))
1653
1529
 
1654
1530
        if revision.delta is not None:
1655
 
            # Use the standard status output to display changes
1656
 
            from bzrlib.delta import report_delta
1657
 
            report_delta(to_file, revision.delta, 
1658
 
                         short_status=self.delta_format==1, 
1659
 
                         show_ids=self.show_ids, indent=indent + offset)
 
1531
            revision.delta.show(to_file, self.show_ids, indent=indent + offset,
 
1532
                                short_status=self.delta_format==1)
1660
1533
        if revision.diff is not None:
1661
1534
            self.show_diff(self.to_exact_file, revision.diff, '      ')
1662
1535
        to_file.write('\n')
1670
1543
 
1671
1544
    def __init__(self, *args, **kwargs):
1672
1545
        super(LineLogFormatter, self).__init__(*args, **kwargs)
1673
 
        width = terminal_width()
1674
 
        if width is not None:
1675
 
            # we need one extra space for terminals that wrap on last char
1676
 
            width = width - 1
1677
 
        self._max_chars = width
 
1546
        self._max_chars = terminal_width() - 1
1678
1547
 
1679
1548
    def truncate(self, str, max_len):
1680
 
        if max_len is None or len(str) <= max_len:
 
1549
        if len(str) <= max_len:
1681
1550
            return str
1682
 
        return str[:max_len-3] + '...'
 
1551
        return str[:max_len-3]+'...'
1683
1552
 
1684
1553
    def date_string(self, rev):
1685
1554
        return format_date(rev.timestamp, rev.timezone or 0,
1737
1606
                               self.show_timezone,
1738
1607
                               date_fmt='%Y-%m-%d',
1739
1608
                               show_offset=False)
1740
 
        committer_str = self.authors(revision.rev, 'first', sep=', ')
1741
 
        committer_str = committer_str.replace(' <', '  <')
 
1609
        committer_str = revision.rev.committer.replace (' <', '  <')
1742
1610
        to_file.write('%s  %s\n\n' % (date_str,committer_str))
1743
1611
 
1744
1612
        if revision.delta is not None and revision.delta.has_changed():
1809
1677
        raise errors.BzrCommandError("unknown log formatter: %r" % name)
1810
1678
 
1811
1679
 
1812
 
def author_list_all(rev):
1813
 
    return rev.get_apparent_authors()[:]
1814
 
 
1815
 
 
1816
 
def author_list_first(rev):
1817
 
    lst = rev.get_apparent_authors()
1818
 
    try:
1819
 
        return [lst[0]]
1820
 
    except IndexError:
1821
 
        return []
1822
 
 
1823
 
 
1824
 
def author_list_committer(rev):
1825
 
    return [rev.committer]
1826
 
 
1827
 
 
1828
 
author_list_registry = registry.Registry()
1829
 
 
1830
 
author_list_registry.register('all', author_list_all,
1831
 
                              'All authors')
1832
 
 
1833
 
author_list_registry.register('first', author_list_first,
1834
 
                              'The first author')
1835
 
 
1836
 
author_list_registry.register('committer', author_list_committer,
1837
 
                              'The committer')
1838
 
 
1839
 
 
1840
1680
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
1841
1681
    # deprecated; for compatibility
1842
1682
    lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
1993
1833
        lf.log_revision(lr)
1994
1834
 
1995
1835
 
1996
 
def _get_info_for_log_files(revisionspec_list, file_list, add_cleanup):
 
1836
def _get_info_for_log_files(revisionspec_list, file_list):
1997
1837
    """Find file-ids and kinds given a list of files and a revision range.
1998
1838
 
1999
1839
    We search for files at the end of the range. If not found there,
2003
1843
    :param file_list: the list of paths given on the command line;
2004
1844
      the first of these can be a branch location or a file path,
2005
1845
      the remainder must be file paths
2006
 
    :param add_cleanup: When the branch returned is read locked,
2007
 
      an unlock call will be queued to the cleanup.
2008
1846
    :return: (branch, info_list, start_rev_info, end_rev_info) where
2009
1847
      info_list is a list of (relative_path, file_id, kind) tuples where
2010
1848
      kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
2011
 
      branch will be read-locked.
2012
1849
    """
2013
1850
    from builtins import _get_revision_range, safe_relpath_files
2014
1851
    tree, b, path = bzrdir.BzrDir.open_containing_tree_or_branch(file_list[0])
2015
 
    add_cleanup(b.lock_read().unlock)
2016
1852
    # XXX: It's damn messy converting a list of paths to relative paths when
2017
1853
    # those paths might be deleted ones, they might be on a case-insensitive
2018
1854
    # filesystem and/or they might be in silly locations (like another branch).
2097
1933
 
2098
1934
properties_handler_registry = registry.Registry()
2099
1935
 
2100
 
# Use the properties handlers to print out bug information if available
2101
 
def _bugs_properties_handler(revision):
2102
 
    if revision.properties.has_key('bugs'):
2103
 
        bug_lines = revision.properties['bugs'].split('\n')
2104
 
        bug_rows = [line.split(' ', 1) for line in bug_lines]
2105
 
        fixed_bug_urls = [row[0] for row in bug_rows if
2106
 
                          len(row) > 1 and row[1] == 'fixed']
2107
 
 
2108
 
        if fixed_bug_urls:
2109
 
            return {'fixes bug(s)': ' '.join(fixed_bug_urls)}
2110
 
    return {}
2111
 
 
2112
 
properties_handler_registry.register('bugs_properties_handler',
2113
 
                                     _bugs_properties_handler)
2114
 
 
2115
1936
 
2116
1937
# adapters which revision ids to log are filtered. When log is called, the
2117
1938
# log_rev_iterator is adapted through each of these factory methods.