/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

  • Committer: Jelmer Vernooij
  • Date: 2010-06-28 22:30:34 UTC
  • mto: (0.200.953 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100628223034-vylrgdyakmqoupl6
use transport repo objects even for local access.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Git inventory."""
19
19
 
20
20
 
21
 
import stat
22
 
 
23
 
 
24
21
from dulwich.objects import (
25
 
    S_ISGITLINK,
26
22
    Blob,
27
23
    Tree,
28
24
    )
33
29
    inventory,
34
30
    osutils,
35
31
    ui,
36
 
    urlutils,
37
32
    )
38
33
 
39
34
from bzrlib.plugins.git.mapping import (
55
50
        self.path = path
56
51
        self.revision = self._inventory.revision_id
57
52
        self.executable = executable
58
 
        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'))
59
55
 
60
56
    @property
61
57
    def object(self):
72
68
    _git_class = Blob
73
69
 
74
70
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
75
 
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
 
71
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path,
 
72
            basename, executable)
76
73
        self.kind = 'file'
77
74
        self.text_id = None
78
75
        self.symlink_target = None
79
76
 
80
77
    @property
81
78
    def text_sha1(self):
82
 
        return osutils.sha_string(self.object.data)
 
79
        return osutils.sha_strings(self.object.chunked)
83
80
 
84
81
    @property
85
82
    def text_size(self):
100
97
        return ''
101
98
 
102
99
    def copy(self):
103
 
        other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
 
100
        other = inventory.InventoryFile(self.file_id, self.name,
 
101
            self.parent_id)
104
102
        other.executable = self.executable
105
103
        other.text_id = self.text_id
106
104
        other.text_sha1 = self.text_sha1
129
127
 
130
128
    def copy(self):
131
129
        other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
 
130
        other.executable = self.executable
132
131
        other.symlink_target = self.symlink_target
133
132
        other.revision = self.revision
134
133
        return other
140
139
 
141
140
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
142
141
        super(GitInventoryTreeReference, self).__init__(inv, parent_id, hexsha, path, basename, executable)
 
142
        self.hexsha = hexsha
 
143
        self.reference_revision = inv.mapping.revision_id_foreign_to_bzr(hexsha)
143
144
        self.text_sha1 = None
144
145
        self.text_size = None
145
146
        self.symlink_target = None
178
179
        for mode, name, hexsha in self.object.entries():
179
180
            basename = name.decode("utf-8")
180
181
            child_path = osutils.pathjoin(self.path, basename)
 
182
            if self._inventory.mapping.is_control_file(child_path):
 
183
                continue
181
184
            executable = mode_is_executable(mode)
182
185
            kind_class = {'directory': GitInventoryDirectory,
183
186
                          'file': GitInventoryFile,
184
187
                          'symlink': GitInventoryLink,
185
188
                          'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
186
 
            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)
187
191
 
188
192
    def copy(self):
189
 
        other = inventory.InventoryDirectory(self.file_id, self.name, 
 
193
        other = inventory.InventoryDirectory(self.file_id, self.name,
190
194
                                             self.parent_id)
191
195
        other.revision = self.revision
192
196
        # note that children are *not* copied; they're pulled across when
196
200
 
197
201
class GitInventory(inventory.Inventory):
198
202
 
199
 
    def __init__(self, tree_id, mapping, store, revision_id):
 
203
    def __init__(self, tree_id, mapping, fileid_map, store, revision_id):
200
204
        super(GitInventory, self).__init__(revision_id=revision_id)
201
205
        self.store = store
 
206
        self.fileid_map = fileid_map
202
207
        self.mapping = mapping
203
208
        self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
204
209
 
208
213
        parts = path.split("/")
209
214
        ie = self.root
210
215
        for name in parts:
211
 
            ie = ie.children[name] 
 
216
            ie = ie.children[name]
212
217
        return ie
213
218
 
214
219
    def has_filename(self, path):
226
231
            return False
227
232
 
228
233
    def id2path(self, file_id):
229
 
        path = self.mapping.parse_file_id(file_id)
 
234
        path = self.fileid_map.lookup_path(file_id)
230
235
        try:
231
236
            ie = self._get_ie(path)
232
237
            assert ie.path == path
242
247
    def __getitem__(self, file_id):
243
248
        if file_id == inventory.ROOT_ID:
244
249
            return self.root
245
 
        path = self.mapping.parse_file_id(file_id)
 
250
        path = self.fileid_map.lookup_path(file_id)
246
251
        try:
247
252
            return self._get_ie(path)
248
253
        except KeyError:
252
257
class GitIndexInventory(inventory.Inventory):
253
258
    """Inventory that retrieves its contents from an index file."""
254
259
 
255
 
    def __init__(self, basis_inventory, mapping, index, store):
 
260
    def __init__(self, basis_inventory, fileid_map, index, store):
256
261
        super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
257
262
        self.basis_inv = basis_inventory
258
 
        self.mapping = mapping
 
263
        self.fileid_map = fileid_map
259
264
        self.index = index
260
 
 
 
265
        self._contents_read = False
 
266
        self.store = store
 
267
        self.root = self.add_path("", 'directory',
 
268
            self.fileid_map.lookup_file_id(""), None)
 
269
 
 
270
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
 
271
        self._read_contents()
 
272
        return super(GitIndexInventory, self).iter_entries_by_dir(
 
273
            specific_file_ids=specific_file_ids, yield_parents=yield_parents)
 
274
 
 
275
    def has_id(self, file_id):
 
276
        if type(file_id) != str:
 
277
            raise AssertionError
 
278
        try:
 
279
            self.id2path(file_id)
 
280
            return True
 
281
        except errors.NoSuchId:
 
282
            return False
 
283
 
 
284
    def has_filename(self, path):
 
285
        if path in self.index:
 
286
            return True
 
287
        self._read_contents()
 
288
        return super(GitIndexInventory, self).has_filename(path)
 
289
 
 
290
    def id2path(self, file_id):
 
291
        if type(file_id) != str:
 
292
            raise AssertionError
 
293
        path = self.fileid_map.lookup_path(file_id)
 
294
        if path in self.index:
 
295
            return path
 
296
        self._read_contents()
 
297
        return super(GitIndexInventory, self).id2path(file_id)
 
298
 
 
299
    def path2id(self, path):
 
300
        if path in self.index:
 
301
            return self.fileid_map.lookup_file_id(path)
 
302
        self._read_contents()
 
303
        return super(GitIndexInventory, self).path2id(path)
 
304
 
 
305
    def __getitem__(self, file_id):
 
306
        self._read_contents()
 
307
        return super(GitIndexInventory, self).__getitem__(file_id)
 
308
 
 
309
    def _read_contents(self):
 
310
        if self._contents_read:
 
311
            return
 
312
        self._contents_read = True
261
313
        pb = ui.ui_factory.nested_progress_bar()
262
314
        try:
263
315
            for i, (path, value) in enumerate(self.index.iteritems()):
264
 
                pb.update("creating working inventory from index", 
 
316
                pb.update("creating working inventory from index",
265
317
                        i, len(self.index))
266
318
                assert isinstance(path, str)
267
319
                assert isinstance(value, tuple) and len(value) == 10
271
323
                except KeyError:
272
324
                    old_ie = None
273
325
                if old_ie is None:
274
 
                    file_id = self.mapping.generate_file_id(path)
 
326
                    file_id = self.fileid_map.lookup_file_id(path)
275
327
                else:
276
328
                    file_id = old_ie.file_id
 
329
                if type(file_id) != str:
 
330
                    raise AssertionError
277
331
                kind = mode_kind(mode)
278
332
                if old_ie is not None and old_ie.hexsha == sha:
279
333
                    # Hasn't changed since basis inv
280
334
                    self.add_parents(path)
281
335
                    self.add(old_ie)
282
336
                else:
283
 
                    ie = self.add_path(path, kind, file_id, self.add_parents(path))
284
 
                    data = store[sha].data
 
337
                    ie = self.add_path(path, kind, file_id,
 
338
                        self.add_parents(path))
 
339
                    data = self.store[sha].data
285
340
                    if kind == "symlink":
286
341
                        ie.symlink_target = data
287
342
                    else:
293
348
 
294
349
    def add_parents(self, path):
295
350
        dirname, _ = osutils.split(path)
296
 
        file_id = self.path2id(dirname)
 
351
        file_id = super(GitIndexInventory, self).path2id(dirname)
297
352
        if file_id is None:
298
353
            if dirname == "":
299
354
                parent_fid = None
300
355
            else:
301
356
                parent_fid = self.add_parents(dirname)
302
 
            ie = self.add_path(dirname, 'directory', 
303
 
                    self.mapping.generate_file_id(dirname), parent_fid)
 
357
            ie = self.add_path(dirname, 'directory',
 
358
                    self.fileid_map.lookup_file_id(dirname), parent_fid)
304
359
            if ie.file_id in self.basis_inv:
305
360
                ie.revision = self.basis_inv[ie.file_id].revision
306
361
            file_id = ie.file_id
 
362
        if type(file_id) != str:
 
363
            raise AssertionError
307
364
        return file_id
308
365