/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4070.4.8 by Martin Pool
Rename format to gnu-changelog
1
# Copyright (C) 2005, 2006, 2007, 2009 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
369 by Martin Pool
- Split out log printing into new show_log function
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
369 by Martin Pool
- Split out log printing into new show_log function
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
369 by Martin Pool
- Split out log printing into new show_log function
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
369 by Martin Pool
- Split out log printing into new show_log function
16
375 by Martin Pool
- New command touching-revisions and function to trace
17
18
527 by Martin Pool
- refactor log command
19
"""Code to show logs of changes.
20
21
Various flavors of log can be produced:
22
23
* for one file, or the whole tree, and (not done yet) for
24
  files in a given directory
25
26
* in "verbose" mode with a description of what changed from one
27
  version to the next
28
29
* with file-ids and revision-ids shown
30
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
31
Logs are actually written out through an abstract LogFormatter
32
interface, which allows for different preferred formats.  Plugins can
33
register formats too.
34
35
Logs can be produced in either forward (oldest->newest) or reverse
36
(newest->oldest) order.
37
38
Logs can be filtered to show only revisions matching a particular
39
search string, or within a particular range of revisions.  The range
40
can be given as date/times, which are reduced to revisions before
41
calling in here.
42
43
In verbose mode we show a summary of what changed in each particular
44
revision.  Note that this is the delta for changes in that revision
2466.12.2 by Kent Gibson
shift log output with only merge revisions to the left margin
45
relative to its left-most parent, not the delta relative to the last
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
46
logged revision.  So for example if you ask for a verbose log of
47
changes touching hello.c you will get a list of those revisions also
48
listing other things that were changed in the same revision, but not
49
all the changes since the previous revision that touched hello.c.
527 by Martin Pool
- refactor log command
50
"""
51
2997.1.2 by Kent Gibson
Move all imports to top of log.py
52
import codecs
3943.5.1 by Ian Clatworthy
first cut at log --show-diff
53
from cStringIO import StringIO
2997.1.2 by Kent Gibson
Move all imports to top of log.py
54
from itertools import (
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
55
    chain,
2997.1.2 by Kent Gibson
Move all imports to top of log.py
56
    izip,
57
    )
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
58
import re
2997.1.2 by Kent Gibson
Move all imports to top of log.py
59
import sys
60
from warnings import (
61
    warn,
62
    )
1185.33.41 by Martin Pool
Fix regression of 'bzr log -v' - it wasn't showing changed files at all. (#4676)
63
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
64
from bzrlib.lazy_import import lazy_import
65
lazy_import(globals(), """
3535.5.1 by John Arbash Meinel
cleanup a few imports to be lazily loaded.
66
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
67
from bzrlib import (
4202.2.1 by Ian Clatworthy
get directory logging working again
68
    bzrdir,
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
69
    config,
3943.5.1 by Ian Clatworthy
first cut at log --show-diff
70
    diff,
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
71
    errors,
3535.5.1 by John Arbash Meinel
cleanup a few imports to be lazily loaded.
72
    repository as _mod_repository,
73
    revision as _mod_revision,
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
74
    revisionspec,
3535.5.1 by John Arbash Meinel
cleanup a few imports to be lazily loaded.
75
    trace,
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
76
    tsort,
77
    )
78
""")
79
3535.5.1 by John Arbash Meinel
cleanup a few imports to be lazily loaded.
80
from bzrlib import (
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
81
    registry,
2997.1.2 by Kent Gibson
Move all imports to top of log.py
82
    )
83
from bzrlib.osutils import (
84
    format_date,
2997.1.3 by Alexander Belchenko
file wrapper around stdout should use terminal encoding, not user_encoding.
85
    get_terminal_encoding,
4183.6.4 by Martin Pool
Separate out re_compile_checked
86
    re_compile_checked,
2997.1.2 by Kent Gibson
Move all imports to top of log.py
87
    terminal_width,
88
    )
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
89
375 by Martin Pool
- New command touching-revisions and function to trace
90
91
def find_touching_revisions(branch, file_id):
92
    """Yield a description of revisions which affect the file_id.
93
94
    Each returned element is (revno, revision_id, description)
95
96
    This is the list of revisions where the file is either added,
97
    modified, renamed or deleted.
98
99
    TODO: Perhaps some way to limit this to only particular revisions,
522 by Martin Pool
todo
100
    or to traverse a non-mainline set of revisions?
375 by Martin Pool
- New command touching-revisions and function to trace
101
    """
102
    last_ie = None
103
    last_path = None
104
    revno = 1
105
    for revision_id in branch.revision_history():
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
106
        this_inv = branch.repository.get_revision_inventory(revision_id)
375 by Martin Pool
- New command touching-revisions and function to trace
107
        if file_id in this_inv:
108
            this_ie = this_inv[file_id]
109
            this_path = this_inv.id2path(file_id)
110
        else:
111
            this_ie = this_path = None
112
113
        # now we know how it was last time, and how it is in this revision.
114
        # are those two states effectively the same or not?
115
116
        if not this_ie and not last_ie:
117
            # not present in either
118
            pass
119
        elif this_ie and not last_ie:
120
            yield revno, revision_id, "added " + this_path
121
        elif not this_ie and last_ie:
122
            # deleted here
123
            yield revno, revision_id, "deleted " + last_path
124
        elif this_path != last_path:
125
            yield revno, revision_id, ("renamed %s => %s" % (last_path, this_path))
126
        elif (this_ie.text_size != last_ie.text_size
127
              or this_ie.text_sha1 != last_ie.text_sha1):
128
            yield revno, revision_id, "modified " + this_path
129
130
        last_ie = this_ie
131
        last_path = this_path
132
        revno += 1
133
134
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
135
def _enumerate_history(branch):
136
    rh = []
137
    revno = 1
138
    for rev_id in branch.revision_history():
139
        rh.append((revno, rev_id))
140
        revno += 1
141
    return rh
142
143
4202.2.1 by Ian Clatworthy
get directory logging working again
144
class LogRequest(object):
145
    """Query parameters for logging a branch."""
146
147
    def __init__(self,
148
        direction='reverse',
149
        specific_fileids=None,
150
        start_revision=None,
151
        end_revision=None,
152
        limit=None,
153
        message_search=None,
154
        levels=1,
155
        generate_tags=True,
156
        delta_type=None,
157
        diff_type=None,
158
        _match_using_deltas=True,
159
        ):
160
        """Create a logging request.
161
162
        Each of these parameter become a public attribute of the object.
163
164
        :param direction: 'reverse' (default) is latest to earliest;
165
          'forward' is earliest to latest.
166
167
        :param specific_fileids: If not None, only include revisions
168
          affecting the specified files, rather than all revisions.
169
170
        :param start_revision: If not None, only generate
171
          revisions >= start_revision
172
173
        :param end_revision: If not None, only generate
174
          revisions <= end_revision
175
176
        :param limit: If set, generate only 'limit' revisions, all revisions
177
          are shown if None or 0.
178
179
        :param message_search: If not None, only include revisions with
180
          matching commit messages
181
182
        :param levels: the number of levels of revisions to
183
          generate; 1 for just the mainline; 0 for all levels.
184
185
        :param generate_tags: If True, include tags for matched revisions.
186
187
        :param delta_type: Either 'full', 'partial' or None.
188
          'full' means generate the complete delta - adds/deletes/modifies/etc;
189
          'partial' means filter the delta using specific_fileids;
190
          None means do not generate any delta.
191
192
        :param diff_type: Either 'full', 'partial' or None.
193
          'full' means generate the complete diff - adds/deletes/modifies/etc;
194
          'partial' means filter the diff using specific_fileids;
195
          None means do not generate any diff.
196
197
        :param _match_using_deltas: a private parameter controlling the
198
          algorithm used for matching specific_fileids. This parameter
199
          may be removed in the future so bzrlib client code should NOT
200
          use it.
201
        """
202
        self.direction = direction
203
        self.specific_fileids = specific_fileids
204
        self.start_revision = start_revision
205
        self.end_revision = end_revision
206
        self.limit = limit
207
        self.message_search = message_search
208
        self.levels = levels
209
        self.generate_tags = generate_tags
210
        self.delta_type = delta_type
211
        self.diff_type = diff_type
212
        # Add 'private' attributes for features that may be deprecated
213
        self._match_using_deltas = _match_using_deltas
214
        self._allow_single_merge_revision = True
215
216
217
def show_log_request(branch, lf, rqst):
218
    """Write out human-readable log of commits to this branch.
219
220
    :param lf: The LogFormatter object showing the output.
221
222
    :param rqst: The LogRequest object specifying the query parameters.
223
    """
224
    branch.lock_read()
225
    try:
226
        if getattr(lf, 'begin_log', None):
227
            lf.begin_log()
228
229
        _show_log_request(branch, lf, rqst)
230
231
        if getattr(lf, 'end_log', None):
232
            lf.end_log()
233
    finally:
234
        branch.unlock()
235
236
378 by Martin Pool
- New usage bzr log FILENAME
237
def show_log(branch,
794 by Martin Pool
- Merge John's nice short-log format.
238
             lf,
527 by Martin Pool
- refactor log command
239
             specific_fileid=None,
378 by Martin Pool
- New usage bzr log FILENAME
240
             verbose=False,
567 by Martin Pool
- New form 'bzr log -r FROM:TO'
241
             direction='reverse',
242
             start_revision=None,
900 by Martin Pool
- patch from john to search for matching commits
243
             end_revision=None,
2466.9.1 by Kent Gibson
add bzr log --limit
244
             search=None,
3943.5.1 by Ian Clatworthy
first cut at log --show-diff
245
             limit=None,
246
             show_diff=False):
369 by Martin Pool
- Split out log printing into new show_log function
247
    """Write out human-readable log of commits to this branch.
248
4202.2.1 by Ian Clatworthy
get directory logging working again
249
    Note: show_log_request() is now the preferred API to this one.
250
    This function is being retained for backwards compatibility but
251
    should not be extended with new parameters.
252
3874.2.2 by Vincent Ladeuil
Cleanup show_log doc string.
253
    :param lf: The LogFormatter object showing the output.
254
255
    :param specific_fileid: If not None, list only the commits affecting the
256
        specified file, rather than all commits.
257
258
    :param verbose: If True show added/changed/deleted/renamed files.
259
260
    :param direction: 'reverse' (default) is latest to earliest; 'forward' is
261
        earliest to latest.
262
263
    :param start_revision: If not None, only show revisions >= start_revision
264
265
    :param end_revision: If not None, only show revisions <= end_revision
266
267
    :param search: If not None, only show revisions with matching commit
268
        messages
269
270
    :param limit: If set, shows only 'limit' revisions, all revisions are shown
271
        if None or 0.
3943.5.1 by Ian Clatworthy
first cut at log --show-diff
272
273
    :param show_diff: If True, output a diff after each revision.
369 by Martin Pool
- Split out log printing into new show_log function
274
    """
4202.2.1 by Ian Clatworthy
get directory logging working again
275
    # Convert old-style parameters to new-style parameters
276
    if specific_fileid is not None:
277
        file_ids = [specific_fileid]
278
    else:
279
        file_ids = None
280
    if verbose:
281
        if file_ids:
282
            delta_type = 'partial'
283
        else:
284
            delta_type = 'full'
285
    else:
286
        delta_type = None
287
    if show_diff:
288
        if file_ids:
289
            diff_type = 'partial'
290
        else:
291
            diff_type = 'full'
292
    else:
293
        diff_type = None
294
295
    # Build the request and execute it
296
    rqst = LogRequest(direction=direction, specific_fileids=file_ids,
297
        start_revision=start_revision, end_revision=end_revision,
298
        limit=limit, message_search=search,
299
        delta_type=delta_type, diff_type=diff_type)
300
    show_log_request(branch, lf, rqst)
301
302
303
def _show_log_request(branch, lf, rqst):
304
    """Worker function for show_log_request - see show_log_request."""
794 by Martin Pool
- Merge John's nice short-log format.
305
    if not isinstance(lf, LogFormatter):
306
        warn("not a LogFormatter instance: %r" % lf)
3936.3.1 by Ian Clatworthy
refactor _show_log
307
4202.2.1 by Ian Clatworthy
get directory logging working again
308
    # Tweak the LogRequest based on what the LogFormatter can handle.
309
    # (There's no point generating stuff if the formatter can't display it.)
310
    rqst.levels = lf.get_levels()
311
    if not getattr(lf, 'supports_tags', False):
312
        rqst.generate_tags = False
313
    if not getattr(lf, 'supports_delta', False):
314
        rqst.delta_type = None
