~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/log.py

  • Committer: Jelmer Vernooij
  • Date: 2009-03-28 14:24:46 UTC
  • mfrom: (4211 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4212.
  • Revision ID: jelmer@samba.org-20090328142446-vqi0ksswdurga631
Merge bzr.dev.

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
69
69
    config,
70
70
    diff,
71
71
    errors,
72
 
    foreign,
73
72
    repository as _mod_repository,
74
73
    revision as _mod_revision,
75
74
    revisionspec,
83
82
    )
84
83
from bzrlib.osutils import (
85
84
    format_date,
86
 
    format_date_with_offset_in_original_timezone,
87
85
    get_terminal_encoding,
88
86
    re_compile_checked,
89
87
    terminal_width,
90
88
    )
91
 
from bzrlib.symbol_versioning import (
92
 
    deprecated_function,
93
 
    deprecated_in,
94
 
    )
95
89
 
96
90
 
97
91
def find_touching_revisions(branch, file_id):
109
103
    last_path = None
110
104
    revno = 1
111
105
    for revision_id in branch.revision_history():
112
 
        this_inv = branch.repository.get_inventory(revision_id)
 
106
        this_inv = branch.repository.get_revision_inventory(revision_id)
113
107
        if file_id in this_inv:
114
108
            this_ie = this_inv[file_id]
115
109
            this_path = this_inv.id2path(file_id)
220
214
    'direction': 'reverse',
221
215
    'levels': 1,
222
216
    'generate_tags': True,
223
 
    'exclude_common_ancestry': False,
224
217
    '_match_using_deltas': True,
225
218
    }
226
219
 
227
220
 
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,
231
 
                          delta_type=None,
232
 
                          diff_type=None, _match_using_deltas=True,
233
 
                          exclude_common_ancestry=False,
234
 
                          ):
 
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.
236
226
 
237
227
    Using this function may make code slightly safer by ensuring
275
265
      algorithm used for matching specific_fileids. This parameter
276
266
      may be removed in the future so bzrlib client code should NOT
277
267
      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
268
    """
282
269
    return {
283
270
        'direction': direction,
290
277
        'generate_tags': generate_tags,
291
278
        'delta_type': delta_type,
292
279
        'diff_type': diff_type,
293
 
        'exclude_common_ancestry': exclude_common_ancestry,
294
280
        # Add 'private' attributes for features that may be deprecated
295
281
        '_match_using_deltas': _match_using_deltas,
 
282
        '_allow_single_merge_revision': True,
296
283
    }
297
284
 
298
285
 
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.
361
348
            rqst['delta_type'] = None
362
349
        if not getattr(lf, 'supports_diff', False):
363
350
            rqst['diff_type'] = None
 
351
        if not getattr(lf, 'supports_merge_revisions', False):
 
352
            rqst['_allow_single_merge_revision'] = getattr(lf,
 
353
                'supports_single_merge_revision', False)
364
354
 
365
355
        # Find and print the interesting revisions
366
356
        generator = self._generator_factory(self.branch, rqst)
367
357
        for lr in generator.iter_log_revisions():
368
358
            lf.log_revision(lr)
369
 
        lf.show_advice()
370
359
 
371
360
    def _generator_factory(self, branch, rqst):
372
361
        """Make the LogGenerator object to use.
397
386
        :return: An iterator yielding LogRevision objects.
