/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

Clean up trailing whitespace.

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,
46
40
    extract_unusual_modes,
47
41
    mapping_registry,
48
 
    revision_to_commit,
49
42
    )
50
43
from bzrlib.plugins.git.shamap import (
51
44
    SqliteGitShaMap,
75
68
            self._idmap = SqliteGitShaMap.from_repository(repository)
76
69
 
77
70
    def _update_sha_map(self, stop_revision=None):
 
71
        graph = self.repository.get_graph()
78
72
        if stop_revision is None:
79
 
            all_revids = self.repository.all_revision_ids()
 
73
            heads = graph.heads(self.repository.all_revision_ids())
80
74
        else:
81
 
            all_revids = self.repository.get_ancestry(stop_revision)
82
 
            first = all_revids.pop(0) # Pop leading None
83
 
            assert first is None
84
 
        graph = self.repository.get_graph()
85
 
        present_revids = set(self._idmap.revids())
86
 
        missing_revids = [revid for revid in graph.iter_topo_order(all_revids) if revid not in present_revids]
 
75
            heads = set([stop_revision])
 
76
        missing_revids = self._idmap.missing_revisions(heads)
 
77
        while heads:
 
78
            parents = graph.get_parent_map(heads)
 
79
            todo = set()
 
80
            for p in parents.values():
 
81
                todo.update([x for x in p if x not in missing_revids])
 
82
            heads = self._idmap.missing_revisions(todo)
 
83
            missing_revids.update(heads)
 
84
        if NULL_REVISION in missing_revids:
 
85
            missing_revids.remove(NULL_REVISION)
87
86
        pb = ui.ui_factory.nested_progress_bar()
88
87
        try:
89
 
            for i, revid in enumerate(missing_revids):
 
88
            for i, revid in enumerate(graph.iter_topo_order(missing_revids)):
90
89
                pb.update("updating git map", i, len(missing_revids))
91
90
                self._update_sha_map_revision(revid)
92
91
        finally:
93
 
            self._idmap.commit()
94
92
            pb.finished()
 
93
        self._idmap.commit_write_group()
95
94
 
96
95
    def __iter__(self):
97
96
        self._update_sha_map()
98
97
        return iter(self._idmap.sha1s())
99
98
 
 
99
    def _revision_to_commit(self, rev, tree_sha):
 
100
        def parent_lookup(revid):
 
101
            try:
 
102
                return self._lookup_revision_sha1(revid)
 
103
            except errors.NoSuchRevision:
 
104
                trace.warning("Ignoring ghost parent %s", revid)
 
105
                return None
 
106
        return self.mapping.export_commit(rev, tree_sha, parent_lookup)
 
107
 
100
108
    def _update_sha_map_revision(self, revid):
101
109
        inv = self.repository.get_inventory(revid)
102
110
        rev = self.repository.get_revision(revid)
103
111
        unusual_modes = extract_unusual_modes(rev)
104
112
        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)
 
113
        commit_obj = self._revision_to_commit(rev, tree_sha)
107
114
        try:
108
115
            foreign_revid, mapping = mapping_registry.parse_revision_id(revid)
109
116
        except errors.InvalidRevisionId:
117
124
    def _check_expected_sha(self, expected_sha, object):
118
125
        if expected_sha is None:
119
126
            return
120
 
        if expected_sha != object.id:
121
 
            raise AssertionError("Invalid sha for %r: %s" % (object, expected_sha))
 
127
        if len(expected_sha) == 40:
 
128
            if expected_sha != object.sha().hexdigest():
 
129
                raise AssertionError("Invalid sha for %r: %s" % (object, expected_sha))
 
130
        elif len(expected_sha) == 20:
 
131
            if expected_sha != object.sha().digest():
 
132
                raise AssertionError("Invalid sha for %r: %s" % (object, sha_to_hex(expected_sha)))
 
133
        else:
 
134
            raise AssertionError("Unknown length %d for %r" % (len(expected_sha), expected_sha))
122
135
 
123
 
    def _get_ie_object(self, entry, inv, unusual_modes):  
 
136
    def _get_ie_object(self, entry, inv, unusual_modes):
124
137
        if entry.kind == "directory":
125
138
            return self._get_tree(entry.file_id, inv.revision_id, inv, unusual_modes)
126
 
        else:
 
139
        elif entry.kind in ("file", "symlink"):
127
140
            return self._get_blob(entry.file_id, entry.revision)
 
141
        else:
 
142
            raise AssertionError("unknown entry kind '%s'" % entry.kind)
128
143
 
129
144
    def _get_ie_object_or_sha1(self, entry, inv, unusual_modes):
130
145
        if entry.kind == "directory":
132
147
                return self._idmap.lookup_tree(entry.file_id, inv.revision_id), None
133
148
            except KeyError:
134
149
                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
137
 
        else:
 
150
                if ret is None:
 
151
                    hexsha = None
 