315
    if not getattr(lf, 'supports_diff', False):
316
        rqst.diff_type = None
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
317
    if not getattr(lf, 'supports_merge_revisions', False):
4202.2.1 by Ian Clatworthy
get directory logging working again
318
        rqst._allow_single_merge_revision = getattr(lf,
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
319
            'supports_single_merge_revision', False)
3302.1.2 by Aaron Bentley
Split out the major view_revision calculation logic
320
3936.3.1 by Ian Clatworthy
refactor _show_log
321
    # Find and print the interesting revisions
4202.2.1 by Ian Clatworthy
get directory logging working again
322
    generator = _LogGenerator(branch, rqst)
323
    for lr in generator.iter_log_revisions():
324
        lf.log_revision(lr)
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
325
326
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
327
class _StartNotLinearAncestor(Exception):
328
    """Raised when a start revision is not found walking left-hand history."""
329
330
4202.2.1 by Ian Clatworthy
get directory logging working again
331
class _LogGenerator(object):
332
    """A generator of log revisions given a branch and a LogRequest."""
333
334
    def __init__(self, branch, rqst):
335
        self.branch = branch
336
        self.rqst = rqst
337
        if rqst.generate_tags and branch.supports_tags():
338
            self.rev_tag_dict = branch.tags.get_reverse_tag_dict()
339
        else:
340
            self.rev_tag_dict = {}
341
342
    def iter_log_revisions(self):
343
        """Iterate over LogRevision objects.
344
345
        :return: An iterator yielding LogRevision objects.
346
        """
347
        rqst = self.rqst
348
        log_count = 0
349
        revision_iterator = self._create_log_revision_iterator()
350
        for revs in revision_iterator:
351
            for (rev_id, revno, merge_depth), rev, delta in revs:
352
                # 0 levels means show everything; merge_depth counts from 0
353
                if rqst.levels != 0 and merge_depth >= rqst.levels:
354
                    continue
355
                diff = self._format_diff(rev, rev_id)
356
                yield LogRevision(rev, revno, merge_depth, delta,
357
                    self.rev_tag_dict.get(rev_id), diff)
358
                if rqst.limit:
359
                    log_count += 1
360
                    if log_count >= rqst.limit:
361
                        return
362
363
    def _format_diff(self, rev, rev_id):
364
        diff_type = self.rqst.diff_type
365
        if diff_type is None:
366
            return None
367
        repo = self.branch.repository
368
        if len(rev.parent_ids) == 0:
369
            ancestor_id = _mod_revision.NULL_REVISION
370
        else:
371
            ancestor_id = rev.parent_ids[0]
372
        tree_1 = repo.revision_tree(ancestor_id)
373
        tree_2 = repo.revision_tree(rev_id)
374
        file_ids = self.rqst.specific_fileids
375
        if diff_type == 'partial' and file_ids is not None:
376
            specific_files = [tree_2.id2path(id) for id in file_ids]
377
        else:
378
            specific_files = None
379
        s = StringIO()
380
        diff.show_diff_trees(tree_1, tree_2, s, specific_files, old_label='',
381
            new_label='')
382
        return s.getvalue()
383
384
    def _create_log_revision_iterator(self):
385
        """Create a revision iterator for log.
386
387
        :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
388
            delta).
389
        """
390
        self.start_rev_id, self.end_rev_id = _get_revision_limits(
391
            self.branch, self.rqst.start_revision, self.rqst.end_revision)
392
        if self.rqst._match_using_deltas:
393
            return self._log_revision_iterator_using_delta_matching()
394
        else:
395
            # We're using the per-file-graph algorithm. This scales really
396
            # well but only makes sense if there is a single file and it's
397
            # not a directory
398
            file_count = len(self.rqst.specific_fileids)
399
            if file_count != 1:
400
                raise BzrError("illegal LogRequest: must match-using-deltas "
401
                    "when logging %d files" % file_count)
402
            return self._log_revision_iterator_using_per_file_graph()
403
404
    def _log_revision_iterator_using_delta_matching(self):
405
        # Get the base revisions, filtering by the revision range
406
        rqst = self.rqst
407
        generate_merge_revisions = rqst.levels != 1
408
        delayed_graph_generation = not rqst.specific_fileids and (
409
                rqst.limit or self.start_rev_id or self.end_rev_id)
410
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
411
            self.end_rev_id, rqst.direction, generate_merge_revisions,
412
            rqst._allow_single_merge_revision,
413
            delayed_graph_generation=delayed_graph_generation)
414
415
        # Apply the other filters
416
        return make_log_rev_iterator(self.branch, view_revisions,
417
            rqst.delta_type, rqst.message_search,
418
            file_ids=rqst.specific_fileids, direction=rqst.direction)
419
420
    def _log_revision_iterator_using_per_file_graph(self):
421
        # Get the base revisions, filtering by the revision range.
422
        # Note that we always generate the merge revisions because
423
        # filter_revisions_touching_file_id() requires them ...
424
        rqst = self.rqst
425
        view_revisions = _calc_view_revisions(self.branch, self.start_rev_id,
426
            self.end_rev_id, rqst.direction, True,
427
            rqst._allow_single_merge_revision)
3936.3.37 by Ian Clatworthy
selectively delay graph generation, not always
428
        if not isinstance(view_revisions, list):
429
            view_revisions = list(view_revisions)
4202.2.1 by Ian Clatworthy
get directory logging working again
430
        view_revisions = _filter_revisions_touching_file_id(self.branch,
431
            rqst.specific_fileids[0], view_revisions,
432
            include_merges=rqst.levels != 1)
433
        return make_log_rev_iterator(self.branch, view_revisions,
434
            rqst.delta_type, rqst.message_search)
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
435
436
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
437
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
3936.3.37 by Ian Clatworthy
selectively delay graph generation, not always
438
    generate_merge_revisions, allow_single_merge_revision,
439
    delayed_graph_generation=False):
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
440
    """Calculate the revisions to view.
441
442
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
443
             a list of the same tuples.
444
    """
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
445
    br_revno, br_rev_id = branch.last_revision_info()
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
446
    if br_revno == 0:
447
        return []
448
3936.3.10 by Ian Clatworthy
more single revision clean-up
449
    # If a single revision is requested, check we can handle it
3936.3.35 by Ian Clatworthy
simplify single revision logic
450
    generate_single_revision = (end_rev_id and start_rev_id == end_rev_id and
451
        (not generate_merge_revisions or not _has_merges(branch, end_rev_id)))
3936.3.10 by Ian Clatworthy
more single revision clean-up
452
    if generate_single_revision:
4202.2.1 by Ian Clatworthy
get directory logging working again
453
        return _generate_one_revision(branch, end_rev_id, br_rev_id, br_revno,
454
            allow_single_merge_revision)
3936.3.10 by Ian Clatworthy
more single revision clean-up
455
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
456
    # If we only want to see linear revisions, we can iterate ...
3936.3.10 by Ian Clatworthy
more single revision clean-up
457
    if not generate_merge_revisions:
4202.2.1 by Ian Clatworthy
get directory logging working again
458
        return _generate_flat_revisions(branch, start_rev_id, end_rev_id,
459
            direction)
460
    else:
461
        return _generate_all_revisions(branch, start_rev_id, end_rev_id,
462
            direction, delayed_graph_generation)
463
464
465
def _generate_one_revision(branch, rev_id, br_rev_id, br_revno,
466
    allow_single_merge_revision):
467
    if rev_id == br_rev_id:
468
        # It's the tip
469
        return [(br_rev_id, br_revno, 0)]
470
    else:
471
        revno = branch.revision_id_to_dotted_revno(rev_id)
472
        if len(revno) > 1 and not allow_single_merge_revision:
473
            # It's a merge revision and the log formatter is
474
            # completely brain dead. This "feature" of allowing
475
            # log formatters incapable of displaying dotted revnos
476
            # ought to be deprecated IMNSHO. IGC 20091022
477
            raise errors.BzrCommandError('Selected log formatter only'
478
                ' supports mainline revisions.')
479
        revno_str = '.'.join(str(n) for n in revno)
480
        return [(rev_id, revno_str, 0)]
481
482
483
def _generate_flat_revisions(branch, start_rev_id, end_rev_id, direction):
484
    result = _linear_view_revisions(branch, start_rev_id, end_rev_id)
485
    # If a start limit was given and it's not obviously an
486
    # ancestor of the end limit, check it before outputting anything
487
    if direction == 'forward' or (start_rev_id
488
        and not _is_obvious_ancestor(branch, start_rev_id, end_rev_id)):
489
        try:
490
            result = list(result)
491
        except _StartNotLinearAncestor:
492
            raise errors.BzrCommandError('Start revision not found in'
493
                ' left-hand history of end revision.')
494
    if direction == 'forward':
495
        result = reversed(result)
496
    return result
497
498
499
def _generate_all_revisions(branch, start_rev_id, end_rev_id, direction,
500
    delayed_graph_generation):
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
501
    # On large trees, generating the merge graph can take 30-60 seconds
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
502
    # so we delay doing it until a merge is detected, incrementally
503
    # returning initial (non-merge) revisions while we can.
504
    initial_revisions = []
3936.3.37 by Ian Clatworthy
selectively delay graph generation, not always
505
    if delayed_graph_generation:
506
        try:
507
            for rev_id, revno, depth in \
508
                _linear_view_revisions(branch, start_rev_id, end_rev_id):
509
                if _has_merges(branch, rev_id):
510
                    end_rev_id = rev_id
511
                    break
512
                else:
513
                    initial_revisions.append((rev_id, revno, depth))
514
            else:
515
                # No merged revisions found
516
                if direction == 'reverse':
517
                    return initial_revisions
518
                elif direction == 'forward':
519
                    return reversed(initial_revisions)
520
                else:
521
                    raise ValueError('invalid direction %r' % direction)
522
        except _StartNotLinearAncestor:
523
            # A merge was never detected so the lower revision limit can't
524
            # be nested down somewhere
525
            raise errors.BzrCommandError('Start revision not found in'
526
                ' history of end revision.')
3936.3.15 by Ian Clatworthy
faster long log for a limited range with no merges
527
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
528
    # A log including nested merges is required. If the direction is reverse,
529
    # we rebase the initial merge depths so that the development line is
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
530
    # shown naturally, i.e. just like it is for linear logging. We can easily
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
531
    # make forward the exact opposite display, but showing the merge revisions
532
    # indented at the end seems slightly nicer in that case.
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
533
    view_revisions = chain(iter(initial_revisions),
534
        _graph_view_revisions(branch, start_rev_id, end_rev_id,
535
        rebase_initial_depths=direction == 'reverse'))
3842.2.5 by Vincent Ladeuil
Better fix for bug #300055.
536
    if direction == 'reverse':
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
537
        return view_revisions
538
    elif direction == 'forward':
539
        # Forward means oldest first, adjusting for depth.
540
        view_revisions = reverse_by_depth(list(view_revisions))
541
        return _rebase_merge_depth(view_revisions)
542
    else:
543
        raise ValueError('invalid direction %r' % direction)
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
544
530 by Martin Pool
- put back verbose log support for reversed logs
545
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
546
def _has_merges(branch, rev_id):
547
    """Does a revision have multiple parents or not?"""
3936.3.40 by Ian Clatworthy
review feedback from jam
548
    parents = branch.repository.get_parent_map([rev_id]).get(rev_id, [])
549
    return len(parents) > 1
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
550
551
3936.3.31 by Ian Clatworthy
nicer obvious ancestor checking
552
def _is_obvious_ancestor(branch, start_rev_id, end_rev_id):
553
    """Is start_rev_id an obvious ancestor of end_rev_id?"""
554
    if start_rev_id and end_rev_id:
555
        start_dotted = branch.revision_id_to_dotted_revno(start_rev_id)
556
        end_dotted = branch.revision_id_to_dotted_revno(end_rev_id)
557
        if len(start_dotted) == 1 and len(end_dotted) == 1:
558
            # both on mainline
559
            return start_dotted[0] <= end_dotted[0]
560
        elif (len(start_dotted) == 3 and len(end_dotted) == 3 and
561
            start_dotted[0:1] == end_dotted[0:1]):
562
            # both on same development line
563
            return start_dotted[2] <= end_dotted[2]
564
        else:
565
            # not obvious
3936.3.18 by Ian Clatworthy
faster incremental results for FILE logging
566
            return False
567
    return True
568
569
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
570
def _linear_view_revisions(branch, start_rev_id, end_rev_id):
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
571
    """Calculate a sequence of revisions to view, newest to oldest.
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
572
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
573
    :param start_rev_id: the lower revision-id
574
    :param end_rev_id: the upper revision-id
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
575
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
576
    :raises _StartNotLinearAncestor: if a start_rev_id is specified but
577
      is not found walking the left-hand history
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
578
    """
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
579
    br_revno, br_rev_id = branch.last_revision_info()
