~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/history.py

  • Committer: Michael Hudson
  • Date: 2007-10-29 16:19:30 UTC
  • mto: This revision was merged to the branch mainline in revision 141.
  • Revision ID: michael.hudson@canonical.com-20071029161930-oxqrd4rd8j1oz3hx
add do nothing check target

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
 
# Copyright (C) 2008  Canonical Ltd. 
3
 
#                     (Authored by Martin Albisetti <argentina@gmail.com>)
4
2
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
5
3
# Copyright (C) 2006  Goffredo Baroncelli <kreijack@inwind.it>
6
4
# Copyright (C) 2005  Jake Edge <jake@edge2.net>
29
27
 
30
28
 
31
29
import bisect
 
30
import cgi
32
31
import datetime
33
32
import logging
 
33
import os
 
34
import posixpath
34
35
import re
 
36
import shelve
 
37
import sys
35
38
import textwrap
36
39
import threading
37
40
import time
38
41
from StringIO import StringIO
39
42
 
40
 
from loggerhead import search
41
43
from loggerhead import util
42
 
from loggerhead.wholehistory import compute_whole_history_data
 
44
from loggerhead.util import decorator
43
45
 
44
46
import bzrlib
 
47
import bzrlib.annotate
45
48
import bzrlib.branch
 
49
import bzrlib.bundle.serializer
 
50
import bzrlib.decorators
46
51
import bzrlib.diff
47
52
import bzrlib.errors
48
53
import bzrlib.progress
49
54
import bzrlib.revision
 
55
import bzrlib.textfile
50
56
import bzrlib.tsort
51
57
import bzrlib.ui
52
58
 
 
59
 
 
60
with_branch_lock = util.with_lock('_lock', 'branch')
 
61
 
 
62
 
53
63
# bzrlib's UIFactory is not thread-safe
54
64
uihack = threading.local()
55
65
 
140
150
 
141
151
    # Make short form of commit message.
142
152
    short_message = message[0]
143
 
    if len(short_message) > 60:
144
 
        short_message = short_message[:60] + '...'
 
153
    if len(short_message) > 80:
 
154
        short_message = short_message[:80] + '...'
145
155
 
146
156
    return message, short_message
147
157
 
174
184
 
175
185
 
176
186
class History (object):
177
 
    """Decorate a branch to provide information for rendering.
178
 
 
179
 
    History objects are expected to be short lived -- when serving a request
180
 
    for a particular branch, open it, read-lock it, wrap a History object
181
 
    around it, serve the request, throw the History object away, unlock the
182
 
    branch and throw it away.
183
 
 
184
 
    :ivar _file_change_cache: xx
185
 
    """
186
 
 
187
 
    def __init__(self, branch, whole_history_data_cache):
188
 
        assert branch.is_locked(), (
189
 
            "Can only construct a History object with a read-locked branch.")
 
187
 
 
188
    def __init__(self):
 
189
        self._change_cache = None
190
190
        self._file_change_cache = None
 
191
        self._index = None
 
192
        self._lock = threading.RLock()
 
193
 
 
194
    @classmethod
 
195
    def from_branch(cls, branch, name=None):
 
196
        z = time.time()
 
197
        self = cls()
191
198
        self._branch = branch
192
 
        self.log = logging.getLogger('loggerhead.%s' % (branch.nick,))
193
 
 
194
 
        self.last_revid = branch.last_revision()
195
 
 
196
 
        whole_history_data = whole_history_data_cache.get(self.last_revid)
197
 
        if whole_history_data is None:
198
 
            whole_history_data = compute_whole_history_data(branch)
199
 
            whole_history_data_cache[self.last_revid] = whole_history_data
200
 
 
201
 
        (self._revision_graph, self._full_history, self._revision_info,
202
 
         self._revno_revid, self._merge_sort, self._where_merged
203
 
         ) = whole_history_data
 
199
        self._last_revid = self._branch.last_revision()
 
200
        if self._last_revid is not None:
 
201
            self._revision_graph = branch.repository.get_revision_graph(self._last_revid)
 
202
        else:
 
203
            self._revision_graph = {}
 
204
 
 
205
        if name is None:
 
