/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 push.py

merge support for git object store-based caching mechanism.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    errors,
21
21
    ui,
22
22
    )
 
23
from bzrlib.graph import (
 
24
    PendingAncestryResult,
 
25
    )
23
26
from bzrlib.repository import (
24
27
    InterRepository,
25
28
    )
113
116
    def dfetch_refs(self, update_refs):
114
117
        """Fetch non-roundtripped revisions into the target repository.
115
118
 
116
 
        :param update_refs: Generate refs to fetch. Receives dictionary
 
119
        :param update_refs: Generate refs to fetch. Receives dictionary 
117
120
            with old names to old git shas. Should return a dictionary
118
121
            of new names to Bazaar revision ids.
119
122
        :return: revision id map, old refs dictionary and new refs dictionary
123
126
    def fetch_refs(self, update_refs):
124
127
        """Fetch possibly roundtripped revisions into the target repository.
125
128
 
126
 
        :param update_refs: Generate refs to fetch. Receives dictionary
127
 
            with old refs (git shas), returns dictionary of new names to
 
129
        :param update_refs: Generate refs to fetch. Receives dictionary 
 
130
            with old refs (git shas), returns dictionary of new names to 
128
131
            git shas.
129
132
        :return: old refs, new refs
130
133
        """
132
135
 
133
136
 
134
137
class InterToLocalGitRepository(InterToGitRepository):
135
 
    """InterBranch implementation between a Bazaar and a Git repository."""
136
138
 
137
139
    def __init__(self, source, target):
138
140
        super(InterToLocalGitRepository, self).__init__(source, target)
139
141
        self.target_store = self.target._git.object_store
140
142
        self.target_refs = self.target._git.refs
141
143
 
142
 
    def _revision_needs_fetching(self, sha_id, revid):
143
 
        if revid == NULL_REVISION:
144
 
            return False
145
 
        if sha_id is None:
146
 
            try:
147
 
                sha_id = self.source_store._lookup_revision_sha1(revid)
148
 
            except KeyError:
149
 
                return False
150
 
        try:
151
 
            return (sha_id not in self.target_store)
152
 
        except errors.NoSuchRevision:
153
 
            # Ghost, can't push
154
 
            return False
155
 
 
156
 
    def missing_revisions(self, stop_revisions):
157
 
        """Find the revisions that are missing from the target repository.
158
 
 
159
 
        :param stop_revisions: Revisions to check for (tuples with
160
 
            Git SHA1, bzr revid)
161
 
        :return: sequence of missing revisions, in topological order
162
 
        :raise: NoSuchRevision if the stop_revisions are not present in
163
 
            the source
164
 
        """
165
 
        revid_sha_map = {}
166
 
        stop_revids = []
167
 
        stop_sha1s = []
168
 
        for (sha1, revid) in stop_revisions:
169
 
            if sha1 is not None and revid is not None:
170
 
                revid_sha_map[revid] = sha1
171
 
            elif sha1 is not None:
172
 
                stop_sha1s.append(sha1)
173
 
            else:
174
 
                assert revid is not None
175
 
                stop_revids.append(revid)
 
144
    def missing_revisions(self, stop_revisions, check_revid):
176
145
        missing = []
177
146
        graph = self.source.get_graph()
178
147
        pb = ui.ui_factory.nested_progress_bar()
179
148
        try:
180
 
            for revid, _ in graph.iter_ancestry(stop_revids):
181
 
                assert type(revid) is str
 
149
            for revid, _ in graph.iter_ancestry(stop_revisions):
182
150
                pb.update("determining revisions to fetch", len(missing))
183
 
                sha1 = revid_sha_map.get(revid)
184
 
                if self._revision_needs_fetching(sha1, revid):
 
151
                if not check_revid(revid):
185
152
                    missing.append(revid)
186
153
        finally:
187
154
            pb.finished()
188
 
        for sha1 in stop_sha1s:
189
 
            try:
190
 
                (kind, (revid, tree_sha, verifiers)) = self.source_store.lookup_git_sha(sha1)
191
 
            except KeyError:
192
 
                continue
193
 
            else:
194
 
                missing.append(revid)
195
 
                revid_sha_map[revid] = sha1
196
155
        return graph.iter_topo_order(missing)
197
156
 
198
 
    def _get_target_bzr_refs(self):
199
 
        """Return a dictionary with references.
200
 
 
201
 
        :return: Dictionary with reference names as keys and tuples
202
 
            with Git SHA, Bazaar revid as values.