3943.4.5 by John Arbash Meinel
Restore _linear_view_revisions.
580
    repo = branch.repository
3936.3.6 by Ian Clatworthy
add & use _NonMainlineRevisionLimit exception
581
    if start_rev_id is None and end_rev_id is None:
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
582
        cur_revno = br_revno
583
        for revision_id in repo.iter_reverse_revision_history(br_rev_id):
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
584
            yield revision_id, str(cur_revno), 0
3936.3.6 by Ian Clatworthy
add & use _NonMainlineRevisionLimit exception
585
            cur_revno -= 1
3936.3.14 by Ian Clatworthy
bug fix
586
    else:
587
        if end_rev_id is None:
588
            end_rev_id = br_rev_id
3936.3.6 by Ian Clatworthy
add & use _NonMainlineRevisionLimit exception
589
        found_start = start_rev_id is None
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
590
        for revision_id in repo.iter_reverse_revision_history(end_rev_id):
3936.3.26 by Ian Clatworthy
use new dotted-revno-revision-id conversion methods to simplify & speed up code
591
            revno = branch.revision_id_to_dotted_revno(revision_id)
592
            revno_str = '.'.join(str(n) for n in revno)
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
593
            if not found_start and revision_id == start_rev_id:
594
                yield revision_id, revno_str, 0
595
                found_start = True
3936.3.6 by Ian Clatworthy
add & use _NonMainlineRevisionLimit exception
596
                break
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
597
            else:
598
                yield revision_id, revno_str, 0
3936.3.6 by Ian Clatworthy
add & use _NonMainlineRevisionLimit exception
599
        else:
3936.3.12 by Ian Clatworthy
more single revision & sequence tuning
600
            if not found_start:
601
                raise _StartNotLinearAncestor()
3302.1.3 by Aaron Bentley
Add optimization of the simple case of generating view revisions
602
603
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
604
def _graph_view_revisions(branch, start_rev_id, end_rev_id,
605
    rebase_initial_depths=True):
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
606
    """Calculate revisions to view including merges, newest to oldest.
607
608
    :param branch: the branch
3936.3.32 by Ian Clatworthy
always delay merge graph generation until necessary
609
    :param start_rev_id: the lower revision-id
610
    :param end_rev_id: the upper revision-id
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
611
    :param rebase_initial_depth: should depths be rebased until a mainline
612
      revision is found?
613
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples.
614
    """
615
    view_revisions = branch.iter_merge_sorted_revisions(
616
        start_revision_id=end_rev_id, stop_revision_id=start_rev_id,
617
        stop_rule="with-merges")
618
    if not rebase_initial_depths:
619
        for (rev_id, merge_depth, revno, end_of_merge
620
             ) in view_revisions:
621
            yield rev_id, '.'.join(map(str, revno)), merge_depth
622
    else:
623
        # We're following a development line starting at a merged revision.
624
        # We need to adjust depths down by the initial depth until we find
625
        # a depth less than it. Then we use that depth as the adjustment.
626
        # If and when we reach the mainline, depth adjustment ends.
627
        depth_adjustment = None
628
        for (rev_id, merge_depth, revno, end_of_merge
629
             ) in view_revisions:
630
            if depth_adjustment is None:
631
                depth_adjustment = merge_depth
632
            if depth_adjustment:
633
                if merge_depth < depth_adjustment:
634
                    depth_adjustment = merge_depth
635
                merge_depth -= depth_adjustment
636
            yield rev_id, '.'.join(map(str, revno)), merge_depth
637
638
3936.3.13 by Ian Clatworthy
feedback from jameinel
639
def calculate_view_revisions(branch, start_revision, end_revision, direction,
640
        specific_fileid, generate_merge_revisions, allow_single_merge_revision):
641
    """Calculate the revisions to view.
642
643
    :return: An iterator of (revision_id, dotted_revno, merge_depth) tuples OR
644
             a list of the same tuples.
645
    """
3936.3.16 by Ian Clatworthy
use deltas to match files in selected use cases
646
    # This method is no longer called by the main code path.
647
    # It is retained for API compatibility and may be deprecated
648
    # soon. IGC 20090116
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
649
    start_rev_id, end_rev_id = _get_revision_limits(branch, start_revision,
650
        end_revision)
651
    view_revisions = list(_calc_view_revisions(branch, start_rev_id, end_rev_id,
3936.3.13 by Ian Clatworthy
feedback from jameinel
652
        direction, generate_merge_revisions or specific_fileid,
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
653
        allow_single_merge_revision))
3936.3.13 by Ian Clatworthy
feedback from jameinel
654
    if specific_fileid:
655
        view_revisions = _filter_revisions_touching_file_id(branch,
656
            specific_fileid, view_revisions,
657
            include_merges=generate_merge_revisions)
3936.3.28 by Ian Clatworthy
api compatibility: calculate_view_revisions rebases merge depth again
658
    return _rebase_merge_depth(view_revisions)
3936.3.13 by Ian Clatworthy
feedback from jameinel
659
660
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
661
def _rebase_merge_depth(view_revisions):
662
    """Adjust depths upwards so the top level is 0."""
663
    # If either the first or last revision have a merge_depth of 0, we're done
664
    if view_revisions and view_revisions[0][2] and view_revisions[-1][2]:
665
        min_depth = min([d for r,n,d in view_revisions])
666
        if min_depth != 0:
667
            view_revisions = [(r,n,d-min_depth) for r,n,d in view_revisions]
668
    return view_revisions
669
670
3936.3.16 by Ian Clatworthy
use deltas to match files in selected use cases
671
def make_log_rev_iterator(branch, view_revisions, generate_delta, search,
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
672
        file_ids=None, direction='reverse'):
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
673
    """Create a revision iterator for log.
674
675
    :param branch: The branch being logged.
676
    :param view_revisions: The revisions being viewed.
677
    :param generate_delta: Whether to generate a delta for each revision.
4202.2.1 by Ian Clatworthy
get directory logging working again
678
      Permitted values are None, 'full' and 'partial'.
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
679
    :param search: A user text search string.
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
680
    :param file_ids: If non empty, only revisions matching one or more of
681
      the file-ids are to be kept.
682
    :param direction: the direction in which view_revisions is sorted
3642.1.7 by Robert Collins
Review feedback.
683
    :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
684
        delta).
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
685
    """
3642.1.5 by Robert Collins
Separate out batching of revisions.
686
    # Convert view_revisions into (view, None, None) groups to fit with
687
    # the standard interface here.
688
    if type(view_revisions) == list:
3642.1.7 by Robert Collins
Review feedback.
689
        # A single batch conversion is faster than many incremental ones.
690
        # As we have all the data, do a batch conversion.
3642.1.5 by Robert Collins
Separate out batching of revisions.
691
        nones = [None] * len(view_revisions)
692
        log_rev_iterator = iter([zip(view_revisions, nones, nones)])
693
    else:
694
        def _convert():
695
            for view in view_revisions:
696
                yield (view, None, None)
697
        log_rev_iterator = iter([_convert()])
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
698
    for adapter in log_adapters:
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
699
        # It would be nicer if log adapters were first class objects
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
700
        # with custom parameters. This will do for now. IGC 20090127
701
        if adapter == _make_delta_filter:
702
            log_rev_iterator = adapter(branch, generate_delta,
703
                search, log_rev_iterator, file_ids, direction)
704
        else:
705
            log_rev_iterator = adapter(branch, generate_delta,
706
                search, log_rev_iterator)
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
707
    return log_rev_iterator
708
709
3642.1.7 by Robert Collins
Review feedback.
710
def _make_search_filter(branch, generate_delta, search, log_rev_iterator):
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
711
    """Create a filtered iterator of log_rev_iterator matching on a regex.
712
713
    :param branch: The branch being logged.
714
    :param generate_delta: Whether to generate a delta for each revision.
715
    :param search: A user text search string.
716
    :param log_rev_iterator: An input iterator containing all revisions that
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
717
        could be displayed, in lists.
3642.1.7 by Robert Collins
Review feedback.
718
    :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
719
        delta).
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
720
    """
721
    if search is None:
722
        return log_rev_iterator
4183.6.4 by Martin Pool
Separate out re_compile_checked
723
    searchRE = re_compile_checked(search, re.IGNORECASE,
724
            'log message filter')
3642.1.1 by Robert Collins
Refactoring in log towards more pluggable revision selection.
725
    return _filter_message_re(searchRE, log_rev_iterator)
726
727
728
def _filter_message_re(searchRE, log_rev_iterator):
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
729
    for revs in log_rev_iterator:
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
730
        new_revs = []
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
731
        for (rev_id, revno, merge_depth), rev, delta in revs:
732
            if searchRE.search(rev.message):
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
733
                new_revs.append(((rev_id, revno, merge_depth), rev, delta))
734
        yield new_revs
735
736
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
737
def _make_delta_filter(branch, generate_delta, search, log_rev_iterator,
738
    fileids=None, direction='reverse'):
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
739
    """Add revision deltas to a log iterator if needed.
740
741
    :param branch: The branch being logged.
742
    :param generate_delta: Whether to generate a delta for each revision.
4202.2.1 by Ian Clatworthy
get directory logging working again
743
      Permitted values are None, 'full' and 'partial'.
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
744
    :param search: A user text search string.
745
    :param log_rev_iterator: An input iterator containing all revisions that
746
        could be displayed, in lists.
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
747
    :param fileids: If non empty, only revisions matching one or more of
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
748
      the file-ids are to be kept.
749
    :param direction: the direction in which view_revisions is sorted
3642.1.7 by Robert Collins
Review feedback.
750
    :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
751
        delta).
752
    """
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
753
    if not generate_delta and not fileids:
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
754
        return log_rev_iterator
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
755
    return _generate_deltas(branch.repository, log_rev_iterator,
756
        generate_delta, fileids, direction)
757
758
4202.2.1 by Ian Clatworthy
get directory logging working again
759
def _generate_deltas(repository, log_rev_iterator, delta_type, fileids,
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
760
    direction):
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
761
    """Create deltas for each batch of revisions in log_rev_iterator.
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
762
3936.3.36 by Ian Clatworthy
minor comment polish & refactoring
763
    If we're only generating deltas for the sake of filtering against
764
    file-ids, we stop generating deltas once all file-ids reach the
765
    appropriate life-cycle point. If we're receiving data newest to
766
    oldest, then that life-cycle point is 'add', otherwise it's 'remove'.
767
    """
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
768
    check_fileids = fileids is not None and len(fileids) > 0
769
    if check_fileids:
770
        fileid_set = set(fileids)
771
        if direction == 'reverse':
772
            stop_on = 'add'
773
        else:
774
            stop_on = 'remove'
775
    else:
776
        fileid_set = None
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
777
    for revs in log_rev_iterator:
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
778
        # If we were matching against fileids and we've run out,
3936.3.40 by Ian Clatworthy
review feedback from jam
779
        # there's nothing left to do
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
780
        if check_fileids and not fileid_set:
3936.3.40 by Ian Clatworthy
review feedback from jam
781
            return
3642.1.3 by Robert Collins
Split out delta generation from revision content reading, and structure it after message evaluation, increasing performance of log -v -m.
782
        revisions = [rev[1] for rev in revs]
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
783
        new_revs = []
4202.2.1 by Ian Clatworthy
get directory logging working again
784
        if delta_type == 'full' and not check_fileids:
785
            deltas = repository.get_deltas_for_revisions(revisions)
786
            for rev, delta in izip(revs, deltas):
787
                new_revs.append((rev[0], rev[1], delta))
788
        else:
789
            deltas = repository.get_deltas_for_revisions(revisions, fileid_set)
790
            for rev, delta in izip(revs, deltas):
791
                if check_fileids:
792
                    if delta is None or not delta.has_changed():
793
                        continue
794
                    else:
795
                        _update_fileids(delta, fileid_set, stop_on)
796
                        if delta_type is None:
797
                            delta = None
798
                        elif delta_type == 'full':
799
                            # If the file matches all the time, rebuilding
800
                            # a full delta like this in addition to a partial
801
                            # one could be slow. However, it's likely the
802
                            # most revisions won't get this far, making it
803
                            # faster to filter on the partial deltas and
804
                            # build the occasional full delta than always
805
                            # building full deltas and filtering those.
806
                            rev_id = rev[0][0]
807
                            delta = repository.get_revision_delta(rev_id)