206
            name = self._branch.nick
 
207
        self._name = name
 
208
        self.log = logging.getLogger('loggerhead.%s' % (name,))
 
209
 
 
210
        self._full_history = []
 
211
        self._revision_info = {}
 
212
        self._revno_revid = {}
 
213
        self._merge_sort = bzrlib.tsort.merge_sort(self._revision_graph, self._last_revid, generate_revno=True)
 
214
        for (seq, revid, merge_depth, revno, end_of_merge) in self._merge_sort:
 
215
            self._full_history.append(revid)
 
216
            revno_str = '.'.join(str(n) for n in revno)
 
217
            self._revno_revid[revno_str] = revid
 
218
            self._revision_info[revid] = (seq, revid, merge_depth, revno_str, end_of_merge)
 
219
 
 
220
        # cache merge info
 
221
        self._where_merged = {}
 
222
        for revid in self._revision_graph.keys():
 
223
            if not revid in self._full_history:
 
224
                continue
 
225
            for parent in self._revision_graph[revid]:
 
226
                self._where_merged.setdefault(parent, set()).add(revid)
 
227
 
 
228
        self.log.info('built revision graph cache: %r secs' % (time.time() - z,))
 
229
        return self
 
230
 
 
231
    @classmethod
 
232
    def from_folder(cls, path, name=None):
 
233
        b = bzrlib.branch.Branch.open(path)
 
234
        return cls.from_branch(b, name)
 
235
 
 
236
    @with_branch_lock
 
237
    def out_of_date(self):
 
238
        # the branch may have been upgraded on disk, in which case we're stale.
 
239
        if self._branch.__class__ is not \
 
240
               bzrlib.branch.Branch.open(self._branch.base).__class__:
 
241
            return True
 
242
        return self._branch.last_revision() != self._last_revid
 
243
 
 
244
    def use_cache(self, cache):
 
245
        self._change_cache = cache
204
246
 
205
247
    def use_file_cache(self, cache):
206
248
        self._file_change_cache = cache
207
249
 
208
 
    @property
209
 
    def has_revisions(self):
210
 
        return not bzrlib.revision.is_null(self.last_revid)
211
 
 
 
250
    def use_search_index(self, index):
 
251
        self._index = index
 
252
 
 
253
    @with_branch_lock
 
254
    def detach(self):
 
255
        # called when a new history object needs to be created, because the
 
256
        # branch history has changed.  we need to immediately close and stop
 
257
        # using our caches, because a new history object will be created to
 
258
        # replace us, using the same cache files.
 
259
        # (may also be called during server shutdown.)
 
260
        if self._change_cache is not None:
 
261
            self._change_cache.close()
 
262
            self._change_cache = None
 
263
        if self._index is not None:
 
264
            self._index.close()
 
265
            self._index = None
 
266
 
 
267
    def flush_cache(self):
 
268
        if self._change_cache is None:
 
269
            return
 
270
        self._change_cache.flush()
 
271
 
 
272
    def check_rebuild(self):
 
273
        if self._change_cache is not None:
 
274
            self._change_cache.check_rebuild()
 
275
        if self._index is not None:
 
276
            self._index.check_rebuild()
 
277
 
 
278
    last_revid = property(lambda self: self._last_revid, None, None)
 
279
 
 
280
    @with_branch_lock
212
281
    def get_config(self):
213
282
        return self._branch.get_config()
214
283
 
219
288
        seq, revid, merge_depth, revno_str, end_of_merge = self._revision_info[revid]
220
289
        return revno_str
221
290
 
222
 
    def get_revids_from(self, revid_list, start_revid):
223
 
        """
224
 
        Yield the mainline (wrt start_revid) revisions that merged each
225
 
        revid in revid_list.
226
 
        """
227
 
        if revid_list is None:
228
 
            revid_list = self._full_history
229
 
        revid_set = set(revid_list)
230
 
        revid = start_revid
231
 
        def introduced_revisions(revid):
232
 
            r = set([revid])
233
 
            seq, revid, md, revno, end_of_merge = self._revision_info[revid]
234
 
            i = seq + 1
235
 
            while i < len(self._merge_sort) and self._merge_sort[i][2] > md:
