/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 object_store.py

Split out _inventory_to_objects into a function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
112
112
            expected_sha))
113
113
 
114
114
 
 
115
def _inventory_to_objects(inv, parent_invs, parent_invshamaps,
 
116
        unusual_modes, iter_files_bytes):
 
117
    """Iterate over the objects that were introduced in a revision.
 
118
 
 
119
    :param inv: Inventory to process
 
120
    :param parent_invs: parent inventory SHA maps
 
121
    :param parent_invshamaps: parent inventory SHA Map
 
122
    :param unusual_modes: Unusual file modes
 
123
    :param iter_files_bytes: Repository.iter_files_bytes-like callback
 
124
    :return: Yields (path, object) entries
 
125
    """
 
126
    new_trees = {}
 
127
    new_blobs = []
 
128
    shamap = {}
 
129
    for path, ie in inv.entries():
 
130
        if ie.kind in ("file", "symlink"):
 
131
            for (pinv, pinvshamap) in zip(parent_invs, parent_invshamaps):
 
132
                try:
 
133
                    pie = pinv[ie.file_id]
 
134
                except errors.NoSuchId:
 
135
                    pass
 
136
                else:
 
137
                    if (pie.kind == ie.kind and
 
138
                        pie.text_sha1 == ie.text_sha1):
 
139
                        shamap[ie.file_id] = pinvshamap.lookup_blob(
 
140
                            pie.file_id, pie.revision)
 
141
                        break
 
142
            else:
 
143
                if ie.kind == "file":
 
144
                    new_blobs.append((path, ie))
 
145
                else:
 
146
                    blob = symlink_to_blob(ie)
 
147
                    yield path, blob
 
148
                    shamap[ie.file_id] = blob.id
 
149
                new_trees[urlutils.dirname(path)] = ie.parent_id
 
150
        elif ie.kind == "directory":
 
151
            for (pinv, pinvshamap) in zip(parent_invs, parent_invshamaps):
 
152
                try:
 
153
                    pie = pinv[ie.file_id]
 
154
                except errors.NoSuchId:
 
155
                    pass
 
156
                else:
 
157
                    if (pie.kind == ie.kind and 
 
158
                        pie.children.keys() == ie.children.keys()):
 
159
                        try:
 
160
                            shamap[ie.file_id] = pinvshamap.lookup_tree(
 
161
                                ie.file_id)
 
162
                        except NotImplementedError:
 
163
                            pass
 
164
                        else:
 
165
                            break
 
166
            else:
 
167
                new_trees[path] = ie.file_id
 
168
        else:
 
169
            raise AssertionError(ie.kind)
 
170
    
 
171
    for (path, fid), chunks in iter_files_bytes(
 
172
        [(ie.file_id, ie.revision, (path, ie.file_id)) for (path, ie) in new_blobs]):
 
173
        obj = Blob()
 
174
        obj.data = "".join(chunks)
 
175
        yield path, obj
 
176
        shamap[fid] = obj.id
 
177
 
 
178
    for fid in unusual_modes:
 
179
        new_trees[inv.id2path(fid)] = inv[fid].parent_id
 
180
    
 
181
    trees = {}
 
182
    while new_trees:
 
183
        items = new_trees.items()
 
184
        new_trees = {}
 
185
        for path, file_id in items:
 
186
            parent_id = inv[file_id].parent_id
 
187
            if parent_id is not None:
 
188
                parent_path = urlutils.dirname(path)
 
189
                new_trees[parent_path] = parent_id
 
190
            trees[path] = file_id
 
191
 
 
192
    for path in sorted(trees.keys(), reverse=True):
 
193
        ie = inv[trees[path]]
 
194
        assert ie.kind == "directory"
 
195
        obj = directory_to_tree(ie, 
 
196
                lambda ie: shamap[ie.file_id], unusual_modes)
 
197
        if obj is not None:
 
198
            shamap[ie.file_id] = obj.id
 
199
            yield path, obj
 
200
 
 
201
 
115
202
class BazaarObjectStore(BaseObjectStore):
116
203
    """A Git-style object store backed onto a Bazaar repository."""
117
204
 
175
262
                return None
176
263
        return self.mapping.export_commit(rev, tree_sha, parent_lookup)
177
264
 
178
 
    def _inventory_to_objects(self, inv, parent_invs, parent_invshamaps,
179
 
            unusual_modes):
180
 
        """Iterate over the objects that were introduced in a revision.
181
 
 
182
 
        :param inv: Inventory to process
183
 
        :param parent_invs: parent inventory SHA maps
184
 
        :param parent_invshamaps: parent inventory SHA Map
185
 
        :param unusual_modes: Unusual file modes
186
 
        :return: Yields (path, object) entries
187
 
        """