808
                new_revs.append((rev[0], rev[1], delta))
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
809
        yield new_revs
810
811
4202.2.1 by Ian Clatworthy
get directory logging working again
812
def _update_fileids(delta, fileids, stop_on):
813
    """Update the set of file-ids to search based on file lifecycle events.
814
    
815
    :param fileids: a set of fileids to update
3936.3.33 by Ian Clatworthy
only generate deltas for file matching as long as necessary
816
    :param stop_on: either 'add' or 'remove' - take file-ids out of the
817
      fileids set once their add or remove entry is detected respectively
818
    """
4202.2.1 by Ian Clatworthy
get directory logging working again
819
    if stop_on == 'add':
820
        for item in delta.added:
821
            if item[1] in fileids:
822
                fileids.remove(item[1])
823
    elif stop_on == 'delete':
824
        for item in delta.removed:
825
            if item[1] in fileids:
826
                fileids.remove(item[1])
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
827
828
3642.1.7 by Robert Collins
Review feedback.
829
def _make_revision_objects(branch, generate_delta, search, log_rev_iterator):
3642.1.4 by Robert Collins
Factor out revision object extraction from revision batching.
830
    """Extract revision objects from the repository
831
832
    :param branch: The branch being logged.
833
    :param generate_delta: Whether to generate a delta for each revision.
834
    :param search: A user text search string.
835
    :param log_rev_iterator: An input iterator containing all revisions that
836
        could be displayed, in lists.
3642.1.7 by Robert Collins
Review feedback.
837
    :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
3642.1.4 by Robert Collins
Factor out revision object extraction from revision batching.
838
        delta).
839
    """
3642.1.5 by Robert Collins
Separate out batching of revisions.
840
    repository = branch.repository
3642.1.4 by Robert Collins
Factor out revision object extraction from revision batching.
841
    for revs in log_rev_iterator:
842
        # r = revision_id, n = revno, d = merge depth
843
        revision_ids = [view[0] for view, _, _ in revs]
844
        revisions = repository.get_revisions(revision_ids)
845
        revs = [(rev[0], revision, rev[2]) for rev, revision in
846
            izip(revs, revisions)]
847
        yield revs
848
849
3642.1.7 by Robert Collins
Review feedback.
850
def _make_batch_filter(branch, generate_delta, search, log_rev_iterator):
3642.1.5 by Robert Collins
Separate out batching of revisions.
851
    """Group up a single large batch into smaller ones.
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
852
853
    :param branch: The branch being logged.
854
    :param generate_delta: Whether to generate a delta for each revision.
855
    :param search: A user text search string.
3642.1.5 by Robert Collins
Separate out batching of revisions.
856
    :param log_rev_iterator: An input iterator containing all revisions that
857
        could be displayed, in lists.
3874.2.4 by Vincent Ladeuil
Fix too long lines.
858
    :return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
859
        delta).
3642.1.2 by Robert Collins
Setup a log iterator that more closely matches what the code tries to do with repository operations.
860
    """
861
    repository = branch.repository
3302.1.1 by Aaron Bentley
Split out _iter_revision, allow view_revisions to be an iterator
862
    num = 9
3642.1.5 by Robert Collins
Separate out batching of revisions.
863
    for batch in log_rev_iterator:
864
        batch = iter(batch)
865
        while True:
866
            step = [detail for _, detail in zip(range(num), batch)]
867
            if len(step) == 0:
868
                break
869
            yield step
870
            num = min(int(num * 1.5), 200)
3302.1.1 by Aaron Bentley
Split out _iter_revision, allow view_revisions to be an iterator
871
872
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
873
def _get_revision_limits(branch, start_revision, end_revision):
874
    """Get and check revision limits.
875
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
876
    :param  branch: The branch containing the revisions.
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
877
878
    :param  start_revision: The first revision to be logged.
879
            For backwards compatibility this may be a mainline integer revno,
880
            but for merge revision support a RevisionInfo is expected.
881
882
    :param  end_revision: The last revision to be logged.
883
            For backwards compatibility this may be a mainline integer revno,
884
            but for merge revision support a RevisionInfo is expected.
885
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
886
    :return: (start_rev_id, end_rev_id) tuple.
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
887
    """
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
888
    branch_revno, branch_rev_id = branch.last_revision_info()
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
889
    start_rev_id = None
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
890
    if start_revision is None:
891
        start_revno = 1
892
    else:
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
893
        if isinstance(start_revision, revisionspec.RevisionInfo):
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
894
            start_rev_id = start_revision.rev_id
895
            start_revno = start_revision.revno or 1
896
        else:
897
            branch.check_real_revno(start_revision)
898
            start_revno = start_revision
3936.3.25 by Ian Clatworthy
fix bug when start/end revision are integers
899
            start_rev_id = branch.get_rev_id(start_revno)
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
900
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
901
    end_rev_id = None
902
    if end_revision is None:
3449.2.5 by John Arbash Meinel
Stop referencing the variable I removed.
903
        end_revno = branch_revno
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
904
    else:
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
905
        if isinstance(end_revision, revisionspec.RevisionInfo):
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
906
            end_rev_id = end_revision.rev_id
3449.2.5 by John Arbash Meinel
Stop referencing the variable I removed.
907
            end_revno = end_revision.revno or branch_revno
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
908
        else:
909
            branch.check_real_revno(end_revision)
910
            end_revno = end_revision
3936.3.25 by Ian Clatworthy
fix bug when start/end revision are integers
911
            end_rev_id = branch.get_rev_id(end_revno)
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
912
3936.3.4 by Ian Clatworthy
fix empty_branch log
913
    if branch_revno != 0:
914
        if (start_rev_id == _mod_revision.NULL_REVISION
915
            or end_rev_id == _mod_revision.NULL_REVISION):
916
            raise errors.BzrCommandError('Logging revision 0 is invalid.')
917
        if start_revno > end_revno:
918
            raise errors.BzrCommandError("Start revision must be older than "
919
                                         "the end revision.")
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
920
    return (start_rev_id, end_rev_id)
921
922
923
def _get_mainline_revs(branch, start_revision, end_revision):
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
924
    """Get the mainline revisions from the branch.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
925
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
926
    Generates the list of mainline revisions for the branch.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
927
928
    :param  branch: The branch containing the revisions.
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
929
930
    :param  start_revision: The first revision to be logged.
931
            For backwards compatibility this may be a mainline integer revno,
932
            but for merge revision support a RevisionInfo is expected.
933
934
    :param  end_revision: The last revision to be logged.
935
            For backwards compatibility this may be a mainline integer revno,
936
            but for merge revision support a RevisionInfo is expected.
937
938
    :return: A (mainline_revs, rev_nos, start_rev_id, end_rev_id) tuple.
3936.3.3 by Ian Clatworthy
add --strict and more refactoring
939
    """
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
940
    branch_revno, branch_last_revision = branch.last_revision_info()
941
    if branch_revno == 0:
942
        return None, None, None, None
943
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
944
    # For mainline generation, map start_revision and end_revision to
945
    # mainline revnos. If the revision is not on the mainline choose the
946
    # appropriate extreme of the mainline instead - the extra will be
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
947
    # filtered later.
948
    # Also map the revisions to rev_ids, to be used in the later filtering
949
    # stage.
950
    start_rev_id = None
951
    if start_revision is None:
952
        start_revno = 1
953
    else:
954
        if isinstance(start_revision, revisionspec.RevisionInfo):
955
            start_rev_id = start_revision.rev_id
956
            start_revno = start_revision.revno or 1
957
        else:
958
            branch.check_real_revno(start_revision)
959
            start_revno = start_revision
960
961
    end_rev_id = None
962
    if end_revision is None:
963
        end_revno = branch_revno
964
    else:
965
        if isinstance(end_revision, revisionspec.RevisionInfo):
966
            end_rev_id = end_revision.rev_id
967
            end_revno = end_revision.revno or branch_revno
968
        else:
969
            branch.check_real_revno(end_revision)
970
            end_revno = end_revision
971
972
    if ((start_rev_id == _mod_revision.NULL_REVISION)
973
        or (end_rev_id == _mod_revision.NULL_REVISION)):
974
        raise errors.BzrCommandError('Logging revision 0 is invalid.')
975
    if start_revno > end_revno:
976
        raise errors.BzrCommandError("Start revision must be older than "
977
                                     "the end revision.")
978
979
    if end_revno < start_revno:
980
        return None, None, None, None
981
    cur_revno = branch_revno
3449.2.1 by John Arbash Meinel
bzr uncommit doesn't need to work in terms of 'revision_history()'
982
    rev_nos = {}
983
    mainline_revs = []
984
    for revision_id in branch.repository.iter_reverse_revision_history(
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
985
                        branch_last_revision):
3449.2.1 by John Arbash Meinel
bzr uncommit doesn't need to work in terms of 'revision_history()'
986
        if cur_revno < start_revno:
3449.2.2 by John Arbash Meinel
Fix bug #172649. Cleanup, and handle the case where we are logging to the first revision.
987
            # We have gone far enough, but we always add 1 more revision
3449.2.1 by John Arbash Meinel
bzr uncommit doesn't need to work in terms of 'revision_history()'
988
            rev_nos[revision_id] = cur_revno
989
            mainline_revs.append(revision_id)
990
            break
991
        if cur_revno <= end_revno:
992
            rev_nos[revision_id] = cur_revno
993
            mainline_revs.append(revision_id)
994
        cur_revno -= 1
3449.2.2 by John Arbash Meinel
Fix bug #172649. Cleanup, and handle the case where we are logging to the first revision.
995
    else:
996
        # We walked off the edge of all revisions, so we add a 'None' marker
997
        mainline_revs.append(None)
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
998
3449.2.1 by John Arbash Meinel
bzr uncommit doesn't need to work in terms of 'revision_history()'
999
    mainline_revs.reverse()
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1000
1001
    # override the mainline to look like the revision history.
3936.3.34 by Ian Clatworthy
return _mainline_revs() API as used in missing.py
1002
    return mainline_revs, rev_nos, start_rev_id, end_rev_id
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1003
1004
1005
def _filter_revision_range(view_revisions, start_rev_id, end_rev_id):
1006
    """Filter view_revisions based on revision ranges.
1007
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1008
    :param view_revisions: A list of (revision_id, dotted_revno, merge_depth)
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1009
            tuples to be filtered.
1010
1011
    :param start_rev_id: If not NONE specifies the first revision to be logged.
1012
            If NONE then all revisions up to the end_rev_id are logged.
1013
1014
    :param end_rev_id: If not NONE specifies the last revision to be logged.
1015
            If NONE then all revisions up to the end of the log are logged.
1016
1017
    :return: The filtered view_revisions.
1018
    """
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
1019
    # This method is no longer called by the main code path.
1020
    # It may be removed soon. IGC 20090127
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1021
    if start_rev_id or end_rev_id:
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1022
        revision_ids = [r for r, n, d in view_revisions]
1023
        if start_rev_id:
1024
            start_index = revision_ids.index(start_rev_id)
1025
        else:
1026
            start_index = 0
1027
        if start_rev_id == end_rev_id:
1028
            end_index = start_index
1029
        else:
1030
            if end_rev_id:
1031
                end_index = revision_ids.index(end_rev_id)
1032
            else:
1033
                end_index = len(view_revisions) - 1
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1034
        # To include the revisions merged into the last revision,
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1035
        # extend end_rev_id down to, but not including, the next rev
1036
        # with the same or lesser merge_depth
1037
        end_merge_depth = view_revisions[end_index][2]
1038
        try:
1039
            for index in xrange(end_index+1, len(view_revisions)+1):
1040
                if view_revisions[index][2] <= end_merge_depth:
1041
                    end_index = index - 1
1042
                    break
1043
        except IndexError:
1044
            # if the search falls off the end then log to the end as well
1045
            end_index = len(view_revisions) - 1
1046
        view_revisions = view_revisions[start_index:end_index+1]
1047
    return view_revisions