236
 
                r.add(self._merge_sort[i][1])
237
 
                i += 1
238
 
            return r
239
 
        while 1:
240
 
            if bzrlib.revision.is_null(revid):
241
 
                return
242
 
            if introduced_revisions(revid) & revid_set:
 
291
    def get_revision_history(self):
 
292
        return self._full_history
 
293
 
 
294
    def get_revids_from(self, revid_list, revid):
 
295
        """
 
296
        given a list of revision ids, yield revisions in graph order,
 
297
        starting from revid.  the list can be None if you just want to travel
 
298
        across all revisions.
 
299
        """
 
300
        while True:
 
301
            if (revid_list is None) or (revid in revid_list):
243
302
                yield revid
 
303
            if not self._revision_graph.has_key(revid):
 
304
                return
244
305
            parents = self._revision_graph[revid]
245
306
            if len(parents) == 0:
246
307
                return
247
308
            revid = parents[0]
248
309
 
 
310
    @with_branch_lock
249
311
    def get_short_revision_history_by_fileid(self, file_id):
250
312
        # wow.  is this really the only way we can get this list?  by
251
313
        # man-handling the weave store directly? :-0
252
314
        # FIXME: would be awesome if we could get, for a folder, the list of
253
315
        # revisions where items within that folder changed.
254
 
        possible_keys = [(file_id, revid) for revid in self._full_history]
255
 
        existing_keys = self._branch.repository.texts.get_parent_map(possible_keys)
256
 
        return [revid for _, revid in existing_keys.iterkeys()]
 
316
        w = self._branch.repository.weave_store.get_weave(file_id, self._branch.repository.get_transaction())
 
317
        w_revids = w.versions()
 
318
        revids = [r for r in self._full_history if r in w_revids]
 
319
        return revids
257
320
 
 
321
    @with_branch_lock
258
322
    def get_revision_history_since(self, revid_list, date):
259
323
        # if a user asks for revisions starting at 01-sep, they mean inclusive,
260
324
        # so start at midnight on 02-sep.
268
332
        index = -index
269
333
        return revid_list[index:]
270
334
 
 
335
    @with_branch_lock
 
336
    def get_revision_history_matching(self, revid_list, text):
 
337
        self.log.debug('searching %d revisions for %r', len(revid_list), text)
 
338
        z = time.time()
 
339
        # this is going to be painfully slow. :(
 
340
        out = []
 
341
        text = text.lower()
 
342
        for revid in revid_list:
 
343
            change = self.get_changes([ revid ])[0]
 
344
            if text in change.comment.lower():
 
345
                out.append(revid)
 
346
        self.log.debug('searched %d revisions for %r in %r secs', len(revid_list), text, time.time() - z)
 
347
        return out
 
348
 
 
349
    def get_revision_history_matching_indexed(self, revid_list, text):
 
350
        self.log.debug('searching %d revisions for %r', len(revid_list), text)
 
351
        z = time.time()
 
352
        if self._index is None:
 
353
            return self.get_revision_history_matching(revid_list, text)
 
354
        out = self._index.find(text, revid_list)
 
355
        self.log.debug('searched %d revisions for %r in %r secs: %d results', len(revid_list), text, time.time() - z, len(out))
 
356
        # put them in some coherent order :)
 
357
        out = [r for r in self._full_history if r in out]
 
358
        return out
 
359
 
 
360
    @with_branch_lock
271
361
    def get_search_revid_list(self, query, revid_list):
272
362
        """
273
363
        given a "quick-search" query, try a few obvious possible meanings:
306
396
        if date is not None:
307
397
            if revid_list is None:
308
398
                # if no limit to the query was given, search only the direct-parent path.
309
 
                revid_list = list(self.get_revids_from(None, self.last_revid))
 
399
                revid_list = list(self.get_revids_from(None, self._last_revid))
310
400
            return self.get_revision_history_since(revid_list, date)
311
401
 
 
402
        # check comment fields.
 
403
        if revid_list is None:
 
404
            revid_list = self._full_history
 
405
        return self.get_revision_history_matching_indexed(revid_list, query)
 
406
 
312
407
    revno_re = re.compile(r'^[\d\.]+$')
313
408
    # the date regex are without a final '$' so that queries like
314
409
    # "2006-11-30 12:15" still mostly work.  (i think it's better to give
322
417
        if revid is None:
323
418
            return revid
324
419
        if revid == 'head:':
325
 
            return self.last_revid
 
420
            return self._last_revid
326
421
        if self.revno_re.match(revid):
327
422
            revid = self._revno_revid[revid]
328
423
        return revid
329
424
 
 
425
    @with_branch_lock
330
426
    def get_file_view(self, revid, file_id):
331
427
        """
332
428
        Given a revid and optional path, return a (revlist, revid) for
336
432
        If file_id is None, the entire revision history is the list scope.
337
433
        """
338
434
        if revid is None:
339
 
            revid = self.last_revid
 
435
            revid = self._last_revid
340
436
        if file_id is not None:
341
437
            # since revid is 'start_revid', possibly should start the path
342
438
            # tracing from revid... FIXME
346
442
            revlist = list(self.get_revids_from(None, revid))
347
443
        return revlist
348
444
 
 
445
    @with_branch_lock
349
446
    def get_view(self, revid, start_revid, file_id, query=None):
350
447
        """
351
448
        use the URL parameters (revid, start_revid, file_id, and query) to
352
449
        determine the revision list we're viewing (start_revid, file_id, query)
353
450
        and where we are in it (revid).
354
451
 
355
 
            - if a query is given, we're viewing query results.
356
 
            - if a file_id is given, we're viewing revisions for a specific
357
 
              file.
358
 
            - if a start_revid is given, we're viewing the branch from a
359
 
              specific revision up the tree.
360
 
 
361
 
        these may be combined to view revisions for a specific file, from
362
 
        a specific revision, with a specific search query.
363
 
 
364
 
        returns a new (revid, start_revid, revid_list) where:
 
452
        if a query is given, we're viewing query results.
 
453
        if a file_id is given, we're viewing revisions for a specific file.
 
454
        if a start_revid is given, we're viewing the branch from a
 
455
            specific revision up the tree.
 
456
        (these may be combined to view revisions for a specific file, from
 
457
            a specific revision, with a specific search query.)
 
458
 
 
459
        returns a new (revid, start_revid, revid_list, scan_list) where:
365
460
 
366
461
            - revid: current position within the view
367
462
            - start_revid: starting revision of this view
371
466
        contain vital context for future url navigation.
372
467
        """
373
468
        if start_revid is None:
374
 
            start_revid = self.last_revid
 
469
            start_revid = self._last_revid
375
470
 
376
471
        if query is None:
377
472
            revid_list = self.get_file_view(start_revid, file_id)
380
475
            if revid not in revid_list:
381
476
                # if the given revid is not in the revlist, use a revlist that
382
477
                # starts at the given revid.
383
 
                revid_list = self.get_file_view(revid, file_id)
 
478
                revid_list= self.get_file_view(revid, file_id)
384
479
                start_revid = revid
385
480
            return revid, start_revid, revid_list
386
481
 
389
484
            revid_list = self.get_file_view(start_revid, file_id)
390
485
        else:
391
486
            revid_list = None
392
 
        revid_list = search.search_revisions(self._branch, query)
393
 
        if revid_list and len(revid_list) > 0:
 
487
 
 
488
        revid_list = self.get_search_revid_list(query, revid_list)
 
489
        if len(revid_list) > 0:
394
490
            if revid not in revid_list:
395
491
                revid = revid_list[0]
396
492
            return revid, start_revid, revid_list
397
493
        else:
398
 
            # XXX: This should return a message saying that the search could
399
 
            # not be completed due to either missing the plugin or missing a
400
 
            # search index.
 
494
            # no results
401
495
            return None, None, []
402
496
 
 
497
    @with_branch_lock
403
498
    def get_inventory(self, revid):
404
499
        return self._branch.repository.get_revision_inventory(revid)
405
500
 
 
501
    @with_branch_lock
406
502
    def get_path(self, revid, file_id):
407
503
        if (file_id is None) or (file_id == ''):
408
504
            return ''
411
507
            path = '/' + path
412
508
        return path
413
509
 
 
510
    @with_branch_lock
414
511
    def get_file_id(self, revid, path):
415
512
        if (len(path) > 0) and not path.startswith('/'):
416
513
            path = '/' + path
417
514
        return self._branch.repository.get_revision_inventory(revid).path2id(path)
418
515
 
 
516
 
419
517
    def get_merge_point_list(self, revid):
420
518
        """
