/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/log.py

  • Committer: Robert Collins
  • Date: 2008-08-20 02:07:36 UTC
  • mfrom: (3640 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3682.
  • Revision ID: robertc@robertcollins.net-20080820020736-g2xe4921zzxtymle
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42
42
 
43
43
In verbose mode we show a summary of what changed in each particular
44
44
revision.  Note that this is the delta for changes in that revision
45
 
relative to its mainline parent, not the delta relative to the last
 
45
relative to its left-most parent, not the delta relative to the last
46
46
logged revision.  So for example if you ask for a verbose log of
47
47
changes touching hello.c you will get a list of those revisions also
48
48
listing other things that were changed in the same revision, but not
49
49
all the changes since the previous revision that touched hello.c.
50
50
"""
51
51
 
52
 
 
53
 
# TODO: option to show delta summaries for merged-in revisions
 
52
import codecs
 
53
from itertools import (
 
54
    izip,
 
55
    )
54
56
import re
55
 
 
56
 
from bzrlib.delta import compare_trees
57
 
import bzrlib.errors as errors
58
 
from bzrlib.trace import mutter
59
 
from bzrlib.tree import EmptyTree
60
 
from bzrlib.tsort import merge_sort
 
57
import sys
 
58
from warnings import (
 
59
    warn,
 
60
    )
 
61
 
 
62
from bzrlib.lazy_import import lazy_import
 
63
lazy_import(globals(), """
 
64
 
 
65
from bzrlib import (
 
66
    config,
 
67
    errors,
 
68
    repository as _mod_repository,
 
69
    revision as _mod_revision,
 
70
    revisionspec,
 
71
    trace,
 
72
    tsort,
 
73
    )
 
74
""")
 
75
 
 
76
from bzrlib import (
 
77
    registry,
 
78
    )
 
79
from bzrlib.osutils import (
 
80
    format_date,
 
81
    get_terminal_encoding,
 
82
    terminal_width,
 
83
    )
61
84
 
62
85
 
63
86
def find_touching_revisions(branch, file_id):
104
127
        revno += 1
105
128
 
106
129
 
107
 
 
108
130
def _enumerate_history(branch):
109
131
    rh = []
110
132
    revno = 1
114
136
    return rh
115
137
 
116
138
 
117
 
def _get_revision_delta(branch, revno):
118
 
    """Return the delta for a mainline revision.
119
 
    
120
 
    This is used to show summaries in verbose logs, and also for finding 
121
 
    revisions which touch a given file."""
122
 
    # XXX: What are we supposed to do when showing a summary for something 
123
 
    # other than a mainline revision.  The delta to it's first parent, or
124
 
    # (more useful) the delta to a nominated other revision.
125
 
    return branch.get_revision_delta(revno)
126
 
 
127
 
 
128
139
def show_log(branch,
129
140
             lf,
130
141
             specific_fileid=None,
132
143
             direction='reverse',
133
144
             start_revision=None,
134
145
             end_revision=None,
135
 
             search=None):
 
146
             search=None,
 
147
             limit=None):
136
148
    """Write out human-readable log of commits to this branch.
137
149
 
138
150
    lf
154
166
 
155
167
    end_revision
156
168
        If not None, only show revisions <= end_revision
 
169
 
 
170
    search
 
171
        If not None, only show revisions with matching commit messages
 
172
 
 
173
    limit
 
174
        If not None or 0, only show limit revisions
157
175
    """
158
176
    branch.lock_read()
159
177
    try:
 
178
        if getattr(lf, 'begin_log', None):
 
179
            lf.begin_log()
 
180
 
160
181
        _show_log(branch, lf, specific_fileid, verbose, direction,
161
 
                  start_revision, end_revision, search)
 
182
                  start_revision, end_revision, search, limit)
 
183
 
 
184
        if getattr(lf, 'end_log', None):
 
185
            lf.end_log()
162
186
    finally:
163
187
        branch.unlock()
164
 
    
 
188
 
 
189
 
165
190
def _show_log(branch,
166
191
             lf,
167
192
             specific_fileid=None,
169
194
             direction='reverse',
170
195
             start_revision=None,
171
196
             end_revision=None,
172
 
             search=None):
 
197
             search=None,
 
198
             limit=None):
173
199
    """Worker function for show_log - see show_log."""
174
 
    from bzrlib.osutils import format_date
175
 
    from bzrlib.errors import BzrCheckError
176
 
    from bzrlib.textui import show_status
177
 
    
178
 
    from warnings import warn
179
 
 
180
200
    if not isinstance(lf, LogFormatter):
181
201
        warn("not a LogFormatter instance: %r" % lf)
182
202
 
183
203
    if specific_fileid:
184
 
        mutter('get log for file_id %r', specific_fileid)
185
 
 
 
204
        trace.mutter('get log for file_id %r', specific_fileid)
 
205
    generate_merge_revisions = getattr(lf, 'supports_merge_revisions', False)
 
206
    allow_single_merge_revision = getattr(lf,
 
207
        'supports_single_merge_revision', False)
 
208
    view_revisions = calculate_view_revisions(branch, start_revision,
 
209
                                              end_revision, direction,
 
210
                                              specific_fileid,
 
211
                                              generate_merge_revisions,
 
212
                                              allow_single_merge_revision)
186
213
    if search is not None:
187
 
        import re
188
214
        searchRE = re.compile(search, re.IGNORECASE)
189
215
    else:
190
216
        searchRE = None
191
217
 
192
 
    which_revs = _enumerate_history(branch)
193
 
    
 
218
    rev_tag_dict = {}
 
219
    generate_tags = getattr(lf, 'supports_tags', False)
 
220
    if generate_tags:
 
221
        if branch.supports_tags():
 
222
            rev_tag_dict = branch.tags.get_reverse_tag_dict()
 
223
 
 
224
    generate_delta = verbose and getattr(lf, 'supports_delta', False)
 
225
 
 
226
    # now we just print all the revisions
 
227
    log_count = 0
 
228
    for (rev_id, revno, merge_depth), rev, delta in _iter_revisions(
 
229
        branch.repository, view_revisions, generate_delta):
 
230
        if searchRE:
 
231
            if not searchRE.search(rev.message):
 
232
                continue
 
233
 
 
234
        lr = LogRevision(rev, revno, merge_depth, delta,
 
235
                         rev_tag_dict.get(rev_id))
 
236
        lf.log_revision(lr)
 
237
        if limit:
 
238
            log_count += 1
 
239
            if log_count >= limit:
 
240
                break
 
241
 
 
242
 
 
243
def calculate_view_revisions(branch, start_revision, end_revision, direction,
 
244
                             specific_fileid, generate_merge_revisions,
 
245
                             allow_single_merge_revision):
 
246
    if (not generate_merge_revisions and start_revision is end_revision is
 
247
        None and direction == 'reverse' and specific_fileid is None):
 
248
        return _linear_view_revisions(branch)
 
249
 
 
250
    mainline_revs, rev_nos, start_rev_id, end_rev_id = \
 
251
        _get_mainline_revs(branch, start_revision, end_revision)
 
252
    if not mainline_revs:
 
253
        return []
 
254
 
 
255
    if direction == 'reverse':
 
256
        start_rev_id, end_rev_id = end_rev_id, start_rev_id
 
257
 
 
258
    generate_single_revision = False
 
259
    if ((not generate_merge_revisions)
 
260
        and ((start_rev_id and (start_rev_id not in rev_nos))
 
261
            or (end_rev_id and (end_rev_id not in rev_nos)))):
 
262
        generate_single_revision = ((start_rev_id == end_rev_id)
 
263
            and allow_single_merge_revision)
 
264
        if not generate_single_revision:
 
265
            raise errors.BzrCommandError('Selected log formatter only supports'
 
266
                ' mainline revisions.')
 
267
        generate_merge_revisions = generate_single_revision
 
268
    view_revs_iter = get_view_revisions(mainline_revs, rev_nos, branch,
 
269
                          direction, include_merges=generate_merge_revisions)
 
270
    view_revisions = _filter_revision_range(list(view_revs_iter),
 
271
                                            start_rev_id,
 
272
                                            end_rev_id)
 
273
    if view_revisions and generate_single_revision:
 
274
        view_revisions = view_revisions[0:1]
 
275
    if specific_fileid:
 
276
        view_revisions = _filter_revisions_touching_file_id(branch,
 
277
                                                         specific_fileid,
 
278
                                                         mainline_revs,
 
279
                                                         view_revisions)
 
280
 
 
281
    # rebase merge_depth - unless there are no revisions or 
 
282
    # either the first or last revision have merge_depth = 0.
 
283
    if view_revisions and view_revisions[0][2] and view_revisions[-1][2]:
 
284
        min_depth = min([d for r,n,d in view_revisions])
 
285
        if min_depth != 0:
 
286
            view_revisions = [(r,n,d-min_depth) for r,n,d in view_revisions]
 
287
    return view_revisions
 
288
 
 
289
 
 
290
def _linear_view_revisions(branch):
 
291
    start_revno, start_revision_id = branch.last_revision_info()
 
292
    repo = branch.repository
 
293
    revision_ids = repo.iter_reverse_revision_history(start_revision_id)
 
294
    for num, revision_id in enumerate(revision_ids):
 
295
        yield revision_id, str(start_revno - num), 0
 
296
 
 
297
 
 
298
def _iter_revisions(repository, view_revisions, generate_delta):
 
299
    num = 9
 
300
    view_revisions = iter(view_revisions)
 
301
    while True:
 
302
        cur_view_revisions = [d for x, d in zip(range(num), view_revisions)]
 
303
        if len(cur_view_revisions) == 0:
 
304
            break
 
305
        cur_deltas = {}
 
306
        # r = revision, n = revno, d = merge depth
 
307
        revision_ids = [r for (r, n, d) in cur_view_revisions]
 
308
        revisions = repository.get_revisions(revision_ids)
 
309
        if generate_delta:
 
310
            deltas = repository.get_deltas_for_revisions(revisions)
 
311
            cur_deltas = dict(izip((r.revision_id for r in revisions),
 
312
                                   deltas))
 
313
        for view_data, revision in izip(cur_view_revisions, revisions):
 
314
            yield view_data, revision, cur_deltas.get(revision.revision_id)
 
315
        num = min(int(num * 1.5), 200)
 
316
 
 
317
 
 
318
def _get_mainline_revs(branch, start_revision, end_revision):
 
319
    """Get the mainline revisions from the branch.
 
320
    
 
321
    Generates the list of mainline revisions for the branch.
 
322
    
 
323
    :param  branch: The branch containing the revisions. 
 
324
 
 
325
    :param  start_revision: The first revision to be logged.
 
326
            For backwards compatibility this may be a mainline integer revno,
 
327
            but for merge revision support a RevisionInfo is expected.
 
328
 
 
329
    :param  end_revision: The last revision to be logged.
 
330
            For backwards compatibility this may be a mainline integer revno,
 
331
            but for merge revision support a RevisionInfo is expected.
 
332
 
 
333
    :return: A (mainline_revs, rev_nos, start_rev_id, end_rev_id) tuple.
 
334
    """
 
335
    branch_revno, branch_last_revision = branch.last_revision_info()
 
336
    if branch_revno == 0:
 
337
        return None, None, None, None
 
338
 
 
339
    # For mainline generation, map start_revision and end_revision to 
 
340
    # mainline revnos. If the revision is not on the mainline choose the 
 
341
    # appropriate extreme of the mainline instead - the extra will be 
 
342
    # filtered later.
 
343
    # Also map the revisions to rev_ids, to be used in the later filtering
 
344
    # stage.
 
345
    start_rev_id = None 
194
346
    if start_revision is None:
195
 
        start_revision = 1
 
347
        start_revno = 1
196
348
    else:
197
 
        branch.check_real_revno(start_revision)
 
349
        if isinstance(start_revision, revisionspec.RevisionInfo):
 
350
            start_rev_id = start_revision.rev_id
 
351
            start_revno = start_revision.revno or 1
 
352
        else:
 
353
            branch.check_real_revno(start_revision)
 
354
            start_revno = start_revision
198
355
    
 
356
    end_rev_id = None
199
357
    if end_revision is None:
200
 
        end_revision = len(which_revs)
201
 
    else:
202
 
        branch.check_real_revno(end_revision)
203
 
 
204
 
    # list indexes are 0-based; revisions are 1-based
205
 
    cut_revs = which_revs[(start_revision-1):(end_revision)]
206
 
    if not cut_revs:
207
 
        return
 
358
        end_revno = branch_revno
 
359
    else:
 
360
        if isinstance(end_revision, revisionspec.RevisionInfo):
 
361
            end_rev_id = end_revision.rev_id
 
362
            end_revno = end_revision.revno or branch_revno
 
363
        else:
 
364
            branch.check_real_revno(end_revision)
 
365
            end_revno = end_revision
 
366
 
 
367
    if ((start_rev_id == _mod_revision.NULL_REVISION)
 
368
        or (end_rev_id == _mod_revision.NULL_REVISION)):
 
369
        raise errors.BzrCommandError('Logging revision 0 is invalid.')
 
370
    if start_revno > end_revno:
 
371
        raise errors.BzrCommandError("Start revision must be older than "
 
372
                                     "the end revision.")
 
373
 
 
374
    if end_revno < start_revno:
 
375
        return None, None, None, None
 
376
    cur_revno = branch_revno
 
377
    rev_nos = {}
 
378
    mainline_revs = []
 
379
    for revision_id in branch.repository.iter_reverse_revision_history(
 
380
                        branch_last_revision):
 
381
        if cur_revno < start_revno:
 
382
            # We have gone far enough, but we always add 1 more revision
 
383
            rev_nos[revision_id] = cur_revno
 
384
            mainline_revs.append(revision_id)
 
385
            break
 
386
        if cur_revno <= end_revno:
 
387
            rev_nos[revision_id] = cur_revno
 
388
            mainline_revs.append(revision_id)
 
389
        cur_revno -= 1
 
390
    else:
 
391
        # We walked off the edge of all revisions, so we add a 'None' marker
 
392
        mainline_revs.append(None)
 
393
 
 
394
    mainline_revs.reverse()
 
395
 
208
396
    # override the mainline to look like the revision history.
209
 
    mainline_revs = [revision_id for index, revision_id in cut_revs]
210
 
    if cut_revs[0][0] == 1:
211
 
        mainline_revs.insert(0, None)
212
 
    else:
213
 
        mainline_revs.insert(0, which_revs[start_revision-2][1])
214
 
 
215
 
    merge_sorted_revisions = merge_sort(
216
 
        branch.repository.get_revision_graph(mainline_revs[-1]),
 
397
    return mainline_revs, rev_nos, start_rev_id, end_rev_id
 
398
 
 
399
 
 
400
def _filter_revision_range(view_revisions, start_rev_id, end_rev_id):
 
401
    """Filter view_revisions based on revision ranges.
 
402
 
 
403
    :param view_revisions: A list of (revision_id, dotted_revno, merge_depth) 
 
404
            tuples to be filtered.
 
405
 
 
406
    :param start_rev_id: If not NONE specifies the first revision to be logged.
 
407
            If NONE then all revisions up to the end_rev_id are logged.
 
408
 
 
409
    :param end_rev_id: If not NONE specifies the last revision to be logged.
 
410
            If NONE then all revisions up to the end of the log are logged.
 
411
 
 
412
    :return: The filtered view_revisions.
 
413
    """
 
414
    if start_rev_id or end_rev_id: 
 
415
        revision_ids = [r for r, n, d in view_revisions]
 
416
        if start_rev_id:
 
417
            start_index = revision_ids.index(start_rev_id)
 
418
        else:
 
419
            start_index = 0
 
420
        if start_rev_id == end_rev_id:
 
421
            end_index = start_index
 
422
        else:
 
423
            if end_rev_id:
 
424
                end_index = revision_ids.index(end_rev_id)
 
425
            else:
 
426
                end_index = len(view_revisions) - 1
 
427
        # To include the revisions merged into the last revision, 
 
428
        # extend end_rev_id down to, but not including, the next rev
 
429
        # with the same or lesser merge_depth
 
430
        end_merge_depth = view_revisions[end_index][2]
 
431
        try:
 
432
            for index in xrange(end_index+1, len(view_revisions)+1):
 
433
                if view_revisions[index][2] <= end_merge_depth:
 
434
                    end_index = index - 1
 
435
                    break
 
436
        except IndexError:
 
437
            # if the search falls off the end then log to the end as well
 
438
            end_index = len(view_revisions) - 1
 
439
        view_revisions = view_revisions[start_index:end_index+1]
 
440
    return view_revisions
 
441
 
 
442
 
 
443
def _filter_revisions_touching_file_id(branch, file_id, mainline_revisions,
 
444
                                       view_revs_iter):
 
445
    """Return the list of revision ids which touch a given file id.
 
446
 
 
447
    The function filters view_revisions and returns a subset.
 
448
    This includes the revisions which directly change the file id,
 
449
    and the revisions which merge these changes. So if the
 
450
    revision graph is::
 
451
        A
 
452
        |\
 
453
        B C
 
454
        |/
 
455
        D
 
456
 
 
457
    And 'C' changes a file, then both C and D will be returned.
 
458
 
 
459
    This will also can be restricted based on a subset of the mainline.
 
460
 
 
461
    :return: A list of (revision_id, dotted_revno, merge_depth) tuples.
 
462
    """
 
463
    # find all the revisions that change the specific file
 
464
    # build the ancestry of each revision in the graph
 
465
    # - only listing the ancestors that change the specific file.
 
466
    graph = branch.repository.get_graph()
 
467
    # This asks for all mainline revisions, which means we only have to spider
 
468
    # sideways, rather than depth history. That said, its still size-of-history
 
469
    # and should be addressed.
 
470
    # mainline_revisions always includes an extra revision at the beginning, so
 
471
    # don't request it.
 
472
    parent_map = dict(((key, value) for key, value in
 
473
        graph.iter_ancestry(mainline_revisions[1:]) if value is not None))
 
474
    sorted_rev_list = tsort.topo_sort(parent_map.items())
 
475
    text_keys = [(file_id, rev_id) for rev_id in sorted_rev_list]
 
476
    modified_text_versions = branch.repository.texts.get_parent_map(text_keys)
 
477
    ancestry = {}
 
478
    for rev in sorted_rev_list:
 
479
        text_key = (file_id, rev)
 
480
        parents = parent_map[rev]
 
481
        if text_key not in modified_text_versions and len(parents) == 1:
 
482
            # We will not be adding anything new, so just use a reference to
 
483
            # the parent ancestry.
 
484
            rev_ancestry = ancestry[parents[0]]
 
485
        else:
 
486
            rev_ancestry = set()
 
487
            if text_key in modified_text_versions:
 
488
                rev_ancestry.add(rev)
 
489
            for parent in parents:
 
490
                if parent not in ancestry:
 
491
                    # parent is a Ghost, which won't be present in
 
492
                    # sorted_rev_list, but we may access it later, so create an
 
493
                    # empty node for it
 
494
                    ancestry[parent] = set()
 
495
                rev_ancestry = rev_ancestry.union(ancestry[parent])
 
496
        ancestry[rev] = rev_ancestry
 
497
 
 
498
    def is_merging_rev(r):
 
499
        parents = parent_map[r]
 
500
        if len(parents) > 1:
 
501
            leftparent = parents[0]
 
502
            for rightparent in parents[1:]:
 
503
                if not ancestry[leftparent].issuperset(
 
504
                        ancestry[rightparent]):
 
505
                    return True
 
506
        return False
 
507
 
 
508
    # filter from the view the revisions that did not change or merge 
 
509
    # the specific file
 
510
    return [(r, n, d) for r, n, d in view_revs_iter
 
511
            if (file_id, r) in modified_text_versions or is_merging_rev(r)]
 
512
 
 
513
 
 
514
def get_view_revisions(mainline_revs, rev_nos, branch, direction,
 
515
                       include_merges=True):
 
516
    """Produce an iterator of revisions to show
 
517
    :return: an iterator of (revision_id, revno, merge_depth)
 
518
    (if there is no revno for a revision, None is supplied)
 
519
    """
 
520
    if include_merges is False:
 
521
        revision_ids = mainline_revs[1:]
 
522
        if direction == 'reverse':
 
523
            revision_ids.reverse()
 
524
        for revision_id in revision_ids:
 
525
            yield revision_id, str(rev_nos[revision_id]), 0
 
526
        return
 
527
    graph = branch.repository.get_graph()
 
528
    # This asks for all mainline revisions, which means we only have to spider
 
529
    # sideways, rather than depth history. That said, its still size-of-history
 
530
    # and should be addressed.
 
531
    # mainline_revisions always includes an extra revision at the beginning, so
 
532
    # don't request it.
 
533
    parent_map = dict(((key, value) for key, value in
 
534
        graph.iter_ancestry(mainline_revs[1:]) if value is not None))
 
535
    # filter out ghosts; merge_sort errors on ghosts.
 
536
    rev_graph = _mod_repository._strip_NULL_ghosts(parent_map)
 
537
    merge_sorted_revisions = tsort.merge_sort(
 
538
        rev_graph,
217
539
        mainline_revs[-1],
218
 
        mainline_revs)
 
540
        mainline_revs,
 
541
        generate_revno=True)
219
542
 
220
 
    if direction == 'reverse':
221
 
        cut_revs.reverse()
222
 
    elif direction == 'forward':
 
543
    if direction == 'forward':
223
544
        # forward means oldest first.
224
 
        merge_sorted_revisions.reverse()
225
 
    else:
 
545
        merge_sorted_revisions = reverse_by_depth(merge_sorted_revisions)
 
546
    elif direction != 'reverse':
226
547
        raise ValueError('invalid direction %r' % direction)
227
548
 
228
 
    revision_history = branch.revision_history()
229
 
 
230
 
    # convert the revision history to a dictionary:
231
 
    rev_nos = {}
232
 
    for index, rev_id in cut_revs:
233
 
        rev_nos[rev_id] = index
234
 
 
235
 
    # now we just print all the revisions
236
 
    for sequence, rev_id, merge_depth, end_of_merge in merge_sorted_revisions:
237
 
        rev = branch.repository.get_revision(rev_id)
238
 
 
239
 
        if searchRE:
240
 
            if not searchRE.search(rev.message):
241
 
                continue
242
 
 
243
 
        if merge_depth == 0:
244
 
            # a mainline revision.
245
 
            if verbose or specific_fileid:
246
 
                delta = _get_revision_delta(branch, rev_nos[rev_id])
247
 
                
248
 
            if specific_fileid:
249
 
                if not delta.touches_file_id(specific_fileid):
250
 
                    continue
251
 
    
252
 
            if not verbose:
253
 
                # although we calculated it, throw it away without display
254
 
                delta = None
255
 
 
256
 
            lf.show(rev_nos[rev_id], rev, delta)
257
 
        elif hasattr(lf, 'show_merge'):
258
 
            lf.show_merge(rev, merge_depth)
259
 
 
260
 
 
261
 
def deltas_for_log_dummy(branch, which_revs):
262
 
    """Return all the revisions without intermediate deltas.
263
 
 
264
 
    Useful for log commands that won't need the delta information.
265
 
    """
266
 
    
267
 
    for revno, revision_id in which_revs:
268
 
        yield revno, branch.get_revision(revision_id), None
269
 
 
270
 
 
271
 
def deltas_for_log_reverse(branch, which_revs):
272
 
    """Compute deltas for display in latest-to-earliest order.
273
 
 
274
 
    branch
275
 
        Branch to traverse
276
 
 
277
 
    which_revs
278
 
        Sequence of (revno, revision_id) for the subset of history to examine
279
 
 
280
 
    returns 
281
 
        Sequence of (revno, rev, delta)
282
 
 
283
 
    The delta is from the given revision to the next one in the
284
 
    sequence, which makes sense if the log is being displayed from
285
 
    newest to oldest.
286
 
    """
287
 
    last_revno = last_revision_id = last_tree = None
288
 
    for revno, revision_id in which_revs:
289
 
        this_tree = branch.revision_tree(revision_id)
290
 
        this_revision = branch.get_revision(revision_id)
291
 
        
292
 
        if last_revno:
293
 
            yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
294
 
 
295
 
        this_tree = EmptyTree(branch.get_root_id())
296
 
 
297
 
        last_revno = revno
298
 
        last_revision = this_revision
299
 
        last_tree = this_tree
300
 
 
301
 
    if last_revno:
302
 
        if last_revno == 1:
303
 
            this_tree = EmptyTree(branch.get_root_id())
 
549
    for sequence, rev_id, merge_depth, revno, end_of_merge in merge_sorted_revisions:
 
550
        yield rev_id, '.'.join(map(str, revno)), merge_depth
 
551
 
 
552
 
 
553
def reverse_by_depth(merge_sorted_revisions, _depth=0):
 
554
    """Reverse revisions by depth.
 
555
 
 
556
    Revisions with a different depth are sorted as a group with the previous
 
557
    revision of that depth.  There may be no topological justification for this,
 
558
    but it looks much nicer.
 
559
    """
 
560
    zd_revisions = []
 
561
    for val in merge_sorted_revisions:
 
562
        if val[2] == _depth:
 
563
            zd_revisions.append([val])
304
564
        else:
305
 
            this_revno = last_revno - 1
306
 
            this_revision_id = branch.revision_history()[this_revno]
307
 
            this_tree = branch.revision_tree(this_revision_id)
308
 
        yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
309
 
 
310
 
 
311
 
def deltas_for_log_forward(branch, which_revs):
312
 
    """Compute deltas for display in forward log.
313
 
 
314
 
    Given a sequence of (revno, revision_id) pairs, return
315
 
    (revno, rev, delta).
316
 
 
317
 
    The delta is from the given revision to the next one in the
318
 
    sequence, which makes sense if the log is being displayed from
319
 
    newest to oldest.
 
565
            zd_revisions[-1].append(val)
 
566
    for revisions in zd_revisions:
 
567
        if len(revisions) > 1:
 
568
            revisions[1:] = reverse_by_depth(revisions[1:], _depth + 1)
 
569
    zd_revisions.reverse()
 
570
    result = []
 
571
    for chunk in zd_revisions:
 
572
        result.extend(chunk)
 
573
    return result
 
574
 
 
575
 
 
576
class LogRevision(object):
 
577
    """A revision to be logged (by LogFormatter.log_revision).
 
578
 
 
579
    A simple wrapper for the attributes of a revision to be logged.
 
580
    The attributes may or may not be populated, as determined by the 
 
581
    logging options and the log formatter capabilities.
320
582
    """
321
 
    last_revno = last_revision_id = last_tree = None
322
 
    prev_tree = EmptyTree(branch.get_root_id())
323
 
 
324
 
    for revno, revision_id in which_revs:
325
 
        this_tree = branch.revision_tree(revision_id)
326
 
        this_revision = branch.get_revision(revision_id)
327
 
 
328
 
        if not last_revno:
329
 
            if revno == 1:
330
 
                last_tree = EmptyTree(branch.get_root_id())
331
 
            else:
332
 
                last_revno = revno - 1
333
 
                last_revision_id = branch.revision_history()[last_revno]
334
 
                last_tree = branch.revision_tree(last_revision_id)
335
 
 
336
 
        yield revno, this_revision, compare_trees(last_tree, this_tree, False)
337
 
 
338
 
        last_revno = revno
339
 
        last_revision = this_revision
340
 
        last_tree = this_tree
 
583
 
 
584
    def __init__(self, rev=None, revno=None, merge_depth=0, delta=None,
 
585
                 tags=None):
 
586
        self.rev = rev
 
587
        self.revno = revno
 
588
        self.merge_depth = merge_depth
 
589
        self.delta = delta
 
590
        self.tags = tags
341
591
 
342
592
 
343
593
class LogFormatter(object):
344
 
    """Abstract class to display log messages."""
 
594
    """Abstract class to display log messages.
 
595
 
 
596
    At a minimum, a derived class must implement the log_revision method.
 
597
 
 
598
    If the LogFormatter needs to be informed of the beginning or end of
 
599
    a log it should implement the begin_log and/or end_log hook methods.
 
600
 
 
601
    A LogFormatter should define the following supports_XXX flags 
 
602
    to indicate which LogRevision attributes it supports:
 
603
 
 
604
    - supports_delta must be True if this log formatter supports delta.
 
605
        Otherwise the delta attribute may not be populated.
 
606
    - supports_merge_revisions must be True if this log formatter supports 
 
607
        merge revisions.  If not, and if supports_single_merge_revisions is
 
608
        also not True, then only mainline revisions will be passed to the 
 
609
        formatter.
 
610
    - supports_single_merge_revision must be True if this log formatter
 
611
        supports logging only a single merge revision.  This flag is
 
612
        only relevant if supports_merge_revisions is not True.
 
613
    - supports_tags must be True if this log formatter supports tags.
 
614
        Otherwise the tags attribute may not be populated.
 
615
 
 
616
    Plugins can register functions to show custom revision properties using
 
617
    the properties_handler_registry. The registered function
 
618
    must respect the following interface description:
 
619
        def my_show_properties(properties_dict):
 
620
            # code that returns a dict {'name':'value'} of the properties 
 
621
            # to be shown
 
622
    """
345
623
 
346
624
    def __init__(self, to_file, show_ids=False, show_timezone='original'):
347
625
        self.to_file = to_file
348
626
        self.show_ids = show_ids
349
627
        self.show_timezone = show_timezone
350
628
 
351
 
    def show(self, revno, rev, delta):
352
 
        raise NotImplementedError('not implemented in abstract base')
 
629
# TODO: uncomment this block after show() has been removed.
 
630
# Until then defining log_revision would prevent _show_log calling show() 
 
631
# in legacy formatters.
 
632
#    def log_revision(self, revision):
 
633
#        """Log a revision.
 
634
#
 
635
#        :param  revision:   The LogRevision to be logged.
 
636
#        """
 
637
#        raise NotImplementedError('not implemented in abstract base')
353
638
 
354
639
    def short_committer(self, rev):
355
 
        return re.sub('<.*@.*>', '', rev.committer).strip(' ')
356
 
    
357
 
    
 
640
        name, address = config.parse_username(rev.committer)
 
641
        if name:
 
642
            return name
 
643
        return address
 
644
 
 
645
    def short_author(self, rev):
 
646
        name, address = config.parse_username(rev.get_apparent_author())
 
647
        if name:
 
648
            return name
 
649
        return address
 
650
 
 
651
    def show_properties(self, revision, indent):
 
652
        """Displays the custom properties returned by each registered handler.
 
653
        
 
654
        If a registered handler raises an error it is propagated.
 
655
        """
 
656
        for key, handler in properties_handler_registry.iteritems():
 
657
            for key, value in handler(revision).items():
 
658
                self.to_file.write(indent + key + ': ' + value + '\n')
 
659
 
 
660
 
358
661
class LongLogFormatter(LogFormatter):
359
 
    def show(self, revno, rev, delta):
360
 
        return self._show_helper(revno=revno, rev=rev, delta=delta)
361
 
 
362
 
    def show_merge(self, rev, merge_depth):
363
 
        return self._show_helper(rev=rev, indent='    '*merge_depth, merged=True, delta=None)
364
 
 
365
 
    def _show_helper(self, rev=None, revno=None, indent='', merged=False, delta=None):
366
 
        """Show a revision, either merged or not."""
367
 
        from bzrlib.osutils import format_date
 
662
 
 
663
    supports_merge_revisions = True
 
664
    supports_delta = True
 
665
    supports_tags = True
 
666
 
 
667
    def log_revision(self, revision):
 
668
        """Log a revision, either merged or not."""
 
669
        indent = '    ' * revision.merge_depth
368
670
        to_file = self.to_file
369
 
        print >>to_file,  indent+'-' * 60
370
 
        if revno is not None:
371
 
            print >>to_file,  'revno:', revno
372
 
        if merged:
373
 
            print >>to_file,  indent+'merged:', rev.revision_id
374
 
        elif self.show_ids:
375
 
            print >>to_file,  indent+'revision-id:', rev.revision_id
 
671
        to_file.write(indent + '-' * 60 + '\n')
 
672
        if revision.revno is not None:
 
673
            to_file.write(indent + 'revno: %s\n' % (revision.revno,))
 
674
        if revision.tags:
 
675
            to_file.write(indent + 'tags: %s\n' % (', '.join(revision.tags)))
376
676
        if self.show_ids:
377
 
            for parent_id in rev.parent_ids:
378
 
                print >>to_file, indent+'parent:', parent_id
379
 
        print >>to_file,  indent+'committer:', rev.committer
380
 
        try:
381
 
            print >>to_file, indent+'branch nick: %s' % \
382
 
                rev.properties['branch-nick']
383
 
        except KeyError:
384
 
            pass
385
 
        date_str = format_date(rev.timestamp,
386
 
                               rev.timezone or 0,
 
677
            to_file.write(indent + 'revision-id: ' + revision.rev.revision_id)
 
678
            to_file.write('\n')
 
679
            for parent_id in revision.rev.parent_ids:
 
680
                to_file.write(indent + 'parent: %s\n' % (parent_id,))
 
681
        self.show_properties(revision.rev, indent)
 
682
 
 
683
        author = revision.rev.properties.get('author', None)
 
684
        if author is not None:
 
685
            to_file.write(indent + 'author: %s\n' % (author,))
 
686
        to_file.write(indent + 'committer: %s\n' % (revision.rev.committer,))
 
687
 
 
688
        branch_nick = revision.rev.properties.get('branch-nick', None)
 
689
        if branch_nick is not None:
 
690
            to_file.write(indent + 'branch nick: %s\n' % (branch_nick,))
 
691
 
 
692
        date_str = format_date(revision.rev.timestamp,
 
693
                               revision.rev.timezone or 0,
387
694
                               self.show_timezone)
388
 
        print >>to_file,  indent+'timestamp: %s' % date_str
 
695
        to_file.write(indent + 'timestamp: %s\n' % (date_str,))
389
696
 
390
 
        print >>to_file,  indent+'message:'
391
 
        if not rev.message:
392
 
            print >>to_file,  indent+'  (no message)'
 
697
        to_file.write(indent + 'message:\n')
 
698
        if not revision.rev.message:
 
699
            to_file.write(indent + '  (no message)\n')
393
700
        else:
394
 
            message = rev.message.rstrip('\r\n')
 
701
            message = revision.rev.message.rstrip('\r\n')
395
702
            for l in message.split('\n'):
396
 
                print >>to_file,  indent+'  ' + l
397
 
        if delta != None:
398
 
            delta.show(to_file, self.show_ids)
 
703
                to_file.write(indent + '  %s\n' % (l,))
 
704
        if revision.delta is not None:
 
705
            revision.delta.show(to_file, self.show_ids, indent=indent)
399
706
 
400
707
 
401
708
class ShortLogFormatter(LogFormatter):
402
 
    def show(self, revno, rev, delta):
403
 
        from bzrlib.osutils import format_date
404
 
 
 
709
 
 
710
    supports_delta = True
 
711
    supports_single_merge_revision = True
 
712
 
 
713
    def log_revision(self, revision):
405
714
        to_file = self.to_file
406
 
        date_str = format_date(rev.timestamp, rev.timezone or 0,
407
 
                            self.show_timezone)
408
 
        print >>to_file, "%5d %s\t%s" % (revno, self.short_committer(rev),
409
 
                format_date(rev.timestamp, rev.timezone or 0,
 
715
        is_merge = ''
 
716
        if len(revision.rev.parent_ids) > 1:
 
717
            is_merge = ' [merge]'
 
718
        to_file.write("%5s %s\t%s%s\n" % (revision.revno,
 
719
                self.short_author(revision.rev),
 
720
                format_date(revision.rev.timestamp,
 
721
                            revision.rev.timezone or 0,
410
722
                            self.show_timezone, date_fmt="%Y-%m-%d",
411
 
                           show_offset=False))
 
723
                            show_offset=False),
 
724
                is_merge))
412
725
        if self.show_ids:
413
 
            print >>to_file,  '      revision-id:', rev.revision_id
414
 
        if not rev.message:
415
 
            print >>to_file,  '      (no message)'
 
726
            to_file.write('      revision-id:%s\n' % (revision.rev.revision_id,))
 
727
        if not revision.rev.message:
 
728
            to_file.write('      (no message)\n')
416
729
        else:
417
 
            message = rev.message.rstrip('\r\n')
 
730
            message = revision.rev.message.rstrip('\r\n')
418
731
            for l in message.split('\n'):
419
 
                print >>to_file,  '      ' + l
 
732
                to_file.write('      %s\n' % (l,))
420
733
 
421
734
        # TODO: Why not show the modified files in a shorter form as
422
735
        # well? rewrap them single lines of appropriate length
423
 
        if delta != None:
424
 
            delta.show(to_file, self.show_ids)
425
 
        print >>to_file, ''
 
736
        if revision.delta is not None:
 
737
            revision.delta.show(to_file, self.show_ids)
 
738
        to_file.write('\n')
426
739
 
427
740
 
428
741
class LineLogFormatter(LogFormatter):
 
742
 
 
743
    supports_single_merge_revision = True
 
744
 
 
745
    def __init__(self, *args, **kwargs):
 
746
        super(LineLogFormatter, self).__init__(*args, **kwargs)
 
747
        self._max_chars = terminal_width() - 1
 
748
 
429
749
    def truncate(self, str, max_len):
430
750
        if len(str) <= max_len:
431
751
            return str
432
752
        return str[:max_len-3]+'...'
433
753
 
434
754
    def date_string(self, rev):
435
 
        from bzrlib.osutils import format_date
436
755
        return format_date(rev.timestamp, rev.timezone or 0, 
437
756
                           self.show_timezone, date_fmt="%Y-%m-%d",
438
757
                           show_offset=False)
443
762
        else:
444
763
            return rev.message
445
764
 
446
 
    def show(self, revno, rev, delta):
447
 
        from bzrlib.osutils import terminal_width
448
 
        print >> self.to_file, self.log_string(revno, rev, terminal_width()-1)
 
765
    def log_revision(self, revision):
 
766
        self.to_file.write(self.log_string(revision.revno, revision.rev,
 
767
                                              self._max_chars))
 
768
        self.to_file.write('\n')
449
769
 
450
770
    def log_string(self, revno, rev, max_chars):
451
771
        """Format log info into one string. Truncate tail of string
458
778
        out = []
459
779
        if revno:
460
780
            # show revno only when is not None
461
 
            out.append("%d:" % revno)
462
 
        out.append(self.truncate(self.short_committer(rev), 20))
 
781
            out.append("%s:" % revno)
 
782
        out.append(self.truncate(self.short_author(rev), 20))
463
783
        out.append(self.date_string(rev))
464
784
        out.append(rev.get_summary())
465
785
        return self.truncate(" ".join(out).rstrip('\n'), max_chars)
469
789
    lf = LineLogFormatter(None)
470
790
    return lf.log_string(None, rev, max_chars)
471
791
 
472
 
FORMATTERS = {
473
 
              'long': LongLogFormatter,
474
 
              'short': ShortLogFormatter,
475
 
              'line': LineLogFormatter,
476
 
              }
 
792
 
 
793
class LogFormatterRegistry(registry.Registry):
 
794
    """Registry for log formatters"""
 
795
 
 
796
    def make_formatter(self, name, *args, **kwargs):
 
797
        """Construct a formatter from arguments.
 
798
 
 
799
        :param name: Name of the formatter to construct.  'short', 'long' and
 
800
            'line' are built-in.
 
801
        """
 
802
        return self.get(name)(*args, **kwargs)
 
803
 
 
804
    def get_default(self, branch):
 
805
        return self.get(branch.get_config().log_format())
 
806
 
 
807
 
 
808
log_formatter_registry = LogFormatterRegistry()
 
809
 
 
810
 
 
811
log_formatter_registry.register('short', ShortLogFormatter,
 
812
                                'Moderately short log format')
 
813
log_formatter_registry.register('long', LongLogFormatter,
 
814
                                'Detailed log format')
 
815
log_formatter_registry.register('line', LineLogFormatter,
 
816
                                'Log format with one line per revision')
 
817
 
477
818
 
478
819
def register_formatter(name, formatter):
479
 
    FORMATTERS[name] = formatter
 
820
    log_formatter_registry.register(name, formatter)
 
821
 
480
822
 
481
823
def log_formatter(name, *args, **kwargs):
482
824
    """Construct a formatter from arguments.
484
826
    name -- Name of the formatter to construct; currently 'long', 'short' and
485
827
        'line' are supported.
486
828
    """
487
 
    from bzrlib.errors import BzrCommandError
488
829
    try:
489
 
        return FORMATTERS[name](*args, **kwargs)
 
830
        return log_formatter_registry.make_formatter(name, *args, **kwargs)
490
831
    except KeyError:
491
 
        raise BzrCommandError("unknown log formatter: %r" % name)
 
832
        raise errors.BzrCommandError("unknown log formatter: %r" % name)
 
833
 
492
834
 
493
835
def show_one_log(revno, rev, delta, verbose, to_file, show_timezone):
494
 
    # deprecated; for compatability
 
836
    # deprecated; for compatibility
495
837
    lf = LongLogFormatter(to_file=to_file, show_timezone=show_timezone)
496
838
    lf.show(revno, rev, delta)
497
839
 
498
 
def show_changed_revisions(branch, old_rh, new_rh, to_file=None, log_format='long'):
 
840
 
 
841
def show_changed_revisions(branch, old_rh, new_rh, to_file=None,
 
842
                           log_format='long'):
499
843
    """Show the change in revision history comparing the old revision history to the new one.
500
844
 
501
845
    :param branch: The branch where the revisions exist
504
848
    :param to_file: A file to write the results to. If None, stdout will be used
505
849
    """
506
850
    if to_file is None:
507
 
        import sys
508
 
        import codecs
509
 
        import bzrlib
510
 
        to_file = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
 
851
        to_file = codecs.getwriter(get_terminal_encoding())(sys.stdout,
 
852
            errors='replace')
511
853
    lf = log_formatter(log_format,
512
854
                       show_ids=False,
513
855
                       to_file=to_file,
535
877
        to_file.write('\nRemoved Revisions:\n')
536
878
        for i in range(base_idx, len(old_rh)):
537
879
            rev = branch.repository.get_revision(old_rh[i])
538
 
            lf.show(i+1, rev, None)
 
880
            lr = LogRevision(rev, i+1, 0, None)
 
881
            lf.log_revision(lr)
539
882
        to_file.write('*'*60)
540
883
        to_file.write('\n\n')
541
884
    if base_idx < len(new_rh):
543
886
        show_log(branch,
544
887
                 lf,
545
888
                 None,
546
 
                 verbose=True,
 
889
                 verbose=False,
547
890
                 direction='forward',
548
891
                 start_revision=base_idx+1,
549
892
                 end_revision=len(new_rh),
550
893
                 search=None)
551
894
 
 
895
 
 
896
properties_handler_registry = registry.Registry()