152
                else:
 
153
                    hexsha = ret.id
 
154
                self._idmap.add_entry(hexsha, "tree", (entry.file_id, inv.revision_id))
 
155
                return hexsha, ret
 
156
        elif entry.kind in ("file", "symlink"):
138
157
            try:
139
158
                return self._idmap.lookup_blob(entry.file_id, entry.revision), None
140
159
            except KeyError:
141
160
                ret = self._get_ie_object(entry, inv, unusual_modes)
142
161
                self._idmap.add_entry(ret.id, "blob", (entry.file_id, entry.revision))
143
162
                return ret.id, ret
 
163
        else:
 
164
            raise AssertionError("unknown entry kind '%s'" % entry.kind)
144
165
 
145
166
    def _get_ie_sha1(self, entry, inv, unusual_modes):
146
167
        return self._get_ie_object_or_sha1(entry, inv, unusual_modes)[0]
147
168
 
148
169
    def _get_blob(self, fileid, revision, expected_sha=None):
149
170
        """Return a Git Blob object from a fileid and revision stored in bzr.
150
 
        
 
171
 
151
172
        :param fileid: File id of the text
152
173
        :param revision: Revision of the text
153
174
        """
154
 
        text = self.repository.texts.get_record_stream([(fileid, revision)],
155
 
            "unordered", True).next().get_bytes_as("fulltext")
 
175
        chunks = self.repository.iter_files_bytes([(fileid, revision, None)]).next()[1]
156
176
        blob = Blob()
157
 
        blob._text = text
 
177
        blob._text = "".join(chunks)
158
178
        self._check_expected_sha(expected_sha, blob)
159
179
        return blob
160
180
 
164
184
        :param fileid: fileid in the tree.
165
185
        :param revision: Revision of the tree.
166
186
        """
167
 
        tree = directory_to_tree(inv[fileid], 
 
187
        tree = directory_to_tree(inv[fileid],
168
188
            lambda ie: self._get_ie_sha1(ie, inv, unusual_modes),
169
189
            unusual_modes)
170
190
        self._check_expected_sha(expected_sha, tree)
171
191
        return tree
172
192
 
173
193
    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)
 
194
        commit = self._revision_to_commit(rev, tree_sha)
178
195
        self._check_expected_sha(expected_sha, commit)
179
196
        return commit
180
197
 
193
210
        try:
194
211
            return self._idmap.lookup_commit(revid)
195
212
        except KeyError:
196
 
            self._update_sha_map(revid)
197
 
            return self._idmap.lookup_commit(revid)
 
213
            try:
 
214
                return mapping_registry.parse_revision_id(revid)[0]
 
215
            except errors.InvalidRevisionId:
 
216
                self._update_sha_map(revid)
 
217
                return self._idmap.lookup_commit(revid)
198
218
 
199
219
    def get_raw(self, sha):
200
220
        """Get the raw representation of a Git object by SHA1.
201
221
 
202
222
        :param sha: SHA1 of the git object
203
223
        """
204
 
        return self[sha].as_raw_string()
 
224
        obj = self[sha]
 
225
        return (obj.type, obj.as_raw_string())
205
226
 
206
227
    def __contains__(self, sha):
207
228
        # See if sha is in map
208
229
        try:
209
 
            self._lookup_git_sha(sha)
 
230
            (type, type_data) = self._lookup_git_sha(sha)
 
231
            if type == "commit":
 
232
                return self.repository.has_revision(type_data[0])
 
233
            elif type == "blob":
 
234
                return self.repository.texts.has_version(type_data)
 
235
            elif type == "tree":
 
236
                return self.repository.has_revision(type_data[1])
 
237
            else:
 
238
                raise AssertionError("Unknown object type '%s'" % type)
210
239
        except KeyError:
211
240
            return False
212
241
        else:
217
246
        try:
218
247
            return self._idmap.lookup_git_sha(sha)
219
248
        except KeyError:
220
 
            # if not, see if there are any unconverted revisions and add them 
 
249
            # if not, see if there are any unconverted revisions and add them
221
250
            # to the map, search for sha in map again
222
251
            self._update_sha_map()
223
252
            return self._idmap.lookup_git_sha(sha)
235
264
        elif type == "blob":
236
265
            return self._get_blob(type_data[0], type_data[1], expected_sha=sha)
237
266
        elif type == "tree":
238
 
            inv = self.repository.get_inventory(type_data[1])
239
 
            rev = self.repository.get_revision(type_data[1])
 
267
            try:
 
268
                inv = self.repository.get_inventory(type_data[1])
 
269
                rev = self.repository.get_revision(type_data[1])
 
270
            except errors.NoSuchRevision:
 
271
                trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
 
272
                raise KeyError(sha)
240
273
            unusual_modes = extract_unusual_modes(rev)
241
274
            try:
242
275
                return self._get_tree(type_data[0], type_data[1], inv, unusual_modes,