1048
1049
3940.1.3 by Ian Clatworthy
fix code
1050
def _filter_revisions_touching_file_id(branch, file_id, view_revisions,
1051
    include_merges=True):
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1052
    r"""Return the list of revision ids which touch a given file id.
2359.1.4 by John Arbash Meinel
Refactor the specific revisions for file id into a helper function.
1053
2466.12.1 by Kent Gibson
Fix ``bzr log -r`` to support selecting merge revisions.
1054
    The function filters view_revisions and returns a subset.
2359.1.4 by John Arbash Meinel
Refactor the specific revisions for file id into a helper function.
1055
    This includes the revisions which directly change the file id,
1056
    and the revisions which merge these changes. So if the
1057
    revision graph is::
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1058
        A-.
1059
        |\ \
1060
        B C E
1061
        |/ /
1062
        D |
1063
        |\|
1064
        | F
2359.1.4 by John Arbash Meinel
Refactor the specific revisions for file id into a helper function.
1065
        |/
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1066
        G
1067
1068
    And 'C' changes a file, then both C and D will be returned. F will not be
1069
    returned even though it brings the changes to C into the branch starting
1070
    with E. (Note that if we were using F as the tip instead of G, then we
1071
    would see C, D, F.)
1072
1073
    This will also be restricted based on a subset of the mainline.
1074
1075
    :param branch: The branch where we can get text revision information.
3842.2.5 by Vincent Ladeuil
Better fix for bug #300055.
1076
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1077
    :param file_id: Filter out revisions that do not touch file_id.
3842.2.5 by Vincent Ladeuil
Better fix for bug #300055.
1078
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1079
    :param view_revisions: A list of (revision_id, dotted_revno, merge_depth)
1080
        tuples. This is the list of revisions which will be filtered. It is
3842.2.5 by Vincent Ladeuil
Better fix for bug #300055.
1081
        assumed that view_revisions is in merge_sort order (i.e. newest
1082
        revision first ).
1083
3940.1.3 by Ian Clatworthy
fix code
1084
    :param include_merges: include merge revisions in the result or not
1085
2359.1.8 by John Arbash Meinel
doc
1086
    :return: A list of (revision_id, dotted_revno, merge_depth) tuples.
2359.1.4 by John Arbash Meinel
Refactor the specific revisions for file id into a helper function.
1087
    """
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1088
    # Lookup all possible text keys to determine which ones actually modified
1089
    # the file.
1090
    text_keys = [(file_id, rev_id) for rev_id, revno, depth in view_revisions]
4183.3.1 by Vincent Ladeuil
Fix bug #346431 by allowing log._filter_revisions_touching_file_id to be
1091
    next_keys = None
3711.3.16 by John Arbash Meinel
Doc update.
1092
    # Looking up keys in batches of 1000 can cut the time in half, as well as
1093
    # memory consumption. GraphIndex *does* like to look for a few keys in
1094
    # parallel, it just doesn't like looking for *lots* of keys in parallel.
3711.3.19 by John Arbash Meinel
Add a TODO discussing how our index requests should evolve.
1095
    # TODO: This code needs to be re-evaluated periodically as we tune the
1096
    #       indexing layer. We might consider passing in hints as to the known
1097
    #       access pattern (sparse/clustered, high success rate/low success
1098
    #       rate). This particular access is clustered with a low success rate.
3711.3.15 by John Arbash Meinel
Work around GraphIndex inefficiencies by requesting keys 1000 at a time.
1099
    get_parent_map = branch.repository.texts.get_parent_map
1100
    modified_text_revisions = set()
1101
    chunk_size = 1000
1102
    for start in xrange(0, len(text_keys), chunk_size):
1103
        next_keys = text_keys[start:start + chunk_size]
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1104
        # Only keep the revision_id portion of the key
3711.3.15 by John Arbash Meinel
Work around GraphIndex inefficiencies by requesting keys 1000 at a time.
1105
        modified_text_revisions.update(
1106
            [k[1] for k in get_parent_map(next_keys)])
1107
    del text_keys, next_keys
3711.3.14 by John Arbash Meinel
Change the per-file log algorithm dramatically.
1108
1109
    result = []
1110
    # Track what revisions will merge the current revision, replace entries
1111
    # with 'None' when they have been added to result
1112
    current_merge_stack = [None]
3711.3.23 by John Arbash Meinel
Documentation and cleanup.
1113
    for info in view_revisions:
3711.3.14 by John Arbash Meinel
Change the per-file log algorithm dramatically.
1114
        rev_id, revno, depth = info
1115
        if depth == len(current_merge_stack):
1116
            current_merge_stack.append(info)
1117
        else:
1118
            del current_merge_stack[depth + 1:]
1119
            current_merge_stack[-1] = info
1120
1121
        if rev_id in modified_text_revisions:
1122
            # This needs to be logged, along with the extra revisions
1123
            for idx in xrange(len(current_merge_stack)):
1124
                node = current_merge_stack[idx]
1125
                if node is not None:
3940.1.3 by Ian Clatworthy
fix code
1126
                    if include_merges or node[2] == 0:
1127
                        result.append(node)
1128
                        current_merge_stack[idx] = None
3711.3.4 by John Arbash Meinel
Significantly faster, but consuming more memory.
1129
    return result
2359.1.4 by John Arbash Meinel
Refactor the specific revisions for file id into a helper function.
1130
1131
1756.2.20 by Aaron Bentley
Optimize log formats that don't show merges
1132
def get_view_revisions(mainline_revs, rev_nos, branch, direction,
1756.2.22 by Aaron Bentley
Apply review comments
1133
                       include_merges=True):
1756.2.18 by Aaron Bentley
Factor out the revision list generation
1134
    """Produce an iterator of revisions to show
1135
    :return: an iterator of (revision_id, revno, merge_depth)
1136
    (if there is no revno for a revision, None is supplied)
1137
    """
3936.3.30 by Ian Clatworthy
use iter_merge_sorted_revisions() with stop_range feature
1138
    # This method is no longer called by the main code path.
1139
    # It is retained for API compatibility and may be deprecated
1140
    # soon. IGC 20090127
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1141
    if not include_merges:
1756.2.20 by Aaron Bentley
Optimize log formats that don't show merges
1142
        revision_ids = mainline_revs[1:]
1143
        if direction == 'reverse':
1144
            revision_ids.reverse()
1145
        for revision_id in revision_ids:
1988.4.2 by Robert Collins
``bzr log`` Now shows dotted-decimal revision numbers for all revisions,
1146
            yield revision_id, str(rev_nos[revision_id]), 0
1756.2.20 by Aaron Bentley
Optimize log formats that don't show merges
1147
        return
3287.6.1 by Robert Collins
* ``VersionedFile.get_graph`` is deprecated, with no replacement method.
1148
    graph = branch.repository.get_graph()
1149
    # This asks for all mainline revisions, which means we only have to spider
1150
    # sideways, rather than depth history. That said, its still size-of-history
1151
    # and should be addressed.
3373.5.4 by John Arbash Meinel
Track down another bogus location. Only triggered with --long
1152
    # mainline_revisions always includes an extra revision at the beginning, so
1153
    # don't request it.
3287.6.8 by Robert Collins
Reduce code duplication as per review.
1154
    parent_map = dict(((key, value) for key, value in
3373.5.4 by John Arbash Meinel
Track down another bogus location. Only triggered with --long
1155
        graph.iter_ancestry(mainline_revs[1:]) if value is not None))
3287.6.1 by Robert Collins
* ``VersionedFile.get_graph`` is deprecated, with no replacement method.
1156
    # filter out ghosts; merge_sort errors on ghosts.
3535.5.1 by John Arbash Meinel
cleanup a few imports to be lazily loaded.
1157
    rev_graph = _mod_repository._strip_NULL_ghosts(parent_map)
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
1158
    merge_sorted_revisions = tsort.merge_sort(
3287.6.1 by Robert Collins
* ``VersionedFile.get_graph`` is deprecated, with no replacement method.
1159
        rev_graph,
1756.2.18 by Aaron Bentley
Factor out the revision list generation
1160
        mainline_revs[-1],
1988.4.2 by Robert Collins
``bzr log`` Now shows dotted-decimal revision numbers for all revisions,
1161
        mainline_revs,
1162
        generate_revno=True)
1756.2.18 by Aaron Bentley
Factor out the revision list generation
1163
1164
    if direction == 'forward':
1165
        # forward means oldest first.
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1166
        merge_sorted_revisions = reverse_by_depth(merge_sorted_revisions)
1756.2.18 by Aaron Bentley
Factor out the revision list generation
1167
    elif direction != 'reverse':
1168
        raise ValueError('invalid direction %r' % direction)
1169
3874.2.4 by Vincent Ladeuil
Fix too long lines.
1170
    for (sequence, rev_id, merge_depth, revno, end_of_merge
1171
         ) in merge_sorted_revisions:
1988.4.2 by Robert Collins
``bzr log`` Now shows dotted-decimal revision numbers for all revisions,
1172
        yield rev_id, '.'.join(map(str, revno)), merge_depth
1756.2.18 by Aaron Bentley
Factor out the revision list generation
1173
1174
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1175
def reverse_by_depth(merge_sorted_revisions, _depth=0):
1176
    """Reverse revisions by depth.
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1177
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1178
    Revisions with a different depth are sorted as a group with the previous
1179
    revision of that depth.  There may be no topological justification for this,
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1180
    but it looks much nicer.
1181
    """
3842.2.6 by Vincent Ladeuil
Fix typo.
1182
    # Add a fake revision at start so that we can always attach sub revisions
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1183
    merge_sorted_revisions = [(None, None, _depth)] + merge_sorted_revisions
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1184
    zd_revisions = []
1185
    for val in merge_sorted_revisions:
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1186
        if val[2] == _depth:
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1187
            # Each revision at the current depth becomes a chunk grouping all
1188
            # higher depth revisions.
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1189
            zd_revisions.append([val])
1190
        else:
1191
            zd_revisions[-1].append(val)
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1192
    for revisions in zd_revisions:
1193
        if len(revisions) > 1:
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1194
            # We have higher depth revisions, let reverse them locally
1756.2.25 by Aaron Bentley
Sort revisions at each depth, instead of just mainline revisions.
1195
            revisions[1:] = reverse_by_depth(revisions[1:], _depth + 1)
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1196
    zd_revisions.reverse()
1197
    result = []
1198
    for chunk in zd_revisions:
1199
        result.extend(chunk)
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1200
    if _depth == 0:
1201
        # Top level call, get rid of the fake revisions that have been added
1202
        result = [r for r in result if r[0] is not None and r[1] is not None]
1756.2.24 by Aaron Bentley
Forward sorting shows merges under mainline revision
1203
    return result
1204
1205
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1206
class LogRevision(object):
1207
    """A revision to be logged (by LogFormatter.log_revision).
1208
1209
    A simple wrapper for the attributes of a revision to be logged.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1210
    The attributes may or may not be populated, as determined by the
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1211
    logging options and the log formatter capabilities.
1212
    """
1213
2490.1.2 by John Arbash Meinel
Cleanup according to PEP8 and some other small whitespace fixes
1214
    def __init__(self, rev=None, revno=None, merge_depth=0, delta=None,
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1215
                 tags=None, diff=None):
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1216
        self.rev = rev
3936.3.39 by Ian Clatworthy
merge bzr.dev r3975
1217
        self.revno = str(revno)
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1218
        self.merge_depth = merge_depth
1219
        self.delta = delta
1220
        self.tags = tags
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1221
        self.diff = diff
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1222
1223
794 by Martin Pool
- Merge John's nice short-log format.
1224
class LogFormatter(object):
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1225
    """Abstract class to display log messages.
1226
1227
    At a minimum, a derived class must implement the log_revision method.
1228
1229
    If the LogFormatter needs to be informed of the beginning or end of
1230
    a log it should implement the begin_log and/or end_log hook methods.
1231
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1232
    A LogFormatter should define the following supports_XXX flags
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1233
    to indicate which LogRevision attributes it supports:
1234
1235
    - supports_delta must be True if this log formatter supports delta.
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1236
        Otherwise the delta attribute may not be populated.  The 'delta_format'
1237
        attribute describes whether the 'short_status' format (1) or the long
3936.3.2 by Ian Clatworthy
minor cleanups
1238
        one (2) should be used.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1239
1240
    - supports_merge_revisions must be True if this log formatter supports
3936.3.2 by Ian Clatworthy
minor cleanups
1241
        merge revisions.  If not, and if supports_single_merge_revision is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1242
        also not True, then only mainline revisions will be passed to the
2997.1.1 by Kent Gibson
Support logging single merge revisions with short and line log formatters.
1243
        formatter.
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1244
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1245
    - preferred_levels is the number of levels this formatter defaults to.
1246
        The default value is zero meaning display all levels.
1247
        This value is only relevant if supports_merge_revisions is True.
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1248
2997.1.1 by Kent Gibson
Support logging single merge revisions with short and line log formatters.
1249
    - supports_single_merge_revision must be True if this log formatter
1250
        supports logging only a single merge revision.  This flag is
1251
        only relevant if supports_merge_revisions is not True.
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1252
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1253
    - supports_tags must be True if this log formatter supports tags.
1254
        Otherwise the tags attribute may not be populated.
3144.7.1 by Guillermo Gonzalez
* added show_properties to LonLogFormat and the hooks to register custom functions
1255
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1256
    - supports_diff must be True if this log formatter supports diffs.
1257
        Otherwise the diff attribute may not be populated.
1258
3144.7.1 by Guillermo Gonzalez
* added show_properties to LonLogFormat and the hooks to register custom functions
1259
    Plugins can register functions to show custom revision properties using
3144.7.13 by Guillermo Gonzalez
* fixed typo LogFormatter.show_properties in docstring
1260
    the properties_handler_registry. The registered function
3144.7.1 by Guillermo Gonzalez
* added show_properties to LonLogFormat and the hooks to register custom functions
1261
    must respect the following interface description:
3144.7.2 by Guillermo Gonzalez
* cleanup a bit the interface
1262
        def my_show_properties(properties_dict):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1263
            # code that returns a dict {'name':'value'} of the properties
3144.7.2 by Guillermo Gonzalez
* cleanup a bit the interface
1264
            # to be shown
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1265
    """