421
519
        Return the list of revids that have merged this node.
489
587
                else:
490
588
                    p.branch_nick = '(missing)'
491
589
 
 
590
    @with_branch_lock
492
591
    def get_changes(self, revid_list):
493
 
        """Return a list of changes objects for the given revids.
494
 
 
495
 
        Revisions not present and NULL_REVISION will be ignored.
496
 
        """
497
 
        changes = self.get_changes_uncached(revid_list)
 
592
        if self._change_cache is None:
 
593
            changes = self.get_changes_uncached(revid_list)
 
594
        else:
 
595
            changes = self._change_cache.get_changes(revid_list)
498
596
        if len(changes) == 0:
499
597
            return changes
500
598
 
503
601
        for change in changes:
504
602
            merge_revids = self.simplify_merge_point_list(self.get_merge_point_list(change.revid))
505
603
            change.merge_points = [util.Container(revid=r, revno=self.get_revno(r)) for r in merge_revids]
506
 
            if len(change.parents) > 0:
507
 
                change.parents = [util.Container(revid=r, 
508
 
                    revno=self.get_revno(r)) for r in change.parents]
509
604
            change.revno = self.get_revno(change.revid)
510
605
 
511
606
        parity = 0
515
610
 
516
611
        return changes
517
612
 
518
 
    def get_changes_uncached(self, revid_list):
519
 
        # FIXME: deprecated method in getting a null revision
520
 
        revid_list = filter(lambda revid: not bzrlib.revision.is_null(revid),
521
 
                            revid_list)
522
 
        parent_map = self._branch.repository.get_graph().get_parent_map(revid_list)
523
 
        # We need to return the answer in the same order as the input,
524
 
        # less any ghosts.
525
 
        present_revids = [revid for revid in revid_list
526
 
                          if revid in parent_map]
527
 
        rev_list = self._branch.repository.get_revisions(present_revids)
528
 
 
529
 
        return [self._change_from_revision(rev) for rev in rev_list]
530
 
 
531
 
    def _get_deltas_for_revisions_with_trees(self, revisions):
532
 
        """Produce a list of revision deltas.
 
613
    # alright, let's profile this sucka.
 
614
    def _get_changes_profiled(self, revid_list, get_diffs=False):
 
615
        from loggerhead.lsprof import profile
 
616
        import cPickle
 
617
        ret, stats = profile(self.get_changes_uncached, revid_list, get_diffs)
 
618
        stats.sort()
 
619
        stats.freeze()
 
620
        cPickle.dump(stats, open('lsprof.stats', 'w'), 2)
 
621
        self.log.info('lsprof complete!')
 
622
        return ret
 
623
 
 
624
    def _get_deltas_for_revisions_with_trees(self, entries):
 
625
        """Produce a generator of revision deltas.
533
626
 
534
627
        Note that the input is a sequence of REVISIONS, not revision_ids.
535
628
        Trees will be held in memory until the generator exits.
536
629
        Each delta is relative to the revision's lefthand predecessor.
537
 
        (This is copied from bzrlib.)