398
387
        """
399
388
        rqst = self.rqst
400
 
        levels = rqst.get('levels')
401
 
        limit = rqst.get('limit')
402
 
        diff_type = rqst.get('diff_type')
403
389
        log_count = 0
404
390
        revision_iterator = self._create_log_revision_iterator()
405
391
        for revs in revision_iterator:
406
392
            for (rev_id, revno, merge_depth), rev, delta in revs:
407
393
                # 0 levels means show everything; merge_depth counts from 0
 
394
                levels = rqst.get('levels')
408
395
                if levels != 0 and merge_depth >= levels:
409
396
                    continue
410
 
                if diff_type is None:
411
 
                    diff = None
412
 
                else:
413
 
                    diff = self._format_diff(rev, rev_id, diff_type)
 
397
                diff = self._format_diff(rev, rev_id)
414
398
                yield LogRevision(rev, revno, merge_depth, delta,
415
399
                    self.rev_tag_dict.get(rev_id), diff)
 
400
                limit = rqst.get('limit')
416
401
                if limit:
417
402
                    log_count += 1
418
403
                    if log_count >= limit:
419
404
                        return
420
405
 
421
 
    def _format_diff(self, rev, rev_id, diff_type):
 
406
    def _format_diff(self, rev, rev_id):
 
407
        diff_type = self.rqst.get('diff_type')
 
408
        if diff_type is None:
 
409
            return None
422
410
        repo = self.branch.repository
423
411
        if len(rev.parent_ids) == 0:
424
412
            ancestor_id = _mod_revision.NULL_REVISION
463
451
        generate_merge_revisions = rqst.get('levels') != 1
464
452
        delayed_graph_generation = not rqst.get('specific_fileids') and (
465
453
                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'))
 
454
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
 
455
            self.end_rev_id, rqst.get('direction'), generate_merge_revisions,
 
456
            rqst.get('_allow_single_merge_revision'),
 
457
            delayed_graph_generation=delayed_graph_generation)
472
458
 
473
459
        # Apply the other filters
474
460
        return make_log_rev_iterator(self.branch, view_revisions,
481
467
        # Note that we always generate the merge revisions because
482
468
        # filter_revisions_touching_file_id() requires them ...
483
469
        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'))
 
470
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
 
471
            self.end_rev_id, rqst.get('direction'), True,
 
472
            rqst.get('_allow_single_merge_revision'))
488
473
        if not isinstance(view_revisions, list):
489
474
            view_revisions = list(view_revisions)
490
475
        view_revisions = _filter_revisions_touching_file_id(self.branch,
495
480
 
496
481
 
497
482
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
 
                         ):
 
483
    generate_merge_revisions, allow_single_merge_revision,
 
484
    delayed_graph_generation=False):
502
485
    """Calculate the revisions to view.
503
486
 
504
487
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
505
488
             a list of the same tuples.
506
489
    """
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
490
    br_revno, br_rev_id = branch.last_revision_info()
513
491
    if br_revno == 0:
514
492
        return []
515
493
 
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)
 
494
    # If a single revision is requested, check we can handle it
 
495
    generate_single_revision = (end_rev_id and start_rev_id == end_rev_id and
 
496
        (not generate_merge_revisions or not _has_merges(branch, end_rev_id)))
 
497
    if generate_single_revision:
 
498
        return _generate_one_revision(branch, end_rev_id, br_rev_id, br_revno,
 
499
            allow_single_merge_revision)
 
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)
528
505
    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
535
 
 
536
 
 
537
 
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno):
 
506
        return _generate_all_revisions(branch, start_rev_id, end_rev_id,
 
507
            direction, delayed_graph_generation)
 
508
 
 
509
 
 
510
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno,
 
511
    allow_single_merge_revision):
538
512
    if rev_id == br_rev_id:
539
513
        # It's the tip
540
514
        return [(br_rev_id, br_revno, 0)]
541
515
    else:
542
516
        revno = branch.revision_id_to_dotted_revno(rev_id)
 
517
        if len(revno) > 1 and not allow_single_merge_revision:
 
518
            # It's a merge revision and the log formatter is
 
519
            # completely brain dead. This "feature" of allowing
 
520
            # log formatters incapable of displaying dotted revnos
 
521
            # ought to be deprecated IMNSHO. IGC 20091022
 
522
            raise errors.BzrCommandError('Selected log formatter only'
 
523
                ' supports mainline revisions.')
543
524
        revno_str = '.'.join(str(n) for n in revno)
544
525
        return [(rev_id, revno_str, 0)]
545
526
 
555
536
        except _StartNotLinearAncestor:
556
537
            raise errors.BzrCommandError('Start revision not found in'
557
538
                ' left-hand history of end revision.')
 
539
    if direction == 'forward':
 
540
        result = reversed(result)
558
541
    return result
559
542
 
560
543
 
561
544
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
562
 
                            delayed_graph_generation,
563
 
                            exclude_common_ancestry=False):
 
545
    delayed_graph_generation):
564
546
    # On large trees, generating the merge graph can take 30-60 seconds
565
547
    # so we delay doing it until a merge is detected, incrementally
566
548
    # 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
549
    initial_revisions = []
572
550
    if delayed_graph_generation:
573
551
        try:
574
 
            for rev_id, revno, depth in  _linear_view_revisions(
575
 
                branch, start_rev_id, end_rev_id):
 
552
            for rev_id, revno, depth in \
 
553
                _linear_view_revisions(branch, start_rev_id, end_rev_id):
576
554
                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
555
                    end_rev_id = rev_id
593
556
                    break
594
557
                else:
595
558
                    initial_revisions.append((rev_id, revno, depth))
596
559
            else:
597
560
                # No merged revisions found
598
 
                return initial_revisions
 
561
                if direction == 'reverse':
 
562
                    return initial_revisions
 
563
                elif direction == 'forward':
 
564
                    return reversed(initial_revisions)
 
565
                else:
 
566
                    raise ValueError('invalid direction %r' % direction)
599
567
        except _StartNotLinearAncestor:
600
568
            # A merge was never detected so the lower revision limit can't
601
569
            # be nested down somewhere
602
570
            raise errors.BzrCommandError('Start revision not found in'
603
571
                ' history of end revision.')
604
572
 
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
573
    # A log including nested merges is required. If the direction is reverse,
609
574
    # we rebase the initial merge depths so that the development line is
610
575
    # shown naturally, i.e. just like it is for linear logging. We can easily
612
577
    # indented at the end seems slightly nicer in that case.
613
578
    view_revisions = chain(iter(initial_revisions),
614
579
        _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
 
580
        rebase_initial_depths=direction == 'reverse'))
 
581
    if direction == 'reverse':
 
582
        return view_revisions
 
583
    elif direction == 'forward':
 
584
        # Forward means oldest first, adjusting for depth.
 
585
        view_revisions = reverse_by_depth(list(view_revisions))
 
586
        return _rebase_merge_depth(view_revisions)
 
587
    else:
 
588
        raise ValueError('invalid direction %r' % direction)
618
589
 
619
590
 
620
591
def _has_merges(branch, rev_id):
638
609
        else:
639
610
            # not obvious
640
611
            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
612
    return True
644
613
 
645
614
 
678
647
 
679
648
 
680
649
def _graph_view_revisions(branch, start_rev_id, end_rev_id,
681
 
                          rebase_initial_depths=True,
682
 
                          exclude_common_ancestry=False):
 
650
    rebase_initial_depths=True):
683
651
    """Calculate revisions to view including merges, newest to oldest.
684
652
 
685
653
    :param branch: the branch
689
657
      revision is found?
690
658
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
691
659
    """
692
 
    if exclude_common_ancestry:
693
 
        stop_rule = 'with-merges-without-common-ancestry'
694
 
    else:
695
 
        stop_rule = 'with-merges'
696
660
    view_revisions = branch.iter_merge_sorted_revisions(
697
661
        start_revision_id=end_rev_id, stop_revision_id=start_rev_id,
698
 
        stop_rule=stop_rule)
 
662
        stop_rule="with-merges")
699
663
    if not rebase_initial_depths:
700
664
        for (rev_id, merge_depth, revno, end_of_merge
701
665
             ) in view_revisions:
712
676
                depth_adjustment = merge_depth
713
677
            if depth_adjustment:
714
678
                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
679
                    depth_adjustment = merge_depth
720
680
                merge_depth -= depth_adjustment
721
681
            yield rev_id, '.'.join(map(str, revno)), merge_depth
722
682
 
723
683
 
724
 
@deprecated_function(deprecated_in((2, 2, 0)))
725
684
def calculate_view_revisions(branch, start_revision, end_revision, direction,
726
 
        specific_fileid, generate_merge_revisions):
 
685
        specific_fileid, generate_merge_revisions, allow_single_merge_revision):
727
686
    """Calculate the revisions to view.
