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

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
        self.path = path
51
51
        self.revision = self._inventory.revision_id
52
52
        self.executable = executable
53
 
        self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
 
53
        self.file_id = self._inventory.fileid_map.lookup_file_id(
 
54
            path.encode('utf-8'))
54
55
 
55
56
    @property
56
57
    def object(self):
67
68
    _git_class = Blob
68
69
 
69
70
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
70
 
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
 
71
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path,
 
72
            basename, executable)
71
73
        self.kind = 'file'
72
74
        self.text_id = None
73
75
        self.symlink_target = None
74
76
 
75
77
    @property
76
78
    def text_sha1(self):
77
 
        return osutils.sha_string(self.object.data)
 
79
        return osutils.sha_strings(self.object.chunked)
78
80
 
79
81
    @property
80
82
    def text_size(self):
95
97
        return ''
96
98
 
97
99
    def copy(self):
98
 
        other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
 
100
        other = inventory.InventoryFile(self.file_id, self.name,
 
101
            self.parent_id)
99
102
        other.executable = self.executable
100
103
        other.text_id = self.text_id
101
104
        other.text_sha1 = self.text_sha1
124
127
 
125
128
    def copy(self):
126
129
        other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
 
130
        other.executable = self.executable
127
131
        other.symlink_target = self.symlink_target
128
132
        other.revision = self.revision
129
133
        return other
175
179
        for mode, name, hexsha in self.object.entries():
176
180
            basename = name.decode("utf-8")
177
181
            child_path = osutils.pathjoin(self.path, basename)
 
182
            if self._inventory.mapping.is_control_file(child_path):
 
183
                continue
178
184
            executable = mode_is_executable(mode)