3947.1.10 by Ian Clatworthy
review feedback from vila
1266
    preferred_levels = 0
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1267
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1268
    def __init__(self, to_file, show_ids=False, show_timezone='original',
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1269
                 delta_format=None, levels=None):
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1270
        """Create a LogFormatter.
1271
1272
        :param to_file: the file to output to
1273
        :param show_ids: if True, revision-ids are to be displayed
1274
        :param show_timezone: the timezone to use
1275
        :param delta_format: the level of delta information to display
1276
          or None to leave it u to the formatter to decide
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1277
        :param levels: the number of levels to display; None or -1 to
1278
          let the log formatter decide.
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1279
        """
794 by Martin Pool
- Merge John's nice short-log format.
1280
        self.to_file = to_file
4110.1.1 by Alexander Belchenko
Fixed problem with `log -p` and non-ascii content of files: show_diff should write the diff to exact [stdout] stream.
1281
        # 'exact' stream used to show diff, it should print content 'as is'
1282
        # and should not try to decode/encode it to unicode to avoid bug #328007
1283
        self.to_exact_file = getattr(to_file, 'stream', to_file)
794 by Martin Pool
- Merge John's nice short-log format.
1284
        self.show_ids = show_ids
1285
        self.show_timezone = show_timezone
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1286
        if delta_format is None:
1287
            # Ensures backward compatibility
1288
            delta_format = 2 # long format
1289
        self.delta_format = delta_format
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1290
        self.levels = levels
1291
3947.1.10 by Ian Clatworthy
review feedback from vila
1292
    def get_levels(self):
1293
        """Get the number of levels to display or 0 for all."""
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1294
        if getattr(self, 'supports_merge_revisions', False):
1295
            if self.levels is None or self.levels == -1:
3947.1.10 by Ian Clatworthy
review feedback from vila
1296
                return self.preferred_levels
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1297
            else:
1298
                return self.levels
1299
        return 1
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1300
3947.1.10 by Ian Clatworthy
review feedback from vila
1301
    def log_revision(self, revision):
1302
        """Log a revision.
1303
1304
        :param  revision:   The LogRevision to be logged.
1305
        """
1306
        raise NotImplementedError('not implemented in abstract base')
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1307
1185.35.19 by Aaron Bentley
Tweaked short-log as Meinel suggested
1308
    def short_committer(self, rev):
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
1309
        name, address = config.parse_username(rev.committer)
1310
        if name:
3063.3.1 by Lukáš Lalinský
Fall back to showing e-mail in ``log --short/--line`` if the committer/author has only e-mail.
1311
            return name
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
1312
        return address
2388.1.11 by Alexander Belchenko
changes after John's review
1313
2671.5.4 by Lukáš Lalinsky
Replace the committer with the author in log, the committer is displayed only in the long format and only if it's different from the author.
1314
    def short_author(self, rev):
4056.2.1 by James Westby
Allow specifying multiple authors for a revision.
1315
        name, address = config.parse_username(rev.get_apparent_authors()[0])
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
1316
        if name:
3063.3.1 by Lukáš Lalinský
Fall back to showing e-mail in ``log --short/--line`` if the committer/author has only e-mail.
1317
            return name
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
1318
        return address
2671.5.4 by Lukáš Lalinsky
Replace the committer with the author in log, the committer is displayed only in the long format and only if it's different from the author.
1319
3144.7.9 by Guillermo Gonzalez
* bzrlib.log.show_roperties don't hide handler errors
1320
    def show_properties(self, revision, indent):
3144.7.8 by Guillermo Gonzalez
* added error handling (and logging) to LogFormatter.show_properties when a handler raise an error
1321
        """Displays the custom properties returned by each registered handler.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1322
3144.7.13 by Guillermo Gonzalez
* fixed typo LogFormatter.show_properties in docstring
1323
        If a registered handler raises an error it is propagated.
3144.7.5 by Guillermo Gonzalez
* some improvements to the doctstring in show_properties method and in LogFormatter
1324
        """
3144.7.9 by Guillermo Gonzalez
* bzrlib.log.show_roperties don't hide handler errors
1325
        for key, handler in properties_handler_registry.iteritems():
1326
            for key, value in handler(revision).items():
1327
                self.to_file.write(indent + key + ': ' + value + '\n')
3144.7.8 by Guillermo Gonzalez
* added error handling (and logging) to LogFormatter.show_properties when a handler raise an error
1328
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1329
    def show_diff(self, to_file, diff, indent):
1330
        for l in diff.rstrip().split('\n'):
1331
            to_file.write(indent + '%s\n' % (l,))
1332
2388.1.11 by Alexander Belchenko
changes after John's review
1333
794 by Martin Pool
- Merge John's nice short-log format.
1334
class LongLogFormatter(LogFormatter):
2388.1.11 by Alexander Belchenko
changes after John's review
1335
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1336
    supports_merge_revisions = True
1337
    supports_delta = True
1338
    supports_tags = True
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1339
    supports_diff = True
2388.1.10 by Alexander Belchenko
Slightly reworked: use None instead of [] as default tags list; PEP-8
1340
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1341
    def log_revision(self, revision):
1342
        """Log a revision, either merged or not."""
2671.2.5 by Lukáš Lalinský
Fixes for comments from the mailing list.
1343
        indent = '    ' * revision.merge_depth
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
1344
        to_file = self.to_file
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1345
        to_file.write(indent + '-' * 60 + '\n')
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1346
        if revision.revno is not None:
2911.6.3 by Blake Winton
Implemented suggestions from John Arbash Meinel.
1347
            to_file.write(indent + 'revno: %s\n' % (revision.revno,))
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1348
        if revision.tags:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1349
            to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
1350
        if self.show_ids:
3257.2.1 by Adeodato Simó
Add a space after "revision-id:" in log output.
1351
            to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1352
            to_file.write('\n')
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1353
            for parent_id in revision.rev.parent_ids:
2911.6.3 by Blake Winton
Implemented suggestions from John Arbash Meinel.
1354
                to_file.write(indent + 'parent: %s\n' % (parent_id,))
3144.7.11 by Guillermo Gonzalez
* updates LongLogFormatter to pass revision instead of the properties dict to show_properties method
1355
        self.show_properties(revision.rev, indent)
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
1356
4056.2.3 by James Westby
Use a new "authors" revision property to allow multiple authors
1357
        committer = revision.rev.committer
1358
        authors = revision.rev.get_apparent_authors()
1359
        if authors != [committer]:
1360
            to_file.write(indent + 'author: %s\n' % (", ".join(authors),))
1361
        to_file.write(indent + 'committer: %s\n' % (committer,))
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
1362
1363
        branch_nick = revision.rev.properties.get('branch-nick', None)
1364
        if branch_nick is not None:
2911.6.3 by Blake Winton
Implemented suggestions from John Arbash Meinel.
1365
            to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
1366
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1367
        date_str = format_date(revision.rev.timestamp,
1368
                               revision.rev.timezone or 0,
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
1369
                               self.show_timezone)
2911.6.3 by Blake Winton
Implemented suggestions from John Arbash Meinel.
1370
        to_file.write(indent + 'timestamp: %s\n' % (date_str,))
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
1371
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1372
        to_file.write(indent + 'message:\n')
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1373
        if not revision.rev.message:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1374
            to_file.write(indent + '  (no message)\n')
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
1375
        else:
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1376
            message = revision.rev.message.rstrip('\r\n')
1185.31.20 by John Arbash Meinel
Stripping trailing newlines when displaying log messages
1377
            for l in message.split('\n'):
3943.5.3 by Ian Clatworthy
add tests
1378
                to_file.write(indent + '  %s\n' % (l,))
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1379
        if revision.delta is not None:
3874.1.7 by Vincent Ladeuil
Restrict '-v' change to log --short only.
1380
            # We don't respect delta_format for compatibility
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1381
            revision.delta.show(to_file, self.show_ids, indent=indent,
3874.1.7 by Vincent Ladeuil
Restrict '-v' change to log --short only.
1382
                                short_status=False)
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1383
        if revision.diff is not None:
1384
            to_file.write(indent + 'diff:\n')
3943.5.6 by Ian Clatworthy
feedback from jam's review
1385
            # Note: we explicitly don't indent the diff (relative to the
1386
            # revision information) so that the output can be fed to patch -p0
4110.1.1 by Alexander Belchenko
Fixed problem with `log -p` and non-ascii content of files: show_diff should write the diff to exact [stdout] stream.
1387
            self.show_diff(self.to_exact_file, revision.diff, indent)
794 by Martin Pool
- Merge John's nice short-log format.
1388
1389
1390
class ShortLogFormatter(LogFormatter):
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1391
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1392
    supports_merge_revisions = True
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1393
    preferred_levels = 1
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1394
    supports_delta = True
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1395
    supports_tags = True
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1396
    supports_diff = True
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1397
3947.1.9 by Ian Clatworthy
get offset right when dotted-revno in column 1
1398
    def __init__(self, *args, **kwargs):
1399
        super(ShortLogFormatter, self).__init__(*args, **kwargs)
1400
        self.revno_width_by_depth = {}
1401
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1402
    def log_revision(self, revision):
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1403
        # We need two indents: one per depth and one for the information
1404
        # relative to that indent. Most mainline revnos are 5 chars or
3970.1.1 by Ian Clatworthy
log -n/--levels (Ian Clatworthy)
1405
        # less while dotted revnos are typically 11 chars or less. Once
3947.1.9 by Ian Clatworthy
get offset right when dotted-revno in column 1
1406
        # calculated, we need to remember the offset for a given depth
1407
        # as we might be starting from a dotted revno in the first column
1408
        # and we want subsequent mainline revisions to line up.
1409
        depth = revision.merge_depth
1410
        indent = '    ' * depth
1411
        revno_width = self.revno_width_by_depth.get(depth)
1412
        if revno_width is None:
1413
            if revision.revno.find('.') == -1:
3947.1.10 by Ian Clatworthy
review feedback from vila
1414
                # mainline revno, e.g. 12345
3947.1.9 by Ian Clatworthy
get offset right when dotted-revno in column 1
1415
                revno_width = 5
1416
            else:
3947.1.10 by Ian Clatworthy
review feedback from vila
1417
                # dotted revno, e.g. 12345.10.55
1418
                revno_width = 11
3947.1.9 by Ian Clatworthy
get offset right when dotted-revno in column 1
1419
            self.revno_width_by_depth[depth] = revno_width
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1420
        offset = ' ' * (revno_width + 1)
1421
794 by Martin Pool
- Merge John's nice short-log format.
1422
        to_file = self.to_file
2483.2.2 by John Arbash Meinel
Add [merge] after the timestamp for revisions with merges.
1423
        is_merge = ''
2483.2.5 by John Arbash Meinel
[merge] bzr.dev 2501
1424
        if len(revision.rev.parent_ids) > 1:
2483.2.2 by John Arbash Meinel
Add [merge] after the timestamp for revisions with merges.
1425
            is_merge = ' [merge]'
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1426
        tags = ''
1427
        if revision.tags:
3946.3.2 by Ian Clatworthy
add tests & NEWS item
1428
            tags = ' {%s}' % (', '.join(revision.tags))
3947.1.8 by Ian Clatworthy
merge bzr.dev r3954
1429
        to_file.write(indent + "%*s %s\t%s%s%s\n" % (revno_width,
1430
                revision.revno, self.short_author(revision.rev),
2483.2.5 by John Arbash Meinel
[merge] bzr.dev 2501
1431
                format_date(revision.rev.timestamp,
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1432
                            revision.rev.timezone or 0,
1185.35.19 by Aaron Bentley
Tweaked short-log as Meinel suggested
1433
                            self.show_timezone, date_fmt="%Y-%m-%d",
2483.2.5 by John Arbash Meinel
[merge] bzr.dev 2501
1434
                            show_offset=False),
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1435
                tags, is_merge))