728
687
 
729
688
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
730
689
             a list of the same tuples.
731
690
    """
 
691
    # This method is no longer called by the main code path.
 
692
    # It is retained for API compatibility and may be deprecated
 
693
    # soon. IGC 20090116
732
694
    start_rev_id, end_rev_id = _get_revision_limits(branch, start_revision,
733
695
        end_revision)
734
696
    view_revisions = list(_calc_view_revisions(branch, start_rev_id, end_rev_id,
735
 
        direction, generate_merge_revisions or specific_fileid))
 
697
        direction, generate_merge_revisions or specific_fileid,
 
698
        allow_single_merge_revision))
736
699
    if specific_fileid:
737
700
        view_revisions = _filter_revisions_touching_file_id(branch,
738
701
            specific_fileid, view_revisions,
1084
1047
    return mainline_revs, rev_nos, start_rev_id, end_rev_id
1085
1048
 
1086
1049
 
1087
 
@deprecated_function(deprecated_in((2, 2, 0)))
1088
1050
def _filter_revision_range(view_revisions, start_rev_id, end_rev_id):
1089
1051
    """Filter view_revisions based on revision ranges.
1090
1052
 
1099
1061
 
1100
1062
    :return: The filtered view_revisions.
1101
1063
    """
 
1064
    # This method is no longer called by the main code path.
 
1065
    # It may be removed soon. IGC 20090127
1102
1066
    if start_rev_id or end_rev_id:
1103
1067
        revision_ids = [r for r, n, d in view_revisions]
1104
1068
        if start_rev_id:
1210
1174
    return result
1211
1175
 
1212
1176
 
1213
 
@deprecated_function(deprecated_in((2, 2, 0)))
1214
1177
def get_view_revisions(mainline_revs, rev_nos, branch, direction,
1215
1178
                       include_merges=True):
1216
1179
    """Produce an iterator of revisions to show
1217
1180
    :return: an iterator of (revision_id, revno, merge_depth)
1218
1181
    (if there is no revno for a revision, None is supplied)
1219
1182
    """
 
1183
    # This method is no longer called by the main code path.
 
1184
    # It is retained for API compatibility and may be deprecated
 
1185
    # soon. IGC 20090127
1220
1186
    if not include_merges:
1221
1187
        revision_ids = mainline_revs[1:]
1222
1188
        if direction == 'reverse':
1317
1283
        one (2) should be used.
1318
1284
 
1319
1285
    - supports_merge_revisions must be True if this log formatter supports
1320
 
        merge revisions.  If not, then only mainline revisions will be passed
1321
 
        to the formatter.
 
1286
        merge revisions.  If not, and if supports_single_merge_revision is
 
1287
        also not True, then only mainline revisions will be passed to the
 
1288
        formatter.
1322
1289
 
1323
1290
    - preferred_levels is the number of levels this formatter defaults to.
1324
1291
        The default value is zero meaning display all levels.
1325
1292
        This value is only relevant if supports_merge_revisions is True.
1326
1293
 
 
1294
    - supports_single_merge_revision must be True if this log formatter
 
1295
        supports logging only a single merge revision.  This flag is
 
1296
        only relevant if supports_merge_revisions is not True.
 
1297
 
1327
1298
    - supports_tags must be True if this log formatter supports tags.
1328
1299
        Otherwise the tags attribute may not be populated.
1329
1300
 
1340
1311
    preferred_levels = 0
1341
1312
 
1342
1313
    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):
 
1314
                 delta_format=None, levels=None):
1345
1315
        """Create a LogFormatter.
1346
1316
 
1347
1317
        :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
1318
        :param show_ids: if True, revision-ids are to be displayed
1351
1319
        :param show_timezone: the timezone to use
1352
1320
        :param delta_format: the level of delta information to display
1353
 
          or None to leave it to the formatter to decide
 
1321
          or None to leave it u to the formatter to decide