203
 
        """
204
 
        bzr_refs = {}
205
 
        refs = self.target._git.get_refs()
206
 
        for k, v in refs.iteritems():
207
 
            try:
208
 
                (kind, type_data) = self.source_store.lookup_git_sha(v)
209
 
            except KeyError:
210
 
                revid = None
211
 
            else:
212
 
                if kind == "commit":
213
 
                    revid = type_data[0]
214
 
                else:
215
 
                    revid = None
216
 
            bzr_refs[k] = (v, revid)
217
 
        return bzr_refs
218
 
 
219
157
    def fetch_refs(self, update_refs):
220
 
        self.source.lock_read()
221
 
        try:
222
 
            old_refs = self._get_target_bzr_refs()
223
 
            new_refs = update_refs(old_refs)
224
 
            self.fetch(mapped_refs=new_refs.values())
225
 
        finally:
226
 
            self.source.unlock()
 
158
        old_refs = self.target._git.get_refs()
 
159
        new_refs = update_refs(old_refs)
 
160
        fetch_spec = PendingAncestryResult(new_refs.values(), self.source)
 
161
        self.fetch(fetch_spec=fetch_spec)
227
162
        return old_refs, new_refs
228
163
 
229
164
    def dfetch_refs(self, update_refs):
230
 
        self.source.lock_read()
231
 
        try:
232
 
            old_refs = self._get_target_bzr_refs()
233
 
            new_refs = update_refs(old_refs)
234
 
            revidmap, gitidmap = self.dfetch(new_refs.values())
235
 
            for name, (gitid, revid) in new_refs.iteritems():
236
 
                if gitid is None:
237
 
                    try:
238
 
                        gitid = gitidmap[revid]
239
 
                    except KeyError:
240
 
                        gitid = self.source_store._lookup_revision_sha1(revid)
241
 
                self.target._git.refs[name] = gitid
242
 
                new_refs[name] = (gitid, self.source_store.lookup_git_sha(gitid)[1][0])
243
 
        finally:
244
 
            self.source.unlock()
 
165
        old_refs = self.target._git.get_refs()
 
166
        new_refs = update_refs(old_refs)
 
167
        revidmap, gitidmap = self.dfetch(new_refs.values())
 
168
        for name, revid in new_refs.iteritems():
 
169
            try:
 
170
                gitid = gitidmap[revid]
 
171
            except KeyError:
 
172
                gitid = self.source_store._lookup_revision_sha1(revid)
 
173
            self.target._git.refs[name] = gitid
 
174
            new_refs[name] = revid
245
175
        return revidmap, old_refs, new_refs
246
176
 
 
177
    def _find_missing_revs(self, stop_revisions):
 
178
        def check_revid(revid):
 
179
            if revid == NULL_REVISION:
 
180
                return True
 
181
            sha_id = self.source_store._lookup_revision_sha1(revid)
 
182
            try:
 
183
                return (sha_id in self.target_store)
 
184
            except errors.NoSuchRevision:
 
185
                # Ghost, can't push
 
186
                return True
 
187
        return list(self.missing_revisions(stop_revisions, check_revid))
 
188
 
247
189
    def _get_missing_objects_iterator(self, pb):
248
190
        return MissingObjectsIterator(self.source_store, self.source, pb)
249
191
 
253
195
        revidmap = {}
254
196
        self.source.lock_read()
255
197
        try:
256
 
            todo = list(self.missing_revisions(stop_revisions))
 
198
            todo = self._find_missing_revs(stop_revisions)
257
199
            pb = ui.ui_factory.nested_progress_bar()
258
200
            try:
259
 
                object_generator = self._get_missing_objects_iterator(pb)
 
201
                object_generator = self._get_missing_objects_iterator()
260
202
                for old_bzr_revid, git_commit in object_generator.import_revisions(
261
203
                    todo, roundtrip=False):
262
204
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
270
212
        return revidmap, gitidmap
271
213
 
272
214
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
273
 
            fetch_spec=None, mapped_refs=None):
274
 
        if mapped_refs is not None:
275
 
            stop_revisions = mapped_refs
276
 
        elif revision_id is not None:
277
 
            stop_revisions = [(None, revision_id)]
 
215
            fetch_spec=None):
 
216
        if revision_id is not None:
 
217
            stop_revisions = [revision_id]
278
218
        elif fetch_spec is not None:
279
 
            stop_revisions = [(None, revid) for revid in fetch_spec.heads]
 
219
            stop_revisions = fetch_spec.heads
280
220
        else:
281
 
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
221
            stop_revisions = self.source.all_revision_ids()
282
222
        self.source.lock_read()
283
223
        try:
284
 
            todo = list(self.missing_revisions(stop_revisions))
 
224
            todo = self._find_missing_revs(stop_revisions)
285
225
            pb = ui.ui_factory.nested_progress_bar()
286
226
            try:
287
227
                object_generator = self._get_missing_objects_iterator(pb)
313
253
            ret = {}
314
254
            self.old_refs = old_refs
315
255
            self.new_refs = update_refs(self.old_refs)
316
 
            for name, (gitid, revid) in self.new_refs.iteritems():
317
 
                if gitid is None:
318
 
                    ret[name] = self.source_store._lookup_revision_sha1(revid)
319
 
                else:
320
 
                    ret[name] = gitid
 
256
            for name, revid in self.new_refs.iteritems():
 
257
                ret[name] = self.source_store._lookup_revision_sha1(revid)
321
258
            return ret
322
259
        self.source.lock_read()
323
260
        try: