~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-29 22:03:03 UTC
  • mfrom: (5416.2.6 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100929220303-cr95h8iwtggco721
(mbp) Add 'break-lock --force'

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
30
30
    serializer,
31
31
    trace,
32
32
    ui,
 
33
    versionedfile as _mod_versionedfile,
33
34
    )
34
35
from bzrlib.bundle import bundle_data, serializer as bundle_serializer
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
 
348
386
        the other side.
349
387
        """
350
388
        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)
 
389
        generator = _MPDiffInventoryGenerator(self.repository,
 
390
                                              inventory_key_order)
 
391
        for revision_id, parent_ids, sha1, diff in generator.iter_diffs():
391
392
            text = ''.join(diff.to_patch())
392
 
            parent_ids = [k[-1] for k in parent_keys]
393
393
            self.bundle.add_multiparent_record(text, sha1, parent_ids,
394
394
                                               'inventory', revision_id, None)
395
395