1354
1322
        :param levels: the number of levels to display; None or -1 to
1355
1323
          let the log formatter decide.
1356
 
        :param show_advice: whether to show advice at the end of the
1357
 
          log or not
1358
1324
        """
1359
1325
        self.to_file = to_file
1360
1326
        # 'exact' stream used to show diff, it should print content 'as is'
1361
1327
        # and should not try to decode/encode it to unicode to avoid bug #328007
1362
 
        if to_exact_file is not None:
1363
 
            self.to_exact_file = to_exact_file
1364
 
        else:
1365
 
            # XXX: somewhat hacky; this assumes it's a codec writer; it's better
1366
 
            # for code that expects to get diffs to pass in the exact file
1367
 
            # stream
1368
 
            self.to_exact_file = getattr(to_file, 'stream', to_file)
 
1328
        self.to_exact_file = getattr(to_file, 'stream', to_file)
1369
1329
        self.show_ids = show_ids
1370
1330
        self.show_timezone = show_timezone
1371
1331
        if delta_format is None:
1373
1333
            delta_format = 2 # long format
1374
1334
        self.delta_format = delta_format
1375
1335
        self.levels = levels
1376
 
        self._show_advice = show_advice
1377
 
        self._merge_count = 0
1378
1336
 
1379
1337
    def get_levels(self):
1380
1338
        """Get the number of levels to display or 0 for all."""
1381
1339
        if getattr(self, 'supports_merge_revisions', False):
1382
1340
            if self.levels is None or self.levels == -1:
1383
 
                self.levels = self.preferred_levels
1384
 
        else:
1385
 
            self.levels = 1
1386
 
        return self.levels
 
1341
                return self.preferred_levels
 
1342
            else:
 
1343
                return self.levels
 
1344
        return 1
1387
1345
 
1388
1346
    def log_revision(self, revision):
1389
1347
        """Log a revision.
1392
1350
        """
1393
1351
        raise NotImplementedError('not implemented in abstract base')
1394
1352
 
1395
 
    def show_advice(self):
1396
 
        """Output user advice, if any, when the log is completed."""
1397
 
        if self._show_advice and self.levels == 1 and self._merge_count > 0:
1398
 
            advice_sep = self.get_advice_separator()
1399
 
            if advice_sep:
1400
 
                self.to_file.write(advice_sep)
1401
 
            self.to_file.write(
1402
 
                "Use --include-merges or -n0 to see merged revisions.\n")
1403
 
 
1404
 
    def get_advice_separator(self):
1405
 
        """Get the text separating the log from the closing advice."""
1406
 
        return ''
1407
 
 
1408
1353
    def short_committer(self, rev):
1409
1354
        name, address = config.parse_username(rev.committer)
1410
1355
        if name:
1417
1362
            return name
1418
1363
        return address
1419
1364
 
1420
 
    def merge_marker(self, revision):
1421
 
        """Get the merge marker to include in the output or '' if none."""
1422
 
        if len(revision.rev.parent_ids) > 1:
1423
 
            self._merge_count += 1
1424
 
            return ' [merge]'
1425
 
        else:
1426
 
            return ''
1427
 
 
1428
1365
    def show_properties(self, revision, indent):
1429
1366
        """Displays the custom properties returned by each registered handler.
1430
1367
 
1431
1368
        If a registered handler raises an error it is propagated.
1432
1369
        """
1433
 
        for line in self.custom_properties(revision):
1434
 
            self.to_file.write("%s%s\n" % (indent, line))
1435
 
 
1436
 
    def custom_properties(self, revision):
1437
 
        """Format the custom properties returned by each registered handler.
1438
 
 
1439
 
        If a registered handler raises an error it is propagated.
1440
 
 
1441
 
        :return: a list of formatted lines (excluding trailing newlines)
1442
 
        """
1443
 
        lines = self._foreign_info_properties(revision)
1444
1370
        for key, handler in properties_handler_registry.iteritems():
1445
 
            lines.extend(self._format_properties(handler(revision)))
1446
 
        return lines
1447
 
 
1448
 
    def _foreign_info_properties(self, rev):
1449
 
        """Custom log displayer for foreign revision identifiers.
1450
 
 
1451
 
        :param rev: Revision object.
1452
 
        """
1453
 
        # Revision comes directly from a foreign repository
1454
 
        if isinstance(rev, foreign.ForeignRevision):
1455
 
            return self._format_properties(
1456
 
                rev.mapping.vcs.show_foreign_revid(rev.foreign_revid))
1457
 
 
1458
 
        # Imported foreign revision revision ids always contain :
1459
 
        if not ":" in rev.revision_id:
1460
 
            return []
1461
 
 
1462
 
        # Revision was once imported from a foreign repository
1463
 
        try:
1464
 
            foreign_revid, mapping = \
1465
 
                foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
1466
 
        except errors.InvalidRevisionId:
1467
 
            return []
1468
 
 
1469
 
        return self._format_properties(
1470
 
            mapping.vcs.show_foreign_revid(foreign_revid))
1471
 
 
1472
 
    def _format_properties(self, properties):
1473
 
        lines = []
1474
 
        for key, value in properties.items():
1475
 
            lines.append(key + ': ' + value)
1476
 
        return lines
 
1371
            for key, value in handler(revision).items():
 
1372
                self.to_file.write(indent + key + ': ' + value + '\n')
1477
1373
 
1478
1374
    def show_diff(self, to_file, diff, indent):
1479
1375
        for l in diff.rstrip().split('\n'):
1480
1376
            to_file.write(indent + '%s\n' % (l,))
1481
1377
 
1482
1378
 
1483
 
# Separator between revisions in long format
1484
 
_LONG_SEP = '-' * 60
1485
 
 
1486
 
 
1487
1379
class LongLogFormatter(LogFormatter):
1488
1380
 
1489
1381
    supports_merge_revisions = True
1490
 
    preferred_levels = 1
1491
1382
    supports_delta = True
1492
1383
    supports_tags = True
1493
1384
    supports_diff = True
1494
1385
 
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
1499
 
        else:
1500
 
            self.date_string = self._date_string_with_timezone
1501
 
 
1502
 
    def _date_string_with_timezone(self, rev):
1503
 
        return format_date(rev.timestamp, rev.timezone or 0,
1504
 
                           self.show_timezone)
1505
 
 
1506
 
    def _date_string_original_timezone(self, rev):
1507
 
        return format_date_with_offset_in_original_timezone(rev.timestamp,
1508
 
            rev.timezone or 0)
1509
 
 
1510
1386
    def log_revision(self, revision):
1511
1387
        """Log a revision, either merged or not."""
1512
1388
        indent = '    ' * revision.merge_depth
1513
 
        lines = [_LONG_SEP]
 
1389
        to_file = self.to_file
 
1390
        to_file.write(indent + '-' * 60 + '\n')
1514
1391
        if revision.revno is not None:
1515
 
            lines.append('revno: %s%s' % (revision.revno,
1516
 
                self.merge_marker(revision)))
 
1392
            to_file.write(indent + 'revno: %s\n' % (revision.revno,))
1517
1393
        if revision.tags:
1518
 
            lines.append('tags: %s' % (', '.join(revision.tags)))
 
1394
            to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
1519
1395
        if self.show_ids:
1520
 
            lines.append('revision-id: %s' % (revision.rev.revision_id,))
 
1396
            to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
 
1397
            to_file.write('\n')
1521
1398
            for parent_id in revision.rev.parent_ids:
1522
 
                lines.append('parent: %s' % (parent_id,))
1523
 
        lines.extend(self.custom_properties(revision.rev))
 
1399
                to_file.write(indent + 'parent: %s\n' % (parent_id,))
 
1400
        self.show_properties(revision.rev, indent)
1524
1401
 
1525
1402
        committer = revision.rev.committer
1526
1403
        authors = revision.rev.get_apparent_authors()
1527
1404
        if authors != [committer]:
1528
 
            lines.append('author: %s' % (", ".join(authors),))
1529
 
        lines.append('committer: %s' % (committer,))
 
1405
            to_file.write(indent + 'author: %s\n' % (", ".join(authors),))
 
1406
        to_file.write(indent + 'committer: %s\n' % (committer,))
1530
1407
 
1531
1408
        branch_nick = revision.rev.properties.get('branch-nick', None)
1532
1409
        if branch_nick is not None:
1533
 
            lines.append('branch nick: %s' % (branch_nick,))
1534
 
 
1535
 
        lines.append('timestamp: %s' % (self.date_string(revision.rev),))
1536
 
 
1537
 
        lines.append('message:')
 
1410
            to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
 
1411
 
 
1412
        date_str = format_date(revision.rev.timestamp,
 
1413
                               revision.rev.timezone or 0,
 
1414
                               self.show_timezone)
 
1415
        to_file.write(indent + 'timestamp: %s\n' % (date_str,))
 
1416
 
 
1417
        to_file.write(indent + 'message:\n')
1538
1418
        if not revision.rev.message:
1539
 
            lines.append('  (no message)')
 
1419
            to_file.write(indent + '  (no message)\n')
1540
1420
        else:
1541
1421
            message = revision.rev.message.rstrip('\r\n')
1542
1422
            for l in message.split('\n'):
1543
 
                lines.append('  %s' % (l,))
1544
 
 
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)))
 
1423
                to_file.write(indent + '  %s\n' % (l,))
1548
1424
        if revision.delta is not None:
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)
 
1425
            # We don't respect delta_format for compatibility
 
1426
            revision.delta.show(to_file, self.show_ids, indent=indent,
 
1427
                                short_status=False)
1553
1428
        if revision.diff is not None:
1554
1429
            to_file.write(indent + 'diff:\n')
1555
 
            to_file.flush()
1556
1430
            # Note: we explicitly don't indent the diff (relative to the
1557
1431
            # revision information) so that the output can be fed to patch -p0
1558
1432
            self.show_diff(self.to_exact_file, revision.diff, indent)
1559
 
            self.to_exact_file.flush()
1560
 
 
1561
 
    def get_advice_separator(self):
1562
 
        """Get the text separating the log from the closing advice."""
1563
 
        return '-' * 60 + '\n'
1564
1433
 
1565
1434
 
1566
1435
class ShortLogFormatter(LogFormatter):
1596
1465
        offset = ' ' * (revno_width + 1)
1597
1466
 
1598
1467
        to_file = self.to_file
 
1468
        is_merge = ''
 
1469
        if len(revision.rev.parent_ids) > 1:
 
1470
            is_merge = ' [merge]'
1599
1471
        tags = ''
1600
1472
        if revision.tags:
1601
1473
            tags = ' {%s}' % (', '.join(revision.tags))
1605
1477
                            revision.rev.timezone or 0,
1606
1478
                            self.show_timezone, date_fmt="%Y-%m-%d",
1607
1479
                            show_offset=False),
1608
 
                tags, self.merge_marker(revision)))
 
1480
                tags, is_merge))
1609
1481
        self.show_properties(revision.rev, indent+offset)
1610
1482
        if self.show_ids:
1611
1483
            to_file.write(indent + offset + 'revision-id:%s\n'
1618
1490
                to_file.write(indent + offset + '%s\n' % (l,))
1619
1491
 
1620
1492
        if revision.delta is not None:
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)
 
1493
            revision.delta.show(to_file, self.show_ids, indent=indent + offset,
 
1494
                                short_status=self.delta_format==1)
1626
1495
        if revision.diff is not None:
1627
1496
            self.show_diff(self.to_exact_file, revision.diff, '      ')
1628
1497
        to_file.write('\n')
1636
1505
 
1637
1506
    def __init__(self, *args, **kwargs):
1638
1507
        super(LineLogFormatter, self).__init__(*args, **kwargs)
1639
 
        width = terminal_width()
1640
 
        if width is not None:
1641
 
            # we need one extra space for terminals that wrap on last char
1642
 
            width = width - 1
1643
 
        self._max_chars = width
 
1508
        self._max_chars = terminal_width() - 1
1644
1509
 
1645
1510
    def truncate(self, str, max_len):
1646
 
        if max_len is None or len(str) <= max_len:
 
1511
        if len(str) <= max_len:
1647
1512
            return str
1648
 
        return str[:max_len-3] + '...'
 
1513
        return str[:max_len-3]+'...'
1649
1514
 
1650
1515
    def date_string(self, rev):
1651
1516
        return format_date(rev.timestamp, rev.timezone or 0,
1703
1568
                               self.show_timezone,
1704
1569
                               date_fmt='%Y-%m-%d',
1705
1570
                               show_offset=False)
1706
 
        committer_str = revision.rev.get_apparent_authors()[0].replace (' <', '  <')
 
1571
        committer_str = revision.rev.committer.replace (' <', '  <')
1707
1572
        to_file.write('%s  %s\n\n' % (date_str,committer_str))
1708
1573
 
1709
1574
        if revision.delta is not None and revision.delta.has_changed():
1930
1795
        lf.log_revision(lr)
1931
1796
 
1932
1797
 
1933
 
def _get_info_for_log_files(revisionspec_list, file_list, add_cleanup):
 
1798
def _get_info_for_log_files(revisionspec_list, file_list):
1934
1799
    """Find file-ids and kinds given a list of files and a revision range.