538
630
        """
539
631
        required_trees = set()
540
 
        for revision in revisions:
541
 
            required_trees.add(revision.revid)
542
 
            required_trees.update([p.revid for p in revision.parents[:1]])
 
632
        for entry in entries:
 
633
            required_trees.add(entry.revid)
 
634
            required_trees.update([p.revid for p in entry.parents[:1]])
543
635
        trees = dict((t.get_revision_id(), t) for
544
636
                     t in self._branch.repository.revision_trees(required_trees))
545
637
        ret = []
546
638
        self._branch.repository.lock_read()
547
639
        try:
548
 
            for revision in revisions:
549
 
                if not revision.parents:
 
640
            for entry in entries:
 
641
                if not entry.parents:
550
642
                    old_tree = self._branch.repository.revision_tree(
551
643
                        bzrlib.revision.NULL_REVISION)
552
644
                else:
553
 
                    old_tree = trees[revision.parents[0].revid]
554
 
                tree = trees[revision.revid]
 
645
                    old_tree = trees[entry.parents[0].revid]
 
646
                tree = trees[entry.revid]
555
647
                ret.append(tree.changes_from(old_tree))
556
648
            return ret
557
649
        finally:
558
650
            self._branch.repository.unlock()
559
651
 
560
 
    def _change_from_revision(self, revision):
561
 
        """
562
 
        Given a bzrlib Revision, return a processed "change" for use in
563
 
        templates.
564
 
        """
 
652
    def entry_from_revision(self, revision):
565
653
        commit_time = datetime.datetime.fromtimestamp(revision.timestamp)
566
654
 
567
655
        parents = [util.Container(revid=r, revno=self.get_revno(r)) for r in revision.parent_ids]
571
659
        entry = {
572
660
            'revid': revision.revision_id,
573
661
            'date': commit_time,
574
 
            'author': revision.get_apparent_author(),
 
662
            'author': revision.committer,
575
663
            'branch_nick': revision.properties.get('branch-nick', None),
576
664
            'short_comment': short_message,
577
665
            'comment': revision.message,
578
666
            'comment_clean': [util.html_clean(s) for s in message],
579
 
            'parents': revision.parent_ids,
 
667
            'parents': parents,
580
668
        }
581
669
        return util.Container(entry)
582
670
 
 
671
    @with_branch_lock
 
672
    def get_changes_uncached(self, revid_list):
 
673
        # Because we may loop and call get_revisions multiple times (to throw
 
674
        # out dud revids), we grab a read lock.
 
675
        self._branch.lock_read()
 
676
        try:
 
677
            while True:
 
678
                try:
 
679
                    rev_list = self._branch.repository.get_revisions(revid_list)
 
680
                except (KeyError, bzrlib.errors.NoSuchRevision), e:
 
681
                    # this sometimes happens with arch-converted branches.
 
682
                    # i don't know why. :(
 
683
                    self.log.debug('No such revision (skipping): %s', e)
 
684
                    revid_list.remove(e.revision)
 
685
                else:
 
686
                    break
 
687
 
 
688
            return [self.entry_from_revision(rev) for rev in rev_list]
 
689
        finally:
 
690
            self._branch.unlock()
 
691
 
583
692
    def get_file_changes_uncached(self, entries):
584
693
        delta_list = self._get_deltas_for_revisions_with_trees(entries)
585
694
 
586
695
        return [self.parse_delta(delta) for delta in delta_list]
587
696
 
 
697
    @with_branch_lock
588
698
    def get_file_changes(self, entries):
589
699
        if self._file_change_cache is None:
590
700
            return self.get_file_changes_uncached(entries)
597
707
        for entry, changes in zip(entries, changes_list):
598
708
            entry.changes = changes
599
709
 
 
710
    @with_branch_lock
600
711
    def get_change_with_diff(self, revid, compare_revid=None):
601
 
        change = self.get_changes([revid])[0]
 
712
        entry = self.get_changes([revid])[0]
602
713
 
603
714
        if compare_revid is None:
604
 
            if change.parents:
605
 
                compare_revid = change.parents[0].revid
 
715
            if entry.parents:
 
716
                compare_revid = entry.parents[0].revid
606
717
            else:
607
718
                compare_revid = 'null:'
608
719
 
610
721
        rev_tree2 = self._branch.repository.revision_tree(revid)
611
722
        delta = rev_tree2.changes_from(rev_tree1)
612
723
 
613
 
        change.changes = self.parse_delta(delta)
614
 
        change.changes.modified = self._parse_diffs(rev_tree1, rev_tree2, delta)
615
 
 
616
 
        return change
617
 
 
 
724
        entry.changes = self.parse_delta(delta)
 
725
 
 
726
        entry.changes.modified = self._parse_diffs(rev_tree1, rev_tree2, delta)
 
727
 
 
728
        return entry
 
729
 
 
730
    @with_branch_lock
618
731
    def get_file(self, file_id, revid):
619
732
        "returns (path, filename, data)"
620
733
        inv = self.get_inventory(revid)
665
778
                    diff = buffer.getvalue()
666
779
            else:
667
780
                diff = ''
668
 
            out.append(util.Container(filename=rich_filename(new_path, kind), file_id=fid, chunks=self._process_diff(diff), raw_diff=diff))
 
781
            out.append(util.Container(filename=rich_filename(new_path, kind), file_id=fid, chunks=self._process_diff(diff)))
669
782
 
670
783
        return out
671
784
 
747
860
            for m in change.changes.modified:
748
861
                m.sbs_chunks = _make_side_by_side(m.chunks)
749
862
 
 
863
    @with_branch_lock
750
864
    def get_filelist(self, inv, file_id, sort_type=None):
751
865
        """