3976.3.1 by Neil Martinsen-Burrell
Add custom properties handling to short log format
1436
        self.show_properties(revision.rev, indent+offset)
794 by Martin Pool
- Merge John's nice short-log format.
1437
        if self.show_ids:
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1438
            to_file.write(indent + offset + 'revision-id:%s\n'
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1439
                          % (revision.rev.revision_id,))
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1440
        if not revision.rev.message:
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1441
            to_file.write(indent + offset + '(no message)\n')
794 by Martin Pool
- Merge John's nice short-log format.
1442
        else:
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1443
            message = revision.rev.message.rstrip('\r\n')
1185.31.20 by John Arbash Meinel
Stripping trailing newlines when displaying log messages
1444
            for l in message.split('\n'):
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1445
                to_file.write(indent + offset + '%s\n' % (l,))
794 by Martin Pool
- Merge John's nice short-log format.
1446
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1447
        if revision.delta is not None:
3947.1.7 by Ian Clatworthy
tweak indenting/offsetting for --short given dotted revno lengths
1448
            revision.delta.show(to_file, self.show_ids, indent=indent + offset,
3874.1.4 by Vincent Ladeuil
Fixed as per Aarons' comment.
1449
                                short_status=self.delta_format==1)
3943.5.2 by Ian Clatworthy
hand control of diff formatting to the log formatter
1450
        if revision.diff is not None:
4110.1.1 by Alexander Belchenko
Fixed problem with `log -p` and non-ascii content of files: show_diff should write the diff to exact [stdout] stream.
1451
            self.show_diff(self.to_exact_file, revision.diff, '      ')
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1452
        to_file.write('\n')
794 by Martin Pool
- Merge John's nice short-log format.
1453
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1454
1185.12.25 by Aaron Bentley
Added one-line log format
1455
class LineLogFormatter(LogFormatter):
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1456
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1457
    supports_merge_revisions = True
3947.1.6 by Ian Clatworthy
log -n/--level-count N option
1458
    preferred_levels = 1
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1459
    supports_tags = True
2997.1.1 by Kent Gibson
Support logging single merge revisions with short and line log formatters.
1460
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1461
    def __init__(self, *args, **kwargs):
1462
        super(LineLogFormatter, self).__init__(*args, **kwargs)
1463
        self._max_chars = terminal_width() - 1
1464
1185.12.25 by Aaron Bentley
Added one-line log format
1465
    def truncate(self, str, max_len):
1466
        if len(str) <= max_len:
1467
            return str
1468
        return str[:max_len-3]+'...'
1469
1470
    def date_string(self, rev):
3842.2.4 by Vincent Ladeuil
Superficial fix for bug #300055.
1471
        return format_date(rev.timestamp, rev.timezone or 0,
1185.12.25 by Aaron Bentley
Added one-line log format
1472
                           self.show_timezone, date_fmt="%Y-%m-%d",
1473
                           show_offset=False)
1474
1475
    def message(self, rev):
1476
        if not rev.message:
1477
            return '(no message)'
1478
        else:
1479
            return rev.message
1480
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1481
    def log_revision(self, revision):
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1482
        indent = '  ' * revision.merge_depth
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1483
        self.to_file.write(self.log_string(revision.revno, revision.rev,
3947.1.8 by Ian Clatworthy
merge bzr.dev r3954
1484
            self._max_chars, revision.tags, indent))
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
1485
        self.to_file.write('\n')
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
1486
3947.1.8 by Ian Clatworthy
merge bzr.dev r3954
1487
    def log_string(self, revno, rev, max_chars, tags=None, prefix=''):
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1488
        """Format log info into one string. Truncate tail of string
3677.1.1 by Vincent Ladeuil
Begin fixing bug #233817.
1489
        :param  revno:      revision number or None.
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1490
                            Revision numbers counts from 1.
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1491
        :param  rev:        revision object
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1492
        :param  max_chars:  maximum length of resulting string
3946.3.1 by Ian Clatworthy
extend ShortLogFormatter & LineLogFormatter to support tags
1493
        :param  tags:       list of tags or None
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1494
        :param  prefix:     string to prefix each line
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1495
        :return:            formatted truncated string
1496
        """
1497
        out = []
1498
        if revno:
1499
            # show revno only when is not None
3946.3.4 by Ian Clatworthy
minor cleanup
1500
            out.append("%s:" % revno)
2671.5.4 by Lukáš Lalinsky
Replace the committer with the author in log, the committer is displayed only in the long format and only if it's different from the author.
1501
        out.append(self.truncate(self.short_author(rev), 20))
1185.12.25 by Aaron Bentley
Added one-line log format
1502
        out.append(self.date_string(rev))
3983.2.1 by Neil Martinsen-Burrell
add merge indication to the line format
1503
        if len(rev.parent_ids) > 1:
1504
            out.append('[merge]')
3946.3.3 by Ian Clatworthy
feedback from jelmer re position of tags in --line
1505
        if tags:
1506
            tag_str = '{%s}' % (', '.join(tags))
1507
            out.append(tag_str)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
1508
        out.append(rev.get_summary())
3947.1.1 by Ian Clatworthy
add --merge-revisions to log
1509
        return self.truncate(prefix + " ".join(out).rstrip('\n'), max_chars)
794 by Martin Pool
- Merge John's nice short-log format.
1510
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1511
4129.1.1 by Andrea Bolognani
Renamed the ChangeLogLogFormatter class to GnuChangelogLogFormatter.
1512
class GnuChangelogLogFormatter(LogFormatter):
4070.4.1 by Andrea Bolognani
New GNU Changelog log format
1513
1514
    supports_merge_revisions = True
1515
    supports_delta = True
1516
1517
    def log_revision(self, revision):
1518
        """Log a revision, either merged or not."""
1519
        to_file = self.to_file
1520
1521
        date_str = format_date(revision.rev.timestamp,
1522
                               revision.rev.timezone or 0,
1523
                               self.show_timezone,
1524
                               date_fmt='%Y-%m-%d',
1525
                               show_offset=False)
1526
        committer_str = revision.rev.committer.replace (' <', '  <')
1527
        to_file.write('%s  %s\n\n' % (date_str,committer_str))
1528
4137.1.1 by James Westby
Small improvements to the GNU ChangeLog formatter.
1529
        if revision.delta is not None and revision.delta.has_changed():
4070.4.1 by Andrea Bolognani
New GNU Changelog log format
1530
            for c in revision.delta.added + revision.delta.removed + revision.delta.modified:
1531
                path, = c[:1]
1532
                to_file.write('\t* %s:\n' % (path,))
1533
            for c in revision.delta.renamed:
1534
                oldpath,newpath = c[:2]
1535
                # For renamed files, show both the old and the new path
1536
                to_file.write('\t* %s:\n\t* %s:\n' % (oldpath,newpath))
1537
            to_file.write('\n')
1538
1539
        if not revision.rev.message:
1540
            to_file.write('\tNo commit message\n')
1541
        else:
1542
            message = revision.rev.message.rstrip('\r\n')
1543
            for l in message.split('\n'):
1544
                to_file.write('\t%s\n' % (l.lstrip(),))
1545
            to_file.write('\n')
1546
1547
1185.12.27 by Aaron Bentley
Use line log for pending merges
1548
def line_log(rev, max_chars):
1549
    lf = LineLogFormatter(None)
1704.2.20 by Martin Pool
log --line shows revision numbers (Alexander)
1550
    return lf.log_string(None, rev, max_chars)
1185.12.27 by Aaron Bentley
Use line log for pending merges
1551
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
1552
1553
class LogFormatterRegistry(registry.Registry):
1554
    """Registry for log formatters"""
1555
1556
    def make_formatter(self, name, *args, **kwargs):
1557
        """Construct a formatter from arguments.
1558
1559
        :param name: Name of the formatter to construct.  'short', 'long' and
1560
            'line' are built-in.
1561
        """
1562
        return self.get(name)(*args, **kwargs)
1563
1564
    def get_default(self, branch):
1565
        return self.get(branch.get_config().log_format())
1566
1567
1568
log_formatter_registry = LogFormatterRegistry()
1569
1570
1571
log_formatter_registry.register('short', ShortLogFormatter,
1572
                                'Moderately short log format')
1573
log_formatter_registry.register('long', LongLogFormatter,
1574
                                'Detailed log format')
1575
log_formatter_registry.register('line', LineLogFormatter,
1576
                                'Log format with one line per revision')
4129.1.1 by Andrea Bolognani
Renamed the ChangeLogLogFormatter class to GnuChangelogLogFormatter.
1577
log_formatter_registry.register('gnu-changelog', GnuChangelogLogFormatter,
1578
                                'Format used by GNU ChangeLog files')
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
1579
794 by Martin Pool
- Merge John's nice short-log format.
1580
1553.2.1 by Erik Bågfors
Support for plugins to register log formatters and set default formatter
1581
def register_formatter(name, formatter):
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
1582
    log_formatter_registry.register(name, formatter)
1583
1553.2.1 by Erik Bågfors
Support for plugins to register log formatters and set default formatter
1584
794 by Martin Pool
- Merge John's nice short-log format.
1585
def log_formatter(name, *args, **kwargs):
1393.1.56 by Martin Pool
- doc and small refactoring of log code
1586
    """Construct a formatter from arguments.
1587
1185.12.27 by Aaron Bentley
Use line log for pending merges
1588
    name -- Name of the formatter to construct; currently 'long', 'short' and
1589
        'line' are supported.
1393.1.56 by Martin Pool
- doc and small refactoring of log code
1590
    """
794 by Martin Pool
- Merge John's nice short-log format.
1591
    try:
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
1592
        return log_formatter_registry.make_formatter(name, *args, **kwargs)
1553.2.2 by Erik Bågfors
Made "unknown log formatter" error message work
1593
    except KeyError:
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
1594
        raise errors.BzrCommandError("unknown log formatter: %r" % name)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1595
2221.4.10 by Aaron Bentley
Implement log options using RegistryOption
1596
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1597
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1598
    # deprecated; for compatibility
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1599
    lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
1600
    lf.show(revno, rev, delta)
1185.32.2 by John Arbash Meinel
Refactor pull --verbose into a log.py function, add tests.
1601
2490.1.4 by John Arbash Meinel
Update bzrlib.log.show_changed_revisions to use the new api
1602
1551.17.2 by Aaron Bentley
Stop showing deltas in pull -v output
1603
def show_changed_revisions(branch, old_rh, new_rh, to_file=None,
1604
                           log_format='long'):
1185.32.2 by John Arbash Meinel
Refactor pull --verbose into a log.py function, add tests.
1605
    """Show the change in revision history comparing the old revision history to the new one.
1606
1607
    :param branch: The branch where the revisions exist
1608
    :param old_rh: The old revision history
1609
    :param new_rh: The new revision history
1610
    :param to_file: A file to write the results to. If None, stdout will be used
1611
    """
1612
    if to_file is None:
2997.1.3 by Alexander Belchenko
file wrapper around stdout should use terminal encoding, not user_encoding.
1613
        to_file = codecs.getwriter(get_terminal_encoding())(sys.stdout,
1614
            errors='replace')
1185.32.2 by John Arbash Meinel
Refactor pull --verbose into a log.py function, add tests.
1615
    lf = log_formatter(log_format,
1616
                       show_ids=False,
1617
                       to_file=to_file,
1618
                       show_timezone='original')
1619
1620
    # This is the first index which is different between
1621
    # old and new
1622
    base_idx = None
1623
    for i in xrange(max(len(new_rh),
1624
                        len(old_rh))):
1625
        if (len(new_rh) <= i
1626
            or len(old_rh) <= i
1627
            or new_rh[i] != old_rh[i]):
1628
            base_idx = i
1629
            break
1630
1631
    if base_idx is None:
1632
        to_file.write('Nothing seems to have changed\n')
1633
        return
1634
    ## TODO: It might be nice to do something like show_log
1635
    ##       and show the merged entries. But since this is the
1636
    ##       removed revisions, it shouldn't be as important
1637
    if base_idx < len(old_rh):
1638
        to_file.write('*'*60)
1639
        to_file.write('\nRemoved Revisions:\n')
1640
        for i in range(base_idx, len(old_rh)):
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
1641
            rev = branch.repository.get_revision(old_rh[i])
2490.1.4 by John Arbash Meinel
Update bzrlib.log.show_changed_revisions to use the new api
1642
            lr = LogRevision(rev, i+1, 0, None)
1643
            lf.log_revision(lr)
1185.32.2 by John Arbash Meinel
Refactor pull --verbose into a log.py function, add tests.
1644
        to_file.write('*'*60)
1645
        to_file.write('\n\n')
1646
    if base_idx < len(new_rh):
1647
        to_file.write('Added Revisions:\n')
1648
        show_log(branch,
1649
                 lf,
1650
                 None,
1551.17.2 by Aaron Bentley
Stop showing deltas in pull -v output
1651
                 verbose=False,
1185.32.2 by John Arbash Meinel
Refactor pull --verbose into a log.py function, add tests.
1652
                 direction='forward',
1653
                 start_revision=base_idx+1,
1654
                 end_revision=len(new_rh),
1655
                 search=None)
1656
3144.7.4 by Guillermo Gonzalez
* move the function regisstry into a real Registry instead of a list
1657
3848.1.7 by Aaron Bentley
Use repository in get_history_change
1658
def get_history_change(old_revision_id, new_revision_id, repository):
3848.1.11 by Aaron Bentley
Cleanup and use of show_branch_change
1659
    """Calculate the uncommon lefthand history between two revisions.
1660
1661
    :param old_revision_id: The original revision id.
1662
    :param new_revision_id: The new revision id.
3848.1.22 by Aaron Bentley
Fix spelling
1663
    :param repository: The repository to use for the calculation.
3848.1.11 by Aaron Bentley
Cleanup and use of show_branch_change
1664
1665
    return old_history, new_history
1666
    """
3848.1.6 by Aaron Bentley
Implement get_history_change
1667
    old_history = []
1668
    old_revisions = set()
1669
    new_history = []
1670
    new_revisions = set()
3848.1.7 by Aaron Bentley
Use repository in get_history_change
1671
    new_iter = repository.iter_reverse_revision_history(new_revision_id)
1672
    old_iter = repository.iter_reverse_revision_history(old_revision_id)
3848.1.6 by Aaron Bentley
Implement get_history_change
1673
    stop_revision = None
1674
    do_old = True
1675
    do_new = True
1676
    while do_new or do_old:
1677
        if do_new:
1678
            try:
1679
                new_revision = new_iter.next()
1680
            except StopIteration:
1681
                do_new = False
1682
            else:
1683
                new_history.append(new_revision)
1684
                new_revisions.add(new_revision)
1685
                if new_revision in old_revisions:
1686
                    stop_revision = new_revision
1687
                    break
1688
        if do_old:
1689
            try:
1690
                old_revision = old_iter.next()
1691
            except StopIteration:
1692
                do_old = False
1693
            else:
1694
                old_history.append(old_revision)
1695
                old_revisions.add(old_revision)
1696
                if old_revision in new_revisions:
1697
                    stop_revision = old_revision
1698
                    break
1699
    new_history.reverse()
1700
    old_history.reverse()
1701
    if stop_revision is not None:
1702
        new_history = new_history[new_history.index(stop_revision) + 1:]
1703
        old_history = old_history[old_history.index(stop_revision) + 1:]
1704
    return old_history, new_history
1705
1706
3848.1.11 by Aaron Bentley
Cleanup and use of show_branch_change
1707
def show_branch_change(branch, output, old_revno, old_revision_id):
1708
    """Show the changes made to a branch.
1709
1710
    :param branch: The branch to show changes about.
1711
    :param output: A file-like object to write changes to.
1712
    :param old_revno: The revno of the old tip.
1713
    :param old_revision_id: The revision_id of the old tip.
1714
    """
3848.1.8 by Aaron Bentley
Implement basic show_branch_change
1715
    new_revno, new_revision_id = branch.last_revision_info()
1716
    old_history, new_history = get_history_change(old_revision_id,
1717
                                                  new_revision_id,
1718
                                                  branch.repository)
1719
    if old_history == [] and new_history == []:
1720
        output.write('Nothing seems to have changed\n')
1721
        return
1722
3848.1.10 by Aaron Bentley
Move log display into show_flat_log
1723
    log_format = log_formatter_registry.get_default(branch)
1724
    lf = log_format(show_ids=False, to_file=output, show_timezone='original')
3848.1.8 by Aaron Bentley
Implement basic show_branch_change
1725
    if old_history != []:
1726
        output.write('*'*60)
1727
        output.write('\nRemoved Revisions:\n')
3848.1.10 by Aaron Bentley
Move log display into show_flat_log
1728
        show_flat_log(branch.repository, old_history, old_revno, lf)
3848.1.8 by Aaron Bentley
Implement basic show_branch_change
1729
        output.write('*'*60)
1730
        output.write('\n\n')
1731
    if new_history != []:
3848.1.9 by Aaron Bentley
new/old sections are omitted as appropriate.
1732
        output.write('Added Revisions:\n')
3848.1.8 by Aaron Bentley
Implement basic show_branch_change
1733
        start_revno = new_revno - len(new_history) + 1
1734
        show_log(branch, lf, None, verbose=False, direction='forward',
1735
                 start_revision=start_revno,)
1736
1737
3848.1.10 by Aaron Bentley
Move log display into show_flat_log
1738
def show_flat_log(repository, history, last_revno, lf):
3848.1.11 by Aaron Bentley
Cleanup and use of show_branch_change
1739
    """Show a simple log of the specified history.
1740
1741
    :param repository: The repository to retrieve revisions from.
1742
    :param history: A list of revision_ids indicating the lefthand history.
1743
    :param last_revno: The revno of the last revision_id in the history.
1744
    :param lf: The log formatter to use.
1745
    """
3848.1.10 by Aaron Bentley
Move log display into show_flat_log
1746
    start_revno = last_revno - len(history) + 1
1747
    revisions = repository.get_revisions(history)
1748
    for i, rev in enumerate(revisions):
1749
        lr = LogRevision(rev, i + last_revno, 0, None)
1750
        lf.log_revision(lr)
1751
1752
4202.2.1 by Ian Clatworthy
get directory logging working again
1753
def _get_info_for_log_files(revisionspec_list, file_list):
1754
    """Find file-ids and kinds given a list of files and a revision range.
1755
1756
    We search for files at the end of the range. If not found there,
1757
    we try the start of the range.
1758
1759
    :param revisionspec_list: revision range as parsed on the command line
1760
    :param file_list: the list of paths given on the command line;
1761
      the first of these can be a branch location or a file path,
1762
      the remainder must be file paths
1763
    :return: (branch, info_list, start_rev_info, end_rev_info) where
1764
      info_list is a list of (relative_path, file_id, kind) tuples where
1765
      kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
3943.6.4 by Ian Clatworthy
review feedback from vila
1766
    """
4202.2.1 by Ian Clatworthy
get directory logging working again
1767
    from builtins import _get_revision_range, safe_relpath_files
1768
    tree, b, path = bzrdir.BzrDir.open_containing_tree_or_branch(file_list[0])
1769
    # XXX: It's damn messy converting a list of paths to relative paths when
1770
    # those paths might be deleted ones, they might be on a case-insensitive
1771
    # filesystem and/or they might be in silly locations (like another branch).
1772
    # For example, what should "log bzr://branch/dir/file1 file2" do? (Is
1773
    # file2 implicitly in the same dir as file1 or should its directory be
1774
    # taken from the current tree somehow?) For now, this solves the common
1775
    # case of running log in a nested directory, assuming paths beyond the
1776
    # first one haven't been deleted ...
1777
    if tree:
1778
        relpaths = [path] + safe_relpath_files(tree, file_list[1:])
1779
    else:
1780
        relpaths = [path] + file_list[1:]
1781
    info_list = []
1782
    start_rev_info, end_rev_info = _get_revision_range(revisionspec_list, b,
1783
        "log")
1784
    if start_rev_info is None and end_rev_info is None:
3943.6.4 by Ian Clatworthy
review feedback from vila
1785
        if tree is None:
1786
            tree = b.basis_tree()
4202.2.1 by Ian Clatworthy
get directory logging working again
1787
        tree1 = None
1788
        for fp in relpaths:
1789
            file_id = tree.path2id(fp)
1790
            kind = _get_kind_for_file_id(tree, file_id)
1791
            if file_id is None:
1792
                # go back to when time began
1793
                if tree1 is None:
1794
                    try:
1795
                        rev1 = b.get_rev_id(1)
1796
                    except errors.NoSuchRevision:
1797
                        # No history at all
1798
                        file_id = None
1799
                        kind = None
1800
                    else:
1801
                        tree1 = b.repository.revision_tree(rev1)
1802
                if tree1:
1803
                    file_id = tree1.path2id(fp)
1804
                    kind = _get_kind_for_file_id(tree1, file_id)
1805
            info_list.append((fp, file_id, kind))
3943.6.4 by Ian Clatworthy
review feedback from vila
1806
4202.2.1 by Ian Clatworthy
get directory logging working again
1807
    elif start_rev_info == end_rev_info:
3943.6.4 by Ian Clatworthy
review feedback from vila
1808
        # One revision given - file must exist in it
4202.2.1 by Ian Clatworthy
get directory logging working again
1809
        tree = b.repository.revision_tree(end_rev_info.rev_id)
1810
        for fp in relpaths:
1811
            file_id = tree.path2id(fp)
1812
            kind = _get_kind_for_file_id(tree, file_id)
1813
            info_list.append((fp, file_id, kind))
3943.6.4 by Ian Clatworthy
review feedback from vila
1814
4202.2.1 by Ian Clatworthy
get directory logging working again
1815
    else:
3943.6.4 by Ian Clatworthy
review feedback from vila
1816
        # Revision range given. Get the file-id from the end tree.
1817
        # If that fails, try the start tree.
4202.2.1 by Ian Clatworthy
get directory logging working again
1818
        rev_id = end_rev_info.rev_id
3943.6.4 by Ian Clatworthy
review feedback from vila
1819
        if rev_id is None:
1820
            tree = b.basis_tree()
1821
        else:
4202.2.1 by Ian Clatworthy
get directory logging working again
1822
            tree = b.repository.revision_tree(rev_id)
1823
        tree1 = None
1824
        for fp in relpaths:
3943.6.4 by Ian Clatworthy
review feedback from vila
1825
            file_id = tree.path2id(fp)
4202.2.1 by Ian Clatworthy
get directory logging working again
1826
            kind = _get_kind_for_file_id(tree, file_id)
1827
            if file_id is None:
1828
                if tree1 is None:
1829
                    rev_id = start_rev_info.rev_id
1830
                    if rev_id is None:
1831
                        rev1 = b.get_rev_id(1)
1832
                        tree1 = b.repository.revision_tree(rev1)
1833
                    else:
1834
                        tree1 = b.repository.revision_tree(rev_id)
1835
                file_id = tree1.path2id(fp)
1836
                kind = _get_kind_for_file_id(tree1, file_id)
1837
            info_list.append((fp, file_id, kind))
1838
    return b, info_list, start_rev_info, end_rev_info
1839
1840
1841
def _get_kind_for_file_id(tree, file_id):
1842
    """Return the kind of a file-id or None if it doesn't exist."""
1843
    if file_id is not None:
1844
        return tree.kind(file_id)
3943.6.4 by Ian Clatworthy
review feedback from vila
1845
    else:
4202.2.1 by Ian Clatworthy
get directory logging working again
1846
        return None
3943.6.4 by Ian Clatworthy
review feedback from vila
1847
1848
3144.7.9 by Guillermo Gonzalez
* bzrlib.log.show_roperties don't hide handler errors
1849
properties_handler_registry = registry.Registry()
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
1850
properties_handler_registry.register_lazy("foreign",
1851
                                          "bzrlib.foreign",
1852
                                          "show_foreign_properties")
1853
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
1854
1855
# adapters which revision ids to log are filtered. When log is called, the
1856
# log_rev_iterator is adapted through each of these factory methods.
1857
# Plugins are welcome to mutate this list in any way they like - as long
1858
# as the overall behaviour is preserved. At this point there is no extensible
1859
# mechanism for getting parameters to each factory method, and until there is
1860
# this won't be considered a stable api.
1861
log_adapters = [
1862
    # core log logic
3642.1.7 by Robert Collins
Review feedback.
1863
    _make_batch_filter,
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
1864
    # read revision objects
3642.1.7 by Robert Collins
Review feedback.
1865
    _make_revision_objects,
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
1866
    # filter on log messages
3642.1.7 by Robert Collins
Review feedback.
1867
    _make_search_filter,
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
1868
    # generate deltas for things we will show
3642.1.7 by Robert Collins
Review feedback.
1869
    _make_delta_filter
3642.1.6 by Robert Collins
Make log revision filtering pluggable.
1870
    ]