1935
1800
 
1936
1801
    We search for files at the end of the range. If not found there,
1940
1805
    :param file_list: the list of paths given on the command line;
1941
1806
      the first of these can be a branch location or a file path,
1942
1807
      the remainder must be file paths
1943
 
    :param add_cleanup: When the branch returned is read locked,
1944
 
      an unlock call will be queued to the cleanup.
1945
1808
    :return: (branch, info_list, start_rev_info, end_rev_info) where
1946
1809
      info_list is a list of (relative_path, file_id, kind) tuples where
1947
1810
      kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
1948
 
      branch will be read-locked.
1949
1811
    """
1950
1812
    from builtins import _get_revision_range, safe_relpath_files
1951
1813
    tree, b, path = bzrdir.BzrDir.open_containing_tree_or_branch(file_list[0])
1952
 
    add_cleanup(b.lock_read().unlock)
1953
1814
    # XXX: It's damn messy converting a list of paths to relative paths when
1954
1815
    # those paths might be deleted ones, they might be on a case-insensitive
1955
1816
    # filesystem and/or they might be in silly locations (like another branch).
1965
1826
    info_list = []
1966
1827
    start_rev_info, end_rev_info = _get_revision_range(revisionspec_list, b,
1967
1828
        "log")
1968
 
    if relpaths in ([], [u'']):
1969
 
        return b, [], start_rev_info, end_rev_info
1970
1829
    if start_rev_info is None and end_rev_info is None:
1971
1830
        if tree is None:
1972
1831
            tree = b.basis_tree()
2033
1892
 
2034
1893
 
2035
1894
properties_handler_registry = registry.Registry()
2036
 
 
2037
 
# Use the properties handlers to print out bug information if available
2038
 
def _bugs_properties_handler(revision):
2039
 
    if revision.properties.has_key('bugs'):
2040
 
        bug_lines = revision.properties['bugs'].split('\n')
2041
 
        bug_rows = [line.split(' ', 1) for line in bug_lines]
2042
 
        fixed_bug_urls = [row[0] for row in bug_rows if
2043
 
                          len(row) > 1 and row[1] == 'fixed']
2044
 
 
2045
 
        if fixed_bug_urls:
2046
 
            return {'fixes bug(s)': ' '.join(fixed_bug_urls)}
2047
 
    return {}
2048
 
 
2049
 
properties_handler_registry.register('bugs_properties_handler',
2050
 
                                     _bugs_properties_handler)
 
1895
properties_handler_registry.register_lazy("foreign",
 
1896
                                          "bzrlib.foreign",
 
1897
                                          "show_foreign_properties")
2051
1898
 
2052
1899
 
2053
1900
# adapters which revision ids to log are filtered. When log is called, the