752
866
        return the list of all files (and their attributes) within a given
780
894
            file_list.append(file)
781
895
 
782
896
        if sort_type == 'filename' or sort_type is None:
783
 
            file_list.sort(key=lambda x: x.filename.lower()) # case-insensitive
 
897
            file_list.sort(key=lambda x: x.filename)
784
898
        elif sort_type == 'size':
785
899
            file_list.sort(key=lambda x: x.size)
786
900
        elif sort_type == 'date':
787
901
            file_list.sort(key=lambda x: x.change.date)
788
 
        
789
 
        # Always sort by kind to get directories first
790
 
        file_list.sort(key=lambda x: x.kind != 'directory')
791
902
 
792
903
        parity = 0
793
904
        for file in file_list:
799
910
 
800
911
    _BADCHARS_RE = re.compile(ur'[\x00-\x08\x0b\x0e-\x1f]')
801
912
 
 
913
    @with_branch_lock
802
914
    def annotate_file(self, file_id, revid):
803
915
        z = time.time()
804
916
        lineno = 1
806
918
 
807
919
        file_revid = self.get_inventory(revid)[file_id].revision
808
920
        oldvalues = None
809
 
        tree = self._branch.repository.revision_tree(file_revid)
 
921
 
 
922
        # because we cache revision metadata ourselves, it's actually much
 
923
        # faster to call 'annotate_iter' on the weave directly than it is to
 
924
        # ask bzrlib to annotate for us.
 
925
        w = self._branch.repository.weave_store.get_weave(file_id, self._branch.repository.get_transaction())
 
926
 
810
927
        revid_set = set()
811
 
 
812
 
        for line_revid, text in tree.annotate_iter(file_id):
 
928
        for line_revid, text in w.annotate_iter(file_revid):
813
929
            revid_set.add(line_revid)
814
930
            if self._BADCHARS_RE.match(text):
815
931
                # bail out; this isn't displayable text
817
933
                                     text='(This is a binary file.)',
818
934
                                     change=util.Container())
819
935
                return
820
 
        change_cache = dict([(c.revid, c) \
821
 
                for c in self.get_changes(list(revid_set))])
 
936
        change_cache = dict([(c.revid, c) for c in self.get_changes(list(revid_set))])
822
937
 
823
938
        last_line_revid = None
824
 
        for line_revid, text in tree.annotate_iter(file_id):
 
939
        for line_revid, text in w.annotate_iter(file_revid):
825
940
            if line_revid == last_line_revid:
826
941
                # remember which lines have a new revno and which don't
827
942
                status = 'same'
839
954
            lineno += 1
840
955
 
841
956
        self.log.debug('annotate: %r secs' % (time.time() - z,))
 
957
 
 
958
    @with_branch_lock
 
959
    def get_bundle(self, revid, compare_revid=None):
 
960
        if compare_revid is None:
 
961
            parents = self._revision_graph[revid]
 
962
            if len(parents) > 0:
 
963
                compare_revid = parents[0]
 
964
            else:
 
965
                compare_revid = None
 
966
        s = StringIO()
 
967
        bzrlib.bundle.serializer.write_bundle(self._branch.repository, revid, compare_revid, s)
 
968
        return s.getvalue()
 
969