/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

  • Committer: Jelmer Vernooij
  • Date: 2009-09-10 13:13:15 UTC
  • mto: (0.200.602 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090910131315-6890xg58pl2jseml
Allow serving remote URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from dulwich.objects import (
20
20
    Blob,
21
 
    Tree,
 
21
    sha_to_hex,
22
22
    )
23
23
from dulwich.object_store import (
24
24
    BaseObjectStore,
25
 
    ObjectStoreIterator,
26
25
    )
27
 
import stat
28
26
 
29
27
from bzrlib import (
30
28
    debug,
36
34
    NULL_REVISION,
37
35
    )
38
36
 
39
 
from bzrlib.plugins.git.errors import (
40
 
    GhostRevision,
41
 
    )
42
 
 
43
37
from bzrlib.plugins.git.mapping import (
44
38
    default_mapping,
45
39
    directory_to_tree,
97
91
        self._update_sha_map()
98
92
        return iter(self._idmap.sha1s())
99
93
 
 
94
    def _revision_to_commit(self, rev, tree_sha):
 
95
        def parent_lookup(revid):
 
96
            try:
 
97
                return self._lookup_revision_sha1(revid)
 
98
            except errors.NoSuchRevision:
 
99
                trace.warning("Ignoring ghost parent %s", revid)
 
100
                return None
 
101
        return revision_to_commit(rev, tree_sha, parent_lookup)
 
102
 
100
103
    def _update_sha_map_revision(self, revid):
101
104
        inv = self.repository.get_inventory(revid)
102
105
        rev = self.repository.get_revision(revid)
103
106
        unusual_modes = extract_unusual_modes(rev)
104
107
        tree_sha = self._get_ie_sha1(inv.root, inv, unusual_modes)
105
 
        commit_obj = revision_to_commit(rev, tree_sha,
106
 
                                        self._idmap.lookup_commit)
 
108
        commit_obj = self._revision_to_commit(rev, tree_sha)
107
109
        try:
108
110
            foreign_revid, mapping = mapping_registry.parse_revision_id(revid)
109
111
        except errors.InvalidRevisionId:
117
119
    def _check_expected_sha(self, expected_sha, object):
118
120
        if expected_sha is None:
119
121
            return
120
 
        if expected_sha != object.id:
121
 
            raise AssertionError("Invalid sha for %r: %s" % (object, expected_sha))
 
122
        if len(expected_sha) == 40:
 
123
            if expected_sha != object.sha().hexdigest():
 
124
                raise AssertionError("Invalid sha for %r: %s" % (object, expected_sha))
 
125
        elif len(expected_sha) == 20:
 
126
            if expected_sha != object.sha().digest():
 
127
                raise AssertionError("Invalid sha for %r: %s" % (object, sha_to_hex(expected_sha)))
 
128
        else:
 
129
            raise AssertionError("Unknown length %d for %r" % (len(expected_sha), expected_sha))
122
130
 
123
131
    def _get_ie_object(self, entry, inv, unusual_modes):  
124
132
        if entry.kind == "directory":
132
140
                return self._idmap.lookup_tree(entry.file_id, inv.revision_id), None
133
141
            except KeyError:
134
142
                ret = self._get_ie_object(entry, inv, unusual_modes)
135
 
                self._idmap.add_entry(ret.id, "tree", (entry.file_id, inv.revision_id))
136
 
                return ret.id, ret
 
143
                if ret is None:
 
144
                    hexsha = None
 
145
                else:
 
146
                    hexsha = ret.id
 
147
                self._idmap.add_entry(hexsha, "tree", (entry.file_id, inv.revision_id))
 
148
                return hexsha, ret
137
149
        else:
138
150
            try:
139
151
                return self._idmap.lookup_blob(entry.file_id, entry.revision), None
171
183
        return tree
172
184
 
173
185
    def _get_commit(self, rev, tree_sha, expected_sha=None):
174
 
        try:
175
 
            commit = revision_to_commit(rev, tree_sha, self._lookup_revision_sha1)
176
 
        except errors.NoSuchRevision, e:
177
 
            raise GhostRevision(e.branch, e.revision)
 
186
        commit = self._revision_to_commit(rev, tree_sha)
178
187
        self._check_expected_sha(expected_sha, commit)
179
188
        return commit
180
189
 
201
210
 
202
211
        :param sha: SHA1 of the git object
203
212
        """
204
 
        return self[sha].as_raw_string()
 
213
        obj = self[sha]
 
214
        return (obj.type, obj.as_raw_string())
205
215
 
206
216
    def __contains__(self, sha):
207
217
        # See if sha is in map
208
218
        try:
209
 
            self._lookup_git_sha(sha)
 
219
            (type, type_data) = self._lookup_git_sha(sha)
 
220
            if type == "commit":
 
221
                return self.repository.has_revision(type_data[0])
 
222
            elif type == "blob":
 
223
                return self.repository.texts.has_version(type_data)
 
224
            elif type == "tree":
 
225
                return self.repository.has_revision(type_data[1])
 
226
            else:
 
227
                raise AssertionError("Unknown object type '%s'" % type)
210
228
        except KeyError:
211
229
            return False
212
230
        else:
235
253
        elif type == "blob":
236
254
            return self._get_blob(type_data[0], type_data[1], expected_sha=sha)
237
255
        elif type == "tree":
238
 
            inv = self.repository.get_inventory(type_data[1])
239
 
            rev = self.repository.get_revision(type_data[1])
 
256
            try:
 
257
                inv = self.repository.get_inventory(type_data[1])
 
258
                rev = self.repository.get_revision(type_data[1])
 
259
            except errors.NoSuchRevision:
 
260
                trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
 
261
                raise KeyError(sha)
240
262
            unusual_modes = extract_unusual_modes(rev)
241
263
            try:
242
264
                return self._get_tree(type_data[0], type_data[1], inv, unusual_modes,