~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/serializer/v4.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-05 14:12:23 UTC
  • mto: This revision was merged to the branch mainline in revision 6348.
  • Revision ID: jelmer@samba.org-20111205141223-8qxae4h37satlzgq
Move more functionality to vf_search.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
19
19
import re
20
20
 
21
21
from bzrlib import (
22
 
    diff,
23
22
    errors,
24
23
    iterablefile,
25
24
    lru_cache,
30
29
    serializer,
31
30
    trace,
32
31
    ui,
 
32
    versionedfile as _mod_versionedfile,
33
33
    )
34
34
from bzrlib.bundle import bundle_data, serializer as bundle_serializer
 
35
from bzrlib.i18n import ngettext
35
36
from bzrlib import bencode
36
37
 
37
38
 
 
39
class _MPDiffInventoryGenerator(_mod_versionedfile._MPDiffGenerator):
 
40
    """Generate Inventory diffs serialized inventories."""
 
41
 
 
42
    def __init__(self, repo, inventory_keys):
 
43
        super(_MPDiffInventoryGenerator, self).__init__(repo.inventories,
 
44
            inventory_keys)
 
45
        self.repo = repo
 
46
        self.sha1s = {}
 
47
 
 
48
    def iter_diffs(self):
 
49
        """Compute the diffs one at a time."""
 
50
        # This is instead of compute_diffs() since we guarantee our ordering of
 
51
        # inventories, we don't have to do any buffering
 
52
        self._find_needed_keys()
 
53
        # We actually use a slightly different ordering. We grab all of the
 
54
        # parents first, and then grab the ordered requests.
 
55
        needed_ids = [k[-1] for k in self.present_parents]
 
56
        needed_ids.extend([k[-1] for k in self.ordered_keys])
 
57
        inv_to_str = self.repo._serializer.write_inventory_to_string
 
58
        for inv in self.repo.iter_inventories(needed_ids):
 
59
            revision_id = inv.revision_id
 
60
            key = (revision_id,)
 
61
            if key in self.present_parents:
 
62
                # Not a key we will transmit, which is a shame, since because
 
63
                # of that bundles don't work with stacked branches
 
64
                parent_ids = None
 
65
            else:
 
66
                parent_ids = [k[-1] for k in self.parent_map[key]]
 
67
            as_bytes = inv_to_str(inv)
 
68
            self._process_one_record(key, (as_bytes,))
 
69
            if parent_ids is None:
 
70
                continue
 
71
            diff = self.diffs.pop(key)
 
72
            sha1 = osutils.sha_string(as_bytes)
 
73
            yield revision_id, parent_ids, sha1, diff
 
74
 
 
75
 
38
76
class BundleWriter(object):
39
77
    """Writer for bundle-format files.
40
78
 
284
322
 
285
323
    def do_write(self):
286
324
        """Write all data to the bundle"""
287
 
        trace.note('Bundling %d revision(s).', len(self.revision_ids))
 
325
        trace.note(ngettext('Bundling %d revision.', 'Bundling %d revisions.',
 
326
                            len(self.revision_ids)), len(self.revision_ids))
288
327
        self.repository.lock_read()
289
328
        try:
290
329
            self.bundle.begin()
348
387
        the other side.
349
388
        """
350
389
        inventory_key_order = [(r,) for r in revision_order]
351
 
        parent_map = self.repository.inventories.get_parent_map(
352
 
                            inventory_key_order)
353
 
        missing_keys = set(inventory_key_order).difference(parent_map)
354
 
        if missing_keys:
355
 
            raise errors.RevisionNotPresent(list(missing_keys)[0],
356
 
                                            self.repository.inventories)
357
 
        inv_to_str = self.repository._serializer.write_inventory_to_string
358
 
        # Make sure that we grab the parent texts first
359
 
        just_parents = set()
360
 
        map(just_parents.update, parent_map.itervalues())
361
 
        just_parents.difference_update(parent_map)
362
 
        # Ignore ghost parents
363
 
        present_parents = self.repository.inventories.get_parent_map(
364
 
                            just_parents)
365
 
        ghost_keys = just_parents.difference(present_parents)
366
 
        needed_inventories = list(present_parents) + inventory_key_order
367
 
        needed_inventories = [k[-1] for k in needed_inventories]
368
 
        all_lines = {}
369
 
        for inv in self.repository.iter_inventories(needed_inventories):
370
 
            revision_id = inv.revision_id
371
 
            key = (revision_id,)
372
 
            as_bytes = inv_to_str(inv)
373
 
            # The sha1 is validated as the xml/textual form, not as the
374
 
            # form-in-the-repository
375
 
            sha1 = osutils.sha_string(as_bytes)
376
 
            as_lines = osutils.split_lines(as_bytes)
377
 
            del as_bytes
378
 
            all_lines[key] = as_lines
379
 
            if key in just_parents:
380
 
                # We don't transmit those entries
381
 
                continue
382
 
            # Create an mpdiff for this text, and add it to the output
383
 
            parent_keys = parent_map[key]
384
 
            # See the comment in VF.make_mpdiffs about how this effects
385
 
            # ordering when there are ghosts present. I think we have a latent
386
 
            # bug
387
 
            parent_lines = [all_lines[p_key] for p_key in parent_keys
388
 
                            if p_key not in ghost_keys]
389
 
            diff = multiparent.MultiParent.from_lines(
390
 
                as_lines, parent_lines)
 
390
        generator = _MPDiffInventoryGenerator(self.repository,
 
391
                                              inventory_key_order)
 
392
        for revision_id, parent_ids, sha1, diff in generator.iter_diffs():
391
393
            text = ''.join(diff.to_patch())
392
 
            parent_ids = [k[-1] for k in parent_keys]
393
394
            self.bundle.add_multiparent_record(text, sha1, parent_ids,
394
395
                                               'inventory', revision_id, None)
395
396