179
185
            kind_class = {'directory': GitInventoryDirectory,
180
186
                          'file': GitInventoryFile,
181
187
                          'symlink': GitInventoryLink,
182
188
                          'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
183
 
            self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
 
189
            self._children[basename] = kind_class(self._inventory,
 
190
                self.file_id, hexsha, child_path, basename, executable)
184
191
 
185
192
    def copy(self):
186
 
        other = inventory.InventoryDirectory(self.file_id, self.name, 
 
193
        other = inventory.InventoryDirectory(self.file_id, self.name,
187
194
                                             self.parent_id)
188
195
        other.revision = self.revision
189
196
        # note that children are *not* copied; they're pulled across when
193
200
 
194
201
class GitInventory(inventory.Inventory):
195
202
 
196
 
    def __init__(self, tree_id, mapping, store, revision_id):
 
203
    def __repr__(self):
 
204
        return "<%s for %r in %r>" % (self.__class__.__name__,
 
205
                self.root.hexsha, self.store)
 
206
 
 
207
    def __init__(self, tree_id, mapping, fileid_map, store, revision_id):
197
208
        super(GitInventory, self).__init__(revision_id=revision_id)
198
209
        self.store = store
 
210
        self.fileid_map = fileid_map
199
211
        self.mapping = mapping
200
212
        self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
201
213
 
202
214
    def _get_ie(self, path):
203
 
        if path == "":
 
215
        if path == "" or path == []:
204
216
            return self.root
205
 
        parts = path.split("/")
 
217
        if isinstance(path, basestring):
 
218
            parts = path.split("/")
 
219
        else:
 
220
            parts = path
206
221
        ie = self.root
207
222
        for name in parts:
208
 
            ie = ie.children[name] 
 
223
            ie = ie.children[name]
209
224
        return ie
210
225
 
211
226
    def has_filename(self, path):
223
238
            return False
224
239
 
225
240
    def id2path(self, file_id):
226
 
        path = self.mapping.parse_file_id(file_id)
 
241
        path = self.fileid_map.lookup_path(file_id)
227
242
        try:
228
243
            ie = self._get_ie(path)
229
244
            assert ie.path == path
239
254
    def __getitem__(self, file_id):
240
255
        if file_id == inventory.ROOT_ID:
241
256
            return self.root
242
 
        path = self.mapping.parse_file_id(file_id)
 
257
        path = self.fileid_map.lookup_path(file_id)
243
258
        try:
244
259
            return self._get_ie(path)
245
260
        except KeyError:
249
264
class GitIndexInventory(inventory.Inventory):
250
265
    """Inventory that retrieves its contents from an index file."""
251
266
 
252
 
    def __init__(self, basis_inventory, mapping, index, store):
253
 
        super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
 
267
    def __repr__(self):
 
268
        return "<%s for %r>" % (self.__class__.__name__, self.index)
 
269
 
 
270
    def __init__(self, basis_inventory, fileid_map, index, store):
 
271
        if basis_inventory is None:
 
272
            root_id = None
 
273
        else:
 
274
            root_id = basis_inventory.root.file_id
 
275
        super(GitIndexInventory, self).__init__(revision_id=None, root_id=root_id)
254
276
        self.basis_inv = basis_inventory
255
 
        self.mapping = mapping
 
277
        self.fileid_map = fileid_map
256
278
        self.index = index
257
279
        self._contents_read = False
258
 
        self.root = self.add_path("", 'directory', 
259
 
            self.mapping.generate_file_id(""), None)
 
280
        self.store = store
 
281
        self.root = self.add_path("", 'directory',
 
282
            self.fileid_map.lookup_file_id(""), None)
260
283
 
261
284
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
262
285
        self._read_contents()
263
 
        return super(GitIndexInventory, self).iter_entries_by_dir(specific_file_ids=specific_file_ids, yield_parents=yield_parents)
 
286
        return super(GitIndexInventory, self).iter_entries_by_dir(
 
287
            specific_file_ids=specific_file_ids, yield_parents=yield_parents)
264
288
 
265
289
    def has_id(self, file_id):
 
290
        if type(file_id) != str:
 
291
            raise AssertionError
266
292
        try:
267
293
            self.id2path(file_id)
268
294
            return True
276
302
        return super(GitIndexInventory, self).has_filename(path)
277
303
 
278
304
    def id2path(self, file_id):
279
 
        path = self.mapping.parse_file_id(file_id)
 
305
        if type(file_id) != str:
 
306
            raise AssertionError
 
307
        path = self.fileid_map.lookup_path(file_id)
280
308
        if path in self.index:
281
309
            return path
282
310
        self._read_contents()
283
311
        return super(GitIndexInventory, self).id2path(file_id)
284
312
 
285
313
    def path2id(self, path):
 
314
        if type(path) in (list, tuple):
 
315
            path = "/".join(path)
286
316
        if path in self.index:
287
 
            return self.mapping.generate_file_id(path)
288
 
        self._read_contents()
289
 
        return super(GitIndexInventory, self).path2id(path)
 
317
            file_id = self.fileid_map.lookup_file_id(path)
 
318
        else:
 
319
            self._read_contents()
 
320
            file_id = super(GitIndexInventory, self).path2id(path)
 
321
        if file_id is not None and type(file_id) is not str:
 
322
            raise AssertionError
 
323
        return file_id
290
324
 
291
325
    def __getitem__(self, file_id):
292
326
        self._read_contents()
299
333
        pb = ui.ui_factory.nested_progress_bar()
300
334
        try:
301
335
            for i, (path, value) in enumerate(self.index.iteritems()):
302
 
                pb.update("creating working inventory from index", 
 
336
                pb.update("creating working inventory from index",
303
337
                        i, len(self.index))
304
338
                assert isinstance(path, str)
305
339
                assert isinstance(value, tuple) and len(value) == 10
306
340
                (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
307
 
                try:
308
 
                    old_ie = self.basis_inv._get_ie(path)
309
 
                except KeyError:
 
341
                if self.basis_inv is not None:
 
342
                    try:
 
343
                        old_ie = self.basis_inv._get_ie(path)
 
344
                    except KeyError:
 
345
                        old_ie = None
 
346
                else:
310
347
                    old_ie = None
311
348
                if old_ie is None:
312
 
                    file_id = self.mapping.generate_file_id(path)
 
349
                    file_id = self.fileid_map.lookup_file_id(path)
313
350
                else:
314
351
                    file_id = old_ie.file_id
 
352
                if type(file_id) != str:
 
353
                    raise AssertionError
315
354
                kind = mode_kind(mode)
316
355
                if old_ie is not None and old_ie.hexsha == sha:
317
356
                    # Hasn't changed since basis inv
318
357
                    self.add_parents(path)
319
358
                    self.add(old_ie)
320
359
                else:
321
 
                    ie = self.add_path(path, kind, file_id, self.add_parents(path))
322
 
                    data = store[sha].data
 
360
                    ie = self.add_path(path, kind, file_id,
 
361
                        self.add_parents(path))
 
362
                    data = self.store[sha].data
323
363
                    if kind == "symlink":
324
364
                        ie.symlink_target = data
325
365
                    else:
337
377
                parent_fid = None
338
378
            else:
339
379
                parent_fid = self.add_parents(dirname)
340
 
            ie = self.add_path(dirname, 'directory', 
341
 
                    self.mapping.generate_file_id(dirname), parent_fid)
342
 
            if ie.file_id in self.basis_inv:
 
380
            ie = self.add_path(dirname, 'directory',
 
381
                    self.fileid_map.lookup_file_id(dirname), parent_fid)
 
382
            if self.basis_inv is not None and ie.file_id in self.basis_inv:
343
383
                ie.revision = self.basis_inv[ie.file_id].revision
344
384
            file_id = ie.file_id
 
385
        if type(file_id) != str:
 
386
            raise AssertionError
345
387
        return file_id
346
388