/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

Add FOSDEM roundtripping notes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
 
18
 
"""Git inventory."""
19
 
 
20
 
 
21
 
from dulwich.objects import (
22
 
    Blob,
23
 
    Tree,
24
 
    )
25
 
 
26
 
 
27
 
from bzrlib import (
28
 
    errors,
29
 
    inventory,
30
 
    osutils,
31
 
    ui,
32
 
    )
33
 
 
34
 
from bzrlib.plugins.git.mapping import (
35
 
    mode_kind,
36
 
    mode_is_executable,
37
 
    )
38
 
 
39
 
 
40
 
class GitInventoryEntry(inventory.InventoryEntry):
41
 
 
42
 
    _git_class = None
43
 
 
44
 
    def __init__(self, inv, parent_id, hexsha, path, name, executable):
45
 
        self.name = name
46
 
        self.parent_id = parent_id
47
 
        self._inventory = inv
48
 
        self._object = None
49
 
        self.hexsha = hexsha
50
 
        self.path = path
51
 
        self.revision = self._inventory.revision_id
52
 
        self.executable = executable
53
 
        self.file_id = self._inventory.fileid_map.lookup_file_id(
54
 
            path.encode('utf-8'))
55
 
 
56
 
    @property
57
 
    def object(self):
58
 
        if self._object is None:
59
 
            self._object = self._inventory.store[self.hexsha]
60
 
            assert isinstance(self._object, self._git_class), \
61
 
                    "Expected instance of %r, got %r" % \
62
 
                    (self._git_class, self._object)
63
 
        return self._object
64
 
 
65
 
 
66
 
class GitInventoryFile(GitInventoryEntry):
67
 
 
68
 
    _git_class = Blob
69
 
 
70
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
71
 
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path,
72
 
            basename, executable)
73
 
        self.kind = 'file'
74
 
        self.text_id = None
75
 
        self.symlink_target = None
76
 
 
77
 
    @property
78
 
    def text_sha1(self):
79
 
        return osutils.sha_strings(self.object.chunked)
80
 
 
81
 
    @property
82
 
    def text_size(self):
83
 
        return len(self.object.data)
84
 
 
85
 
    def __repr__(self):
86
 
        return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
87
 
                % (self.__class__.__name__,
88
 
                   self.file_id,
89
 
                   self.name,
90
 
                   self.parent_id,
91
 
                   self.text_sha1,
92
 
                   self.text_size,
93
 
                   self.revision))
94
 
 
95
 
    def kind_character(self):
96
 
        """See InventoryEntry.kind_character."""
97
 
        return ''
98
 
 
99
 
    def copy(self):
100
 
        other = inventory.InventoryFile(self.file_id, self.name,
101
 
            self.parent_id)
102
 
        other.executable = self.executable
103
 
        other.text_id = self.text_id
104
 
        other.text_sha1 = self.text_sha1
105
 
        other.text_size = self.text_size
106
 
        other.revision = self.revision
107
 
        return other
108
 
 
109
 
 
110
 
class GitInventoryLink(GitInventoryEntry):
111
 
 
112
 
    _git_class = Blob
113
 
 
114
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
115
 
        super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
116
 
        self.text_sha1 = None
117
 
        self.text_size = None
118
 
        self.kind = 'symlink'
119
 
 
120
 
    @property
121
 
    def symlink_target(self):
122
 
        return self.object.data
123
 
 
124
 
    def kind_character(self):
125
 
        """See InventoryEntry.kind_character."""
126
 
        return ''
127
 
 
128
 
    def copy(self):
129
 
        other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
130
 
        other.executable = self.executable
131
 
        other.symlink_target = self.symlink_target
132
 
        other.revision = self.revision
133
 
        return other
134
 
 
135
 
 
136
 
class GitInventoryTreeReference(GitInventoryEntry):
137
 
 
138
 
    _git_class = None
139
 
 
140
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
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)
144
 
        self.text_sha1 = None
145
 
        self.text_size = None
146
 
        self.symlink_target = None
147
 
        self.kind = 'tree-reference'
148
 
        self._children = None
149
 
 
150
 
    def kind_character(self):
151
 
        """See InventoryEntry.kind_character."""
152
 
        return '/'
153
 
 
154
 
 
155
 
class GitInventoryDirectory(GitInventoryEntry):
156
 
 
157
 
    _git_class = Tree
158
 
 
159
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
160
 
        super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
161
 
        self.text_sha1 = None
162
 
        self.text_size = None
163
 
        self.symlink_target = None
164
 
        self.kind = 'directory'
165
 
        self._children = None
166
 
 
167
 
    def kind_character(self):
168
 
        """See InventoryEntry.kind_character."""
169
 
        return '/'
170
 
 
171
 
    @property
172
 
    def children(self):
173
 
        if self._children is None:
174
 
            self._retrieve_children()
175
 
        return self._children
176
 
 
177
 
    def _retrieve_children(self):
178
 
        self._children = {}
179
 
        for mode, name, hexsha in self.object.entries():
180
 
            basename = name.decode("utf-8")
181
 
            child_path = osutils.pathjoin(self.path, basename)
182
 
            if self._inventory.mapping.is_control_file(child_path):
183
 
                continue