188
 
        new_trees = {}
189
 
        new_blobs = []
190
 
        shamap = {}
191
 
        for path, ie in inv.entries():
192
 
            if ie.kind in ("file", "symlink"):
193
 
                for (pinv, pinvshamap) in zip(parent_invs, parent_invshamaps):
194
 
                    try:
195
 
                        pie = pinv[ie.file_id]
196
 
                    except errors.NoSuchId:
197
 
                        pass
198
 
                    else:
199
 
                        if (pie.kind == ie.kind and
200
 
                            pie.text_sha1 == ie.text_sha1):
201
 
                            shamap[ie.file_id] = pinvshamap.lookup_blob(
202
 
                                pie.file_id, pie.revision)
203
 
                            break
204
 
                else:
205
 
                    if ie.kind == "file":
206
 
                        new_blobs.append(ie)
207
 
                    else:
208
 
                        blob = symlink_to_blob(ie)
209
 
                        yield path, blob
210
 
                        shamap[ie.file_id] = blob.id
211
 
                    new_trees[urlutils.dirname(path)] = ie.parent_id
212
 
            elif ie.kind == "directory":
213
 
                for (pinv, pinvshamap) in zip(parent_invs, parent_invshamaps):
214
 
                    try:
215
 
                        pie = pinv[ie.file_id]
216
 
                    except errors.NoSuchId:
217
 
                        pass
218
 
                    else:
219
 
                        if (pie.kind == ie.kind and 
220
 
                            pie.children.keys() == ie.children.keys()):
221
 
                            try:
222
 
                                shamap[ie.file_id] = pinvshamap.lookup_tree(
223
 
                                    ie.file_id)
224
 
                            except NotImplementedError:
225
 
                                pass
226
 
                            else:
227
 
                                break
228
 
                else:
229
 
                    new_trees[path] = ie.file_id
230
 
            else:
231
 
                raise AssertionError(ie.kind)
232
 
        
233
 
        for ie, chunks in self.repository.iter_files_bytes(
234
 
            [(ie.file_id, ie.revision, ie) for ie in new_blobs]):
235
 
            obj = Blob()
236
 
            obj._text = "".join(chunks)
237
 
            yield path, obj
238
 
            shamap[ie.file_id] = obj.id
239
 
 
240
 
        for fid in unusual_modes:
241
 
            new_trees[inv.id2path(fid)] = inv[fid].parent_id
242
 
        
243
 
        trees = {}
244
 
        while new_trees:
245
 
            items = new_trees.items()
246
 
            new_trees = {}
247
 
            for path, file_id in items:
248
 
                parent_id = inv[file_id].parent_id
249
 
                if parent_id is not None:
250
 
                    parent_path = urlutils.dirname(path)
251
 
                    new_trees[parent_path] = parent_id
252
 
                trees[path] = file_id
253
 
 
254
 
        for path in sorted(trees.keys(), reverse=True):
255
 
            ie = inv[trees[path]]
256
 
            assert ie.kind == "directory"
257
 
            obj = directory_to_tree(ie, 
258
 
                    lambda ie: shamap[ie.file_id], unusual_modes)
259
 
            if obj is not None:
260
 
                shamap[ie.file_id] = obj.id
261
 
                yield path, obj
262
 
 
263
265
    def _revision_to_objects(self, rev, inv):
264
266
        unusual_modes = extract_unusual_modes(rev)
265
267
        present_parents = self.repository.has_revisions(rev.parent_ids)
267
269
            [p for p in rev.parent_ids if p in present_parents])
268
270
        parent_invshamaps = [self._idmap.get_inventory_sha_map(r) for r in rev.parent_ids if r in present_parents]
269
271
        tree_sha = None
270
 
        for path, obj in self._inventory_to_objects(inv, parent_invs,
271
 
                parent_invshamaps, unusual_modes):
 
272
        for path, obj in _inventory_to_objects(inv, parent_invs,
 
273
                parent_invshamaps, unusual_modes,
 
274
                self.repository.iter_files_bytes):
272
275
            yield path, obj
273
276
            if path == "":
274
277
                tree_sha = obj.id
316
319
        """
317
320
        blob = Blob()
318
321
        chunks = self.repository.iter_files_bytes([(fileid, revision, None)]).next()[1]
319
 
        blob._text = "".join(chunks)
 
322
        blob.data = "".join(chunks)
320
323
        if blob.id != expected_sha:
321
324
            # Perhaps it's a symlink ?
322
325
            inv = self.parent_invs_cache.get_inventory(revision)