~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-19 16:09:34 UTC
  • mfrom: (2520.4.135 bzr.mpbundle)
  • Revision ID: pqm@pqm.ubuntu.com-20070719160934-d51fyijw69oto88p
Add new bundle and merge-directive formats

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
_serializers = {}
40
40
 
 
41
v4_string = '4alpha'
 
42
 
 
43
def _get_bundle_header(version):
 
44
    return '%s%s\n' % (BUNDLE_HEADER, version)
41
45
 
42
46
def _get_filename(f):
43
47
    return getattr(f, 'name', '<unknown>')
79
83
    return serializer.read(f)
80
84
 
81
85
 
 
86
def get_serializer(version):
 
87
    try:
 
88
        return _serializers[version](version)
 
89
    except KeyError:
 
90
        raise errors.BundleNotSupported(version, 'unknown bundle format')
 
91
 
 
92
 
82
93
def write(source, revision_ids, f, version=None, forced_bases={}):
83
94
    """Serialize a list of bundles to a filelike object.
84
95
 
88
99
    :param version: [optional] target serialization version
89
100
    """
90
101
 
91
 
    if version not in _serializers:
92
 
        raise errors.BundleNotSupported(version, 'unknown bundle format')
93
 
 
94
 
    serializer = _serializers[version](version)
95
102
    source.lock_read()
96
103
    try:
97
 
        return serializer.write(source, revision_ids, forced_bases, f)
 
104
        return get_serializer(version).write(source, revision_ids,
 
105
                                             forced_bases, f)
98
106
    finally:
99
107
        source.unlock()
100
108
 
101
109
 
102
110
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
103
 
    """"""
104
 
    repository.lock_read()
105
 
    try:
106
 
        return _write_bundle(repository, revision_id, base_revision_id, out,
107
 
                             format)
108
 
    finally:
109
 
        repository.unlock()
110
 
 
111
 
 
112
 
def _write_bundle(repository, revision_id, base_revision_id, out, format):
113
111
    """Write a bundle of revisions.
114
112
 
115
113
    :param repository: Repository containing revisions to serialize.
118
116
         applying the bundle.
119
117
    :param out: Output file.
120
118
    """
121
 
    revision_ids = set(repository.get_ancestry(revision_id, topo_sorted=False))
122
 
    revision_ids.difference_update(repository.get_ancestry(base_revision_id,
123
 
                                   topo_sorted=False))
124
 
    revision_ids = list(repository.get_graph().iter_topo_order(revision_ids))
125
 
    revision_ids.reverse()
126
 
    write(repository, revision_ids, out, format,
127
 
          forced_bases = {revision_id:base_revision_id})
128
 
    return revision_ids
 
119
    repository.lock_read()
 
120
    try:
 
121
        return get_serializer(format).write_bundle(repository, revision_id,
 
122
                                                   base_revision_id, out)
 
123
    finally:
 
124
        repository.unlock()
129
125
 
130
126
 
131
127
class BundleSerializer(object):
144
140
        """
145
141
        raise NotImplementedError
146
142
 
 
143
    def write_bundle(self, repository, target, base, fileobj):
 
144
        """Write the bundle to the supplied file.
 
145
 
 
146
        :param repository: The repository to retrieve revision data from
 
147
        :param target: The revision to provide data for
 
148
        :param base: The most recent of ancestor of the revision that does not
 
149
            need to be included in the bundle
 
150
        :param fileobj: The file to output to
 
151
        """
 
152
        raise NotImplementedError
 
153
 
147
154
    def write(self, source, revision_ids, forced_bases, f):
148
155
        """Write the bundle to the supplied file.
149
156
 
 
157
        DEPRECATED: see write_bundle
150
158
        :param source: A source for revision information
151
159
        :param revision_ids: The list of revision ids to serialize
152
160
        :param forced_bases: A dict of revision -> base that overrides default
154
162
        """
155
163
        raise NotImplementedError
156
164
 
 
165
    def _write_bundle(self, repository, revision_id, base_revision_id, out):
 
166
        """Helper function for translating write_bundle to write"""
 
167
        forced_bases = {revision_id:base_revision_id}
 
168
        if base_revision_id is NULL_REVISION:
 
169
            base_revision_id = None
 
170
        revision_ids = set(repository.get_ancestry(revision_id,
 
171
                           topo_sorted=False))
 
172
        revision_ids.difference_update(repository.get_ancestry(
 
173
            base_revision_id, topo_sorted=False))
 
174
        revision_ids = list(repository.get_graph().iter_topo_order(
 
175
            revision_ids))
 
176
        revision_ids.reverse()
 
177
        self.write(repository, revision_ids, forced_bases, out)
 
178
        return revision_ids
 
179
 
157
180
 
158
181
def register(version, klass, overwrite=False):
159
182
    """Register a BundleSerializer version.
195
218
 
196
219
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
197
220
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
198
 
register_lazy(None, 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
 
221
register_lazy(v4_string, 'bzrlib.bundle.serializer.v4',
 
222
              'BundleSerializerV4')
 
223
register_lazy(None, 'bzrlib.bundle.serializer.v4', 'BundleSerializerV4')
199
224