184
 
            executable = mode_is_executable(mode)
185
 
            kind_class = {'directory': GitInventoryDirectory,
186
 
                          'file': GitInventoryFile,
187
 
                          'symlink': GitInventoryLink,
188
 
                          'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
189
 
            self._children[basename] = kind_class(self._inventory,
190
 
                self.file_id, hexsha, child_path, basename, executable)
191
 
 
192
 
    def copy(self):
193
 
        other = inventory.InventoryDirectory(self.file_id, self.name,
194
 
                                             self.parent_id)
195
 
        other.revision = self.revision
196
 
        # note that children are *not* copied; they're pulled across when
197
 
        # others are added
198
 
        return other
199
 
 
200
 
 
201
 
class GitInventory(inventory.Inventory):
202
 
 
203
 
    def __init__(self, tree_id, mapping, fileid_map, store, revision_id):
204
 
        super(GitInventory, self).__init__(revision_id=revision_id)
205
 
        self.store = store
206
 
        self.fileid_map = fileid_map
207
 
        self.mapping = mapping
208
 
        self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
209
 
 
210
 
    def _get_ie(self, path):
211
 
        if path == "":
212
 
            return self.root
213
 
        parts = path.split("/")
214
 
        ie = self.root
215
 
        for name in parts:
216
 
            ie = ie.children[name]
217
 
        return ie
218
 
 
219
 
    def has_filename(self, path):
220
 
        try:
221
 
            self._get_ie(path)
222
 
            return True
223
 
        except KeyError:
224
 
            return False
225
 
 
226
 
    def has_id(self, file_id):
227
 
        try:
228
 
            self.id2path(file_id)
229
 
            return True
230
 
        except errors.NoSuchId:
231
 
            return False
232
 
 
233
 
    def id2path(self, file_id):
234
 
        path = self.fileid_map.lookup_path(file_id)
235
 
        try:
236
 
            ie = self._get_ie(path)
237
 
            assert ie.path == path
238
 
        except KeyError:
239
 
            raise errors.NoSuchId(None, file_id)
240
 
 
241
 
    def path2id(self, path):
242
 
        try:
243
 
            return self._get_ie(path).file_id
244
 
        except KeyError:
245
 
            return None
246
 
 
247
 
    def __getitem__(self, file_id):
248
 
        if file_id == inventory.ROOT_ID:
249
 
            return self.root
250
 
        path = self.fileid_map.lookup_path(file_id)
251
 
        try:
252
 
            return self._get_ie(path)
253
 
        except KeyError:
254
 
            raise errors.NoSuchId(None, file_id)
255
 
 
256
 
 
257
 
class GitIndexInventory(inventory.Inventory):
258
 
    """Inventory that retrieves its contents from an index file."""
259
 
 
260
 
    def __init__(self, basis_inventory, fileid_map, index, store):
261
 
        super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
262
 
        self.basis_inv = basis_inventory
263
 
        self.fileid_map = fileid_map
264
 
        self.index = index
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
313
 
        pb = ui.ui_factory.nested_progress_bar()
314
 
        try:
315
 
            for i, (path, value) in enumerate(self.index.iteritems()):
316
 
                pb.update("creating working inventory from index",
317
 
                        i, len(self.index))
318
 
                assert isinstance(path, str)
319
 
                assert isinstance(value, tuple) and len(value) == 10
320
 
                (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
321
 
                try:
322
 
                    old_ie = self.basis_inv._get_ie(path)
323
 
                except KeyError:
324
 
                    old_ie = None
325
 
                if old_ie is None:
326
 
                    file_id = self.fileid_map.lookup_file_id(path)
327
 
                else:
328
 
                    file_id = old_ie.file_id
329
 
                if type(file_id) != str:
330
 
                    raise AssertionError
331
 
                kind = mode_kind(mode)
332
 
                if old_ie is not None and old_ie.hexsha == sha:
333
 
                    # Hasn't changed since basis inv
334
 
                    self.add_parents(path)
335
 
                    self.add(old_ie)
336
 
                else:
337
 
                    ie = self.add_path(path, kind, file_id,
338
 
                        self.add_parents(path))
339
 
                    data = self.store[sha].data
340
 
                    if kind == "symlink":
341
 
                        ie.symlink_target = data
342
 
                    else:
343
 
                        ie.text_sha1 = osutils.sha_string(data)
344
 
                        ie.text_size = len(data)
345
 
                    ie.revision = None
346
 
        finally:
347
 
            pb.finished()
348
 
 
349
 
    def add_parents(self, path):
350
 
        dirname, _ = osutils.split(path)
351
 
        file_id = super(GitIndexInventory, self).path2id(dirname)
352
 
        if file_id is None:
353
 
            if dirname == "":
354
 
                parent_fid = None
355
 
            else:
356
 
                parent_fid = self.add_parents(dirname)
357
 
            ie = self.add_path(dirname, 'directory',
358
 
                    self.fileid_map.lookup_file_id(dirname), parent_fid)
359
 
            if ie.file_id in self.basis_inv:
360
 
                ie.revision = self.basis_inv[ie.file_id].revision
361
 
            file_id = ie.file_id
362
 
        if type(file_id) != str:
363
 
            raise AssertionError
364
 
        return file_id
365