/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:
39
39
    directory_to_tree,
40
40
    extract_unusual_modes,
41
41
    mapping_registry,
 
42
    revision_to_commit,
42
43
    )
43
44
from bzrlib.plugins.git.shamap import (
44
45
    SqliteGitShaMap,
62
63
            self.mapping = default_mapping
63
64
        else:
64
65
            self.mapping = mapping
65
 
        self._idmap = SqliteGitShaMap.from_repository(repository)
 
66
        try:
 
67
            self._idmap = TdbGitShaMap.from_repository(repository)
 
68
        except ImportError:
 
69
            self._idmap = SqliteGitShaMap.from_repository(repository)
66
70
 
67
71
    def _update_sha_map(self, stop_revision=None):
 
72
        if stop_revision is None:
 
73
            all_revids = self.repository.all_revision_ids()
 
74
        else:
 
75
            all_revids = self.repository.get_ancestry(stop_revision)
 
76
            first = all_revids.pop(0) # Pop leading None
 
77
            assert first is None
68
78
        graph = self.repository.get_graph()
69
 
        if stop_revision is None:
70
 
            heads = graph.heads(self.repository.all_revision_ids())
71
 
        else:
72
 
            heads = set([stop_revision])
73
 
        missing_revids = self._idmap.missing_revisions(heads)
74
 
        while heads:
75
 
            parents = graph.get_parent_map(heads)
76
 
            todo = set()
77
 
            for p in parents.values():
78
 
                todo.update([x for x in p if x not in missing_revids])
79
 
            heads = self._idmap.missing_revisions(todo)
80
 
            missing_revids.update(heads)
81
 
        if NULL_REVISION in missing_revids:
82
 
            missing_revids.remove(NULL_REVISION)
 
79
        present_revids = set(self._idmap.revids())
 
80
        missing_revids = [revid for revid in graph.iter_topo_order(all_revids) if revid not in present_revids]
83
81
        pb = ui.ui_factory.nested_progress_bar()
84
82
        try:
85
 
            for i, revid in enumerate(graph.iter_topo_order(missing_revids)):
 
83
            for i, revid in enumerate(missing_revids):
86
84
                pb.update("updating git map", i, len(missing_revids))
87
85
                self._update_sha_map_revision(revid)
88
86
        finally:
 
87
            self._idmap.commit()
89
88
            pb.finished()
90
 
        self._idmap.commit_write_group()
91
89
 
92
90
    def __iter__(self):
93
91
        self._update_sha_map()
100
98
            except errors.NoSuchRevision:
101
99
                trace.warning("Ignoring ghost parent %s", revid)
102
100
                return None
103
 
        return self.mapping.export_commit(rev, tree_sha, parent_lookup)
 
101
        return revision_to_commit(rev, tree_sha, parent_lookup)
104
102
 
105
103
    def _update_sha_map_revision(self, revid):
106
104
        inv = self.repository.get_inventory(revid)
130
128
        else:
131
129
            raise AssertionError("Unknown length %d for %r" % (len(expected_sha), expected_sha))
132
130
 
133
 
    def _get_ie_object(self, entry, inv, unusual_modes):
 
131
    def _get_ie_object(self, entry, inv, unusual_modes):  
134
132
        if entry.kind == "directory":
135
133
            return self._get_tree(entry.file_id, inv.revision_id, inv, unusual_modes)
136
 
        elif entry.kind == "symlink":
137
 
            return self._get_blob_for_symlink(entry.symlink_target)
138
 
        elif entry.kind == "file":
139
 
            return self._get_blob_for_file(entry.file_id, entry.revision)
140
134
        else:
141
 
            raise AssertionError("unknown entry kind '%s'" % entry.kind)
 
135
            return self._get_blob(entry.file_id, entry.revision)
142
136
 
143
137
    def _get_ie_object_or_sha1(self, entry, inv, unusual_modes):
144
138
        if entry.kind == "directory":
152
146
                    hexsha = ret.id
153
147
                self._idmap.add_entry(hexsha, "tree", (entry.file_id, inv.revision_id))
154
148
                return hexsha, ret
155
 
        elif entry.kind in ("file", "symlink"):
 
149
        else:
156
150
            try:
157
151
                return self._idmap.lookup_blob(entry.file_id, entry.revision), None
158
152
            except KeyError:
159
153
                ret = self._get_ie_object(entry, inv, unusual_modes)
160
154
                self._idmap.add_entry(ret.id, "blob", (entry.file_id, entry.revision))
161
155
                return ret.id, ret
162
 
        else:
163
 
            raise AssertionError("unknown entry kind '%s'" % entry.kind)
164
156
 
165
157
    def _get_ie_sha1(self, entry, inv, unusual_modes):
166
158
        return self._get_ie_object_or_sha1(entry, inv, unusual_modes)[0]
167
159
 
168
 
    def _get_blob_for_symlink(self, symlink_target, expected_sha=None):
169
 
        """Return a Git Blob object for symlink.
170
 
 
171
 
        :param symlink_target: target of symlink.
172
 
        """
173
 
        if type(symlink_target) == unicode:
174
 
            symlink_target = symlink_target.encode('utf-8')
175
 
        blob = Blob()
176
 
        blob._text = symlink_target
177
 
        self._check_expected_sha(expected_sha, blob)
178
 
        return blob
179
 
 
180
 
    def _get_blob_for_file(self, fileid, revision, expected_sha=None):
181
 
        """Return a Git Blob object from a fileid and revision stored in bzr.
182
 
 
183
 
        :param fileid: File id of the text
184
 
        :param revision: Revision of the text
185
 
        """
186
 
        blob = Blob()
187
 
        chunks = self.repository.iter_files_bytes([(fileid, revision, None)]).next()[1]
188
 
        blob._text = "".join(chunks)
189
 
        self._check_expected_sha(expected_sha, blob)
190
 
        return blob
191
 
 
192
160
    def _get_blob(self, fileid, revision, expected_sha=None):
193
161
        """Return a Git Blob object from a fileid and revision stored in bzr.
194
 
 
 
162
        
195
163
        :param fileid: File id of the text
196
164
        :param revision: Revision of the text
197
165
        """
198
 
        inv = self.repository.get_inventory(revision)
199
 
        entry = inv[fileid]
200
 
 
201
 
        if entry.kind == 'file':
202
 
            return self._get_blob_for_file(entry.file_id, entry.revision,
203
 
                                           expected_sha=expected_sha)
204
 
        elif entry.kind == 'symlink':
205
 
            return self._get_blob_for_symlink(entry.symlink_target,
206
 
                                              expected_sha=expected_sha)
207
 
        else:
208
 
            raise AssertionError
 
166
        text = self.repository.texts.get_record_stream([(fileid, revision)],
 
167
            "unordered", True).next().get_bytes_as("fulltext")
 
168
        blob = Blob()
 
169
        blob._text = text
 
170
        self._check_expected_sha(expected_sha, blob)
 
171
        return blob
209
172
 
210
173
    def _get_tree(self, fileid, revid, inv, unusual_modes, expected_sha=None):
211
174
        """Return a Git Tree object from a file id and a revision stored in bzr.
213
176
        :param fileid: fileid in the tree.
214
177
        :param revision: Revision of the tree.
215
178
        """
216
 
        tree = directory_to_tree(inv[fileid],
 
179
        tree = directory_to_tree(inv[fileid], 
217
180
            lambda ie: self._get_ie_sha1(ie, inv, unusual_modes),
218
181
            unusual_modes)
219
182
        self._check_expected_sha(expected_sha, tree)
239
202
        try:
240
203
            return self._idmap.lookup_commit(revid)
241
204
        except KeyError:
242
 
            try:
243
 
                return mapping_registry.parse_revision_id(revid)[0]
244
 
            except errors.InvalidRevisionId:
245
 
                self._update_sha_map(revid)
246
 
                return self._idmap.lookup_commit(revid)
 
205
            self._update_sha_map(revid)
 
206
            return self._idmap.lookup_commit(revid)
247
207
 
248
208
    def get_raw(self, sha):
249
209
        """Get the raw representation of a Git object by SHA1.
275
235
        try:
276
236
            return self._idmap.lookup_git_sha(sha)
277
237
        except KeyError:
278
 
            # if not, see if there are any unconverted revisions and add them
 
238
            # if not, see if there are any unconverted revisions and add them 
279
239
            # to the map, search for sha in map again
280
240
            self._update_sha_map()
281
241
            return self._idmap.lookup_git_sha(sha)