/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

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    NoPushSupport,
32
32
    )
33
33
from bzrlib.plugins.git.object_store import (
34
 
    get_object_store,
 
34
    BazaarObjectStore,
35
35
    )
36
36
from bzrlib.plugins.git.repository import (
37
37
    GitRepository,
41
41
from bzrlib.plugins.git.remote import (
42
42
    RemoteGitRepository,
43
43
    )
44
 
from bzrlib.plugins.git.unpeel_map import (
45
 
    UnpeelMap,
46
 
    )
47
44
 
48
45
 
49
46
class MissingObjectsIterator(object):
103
100
    def __init__(self, source, target):
104
101
        super(InterToGitRepository, self).__init__(source, target)
105
102
        self.mapping = self.target.get_mapping()
106
 
        self.source_store = get_object_store(self.source, self.mapping)
 
103
        self.source_store = BazaarObjectStore(self.source, self.mapping)
107
104
 
108
105
    @staticmethod
109
106
    def _get_repo_format_to_test():
113
110
        """See InterRepository.copy_content."""
114
111
        self.fetch(revision_id, pb, find_ghosts=False)
115
112
 
116
 
    def fetch_refs(self, update_refs, lossy):
117
 
        """Fetch possibly roundtripped revisions into the target repository
118
 
        and update refs.
 
113
    def dfetch_refs(self, update_refs):
 
114
        """Fetch non-roundtripped revisions into the target repository.
 
115
 
 
116
        :param update_refs: Generate refs to fetch. Receives dictionary
 
117
            with old names to old git shas. Should return a dictionary
 
118
            of new names to Bazaar revision ids.
 
119
        :return: revision id map, old refs dictionary and new refs dictionary
 
120
        """
 
121
        raise NotImplementedError(self.dfetch_refs)
 
122
 
 
123
    def fetch_refs(self, update_refs):
 
124
        """Fetch possibly roundtripped revisions into the target repository.
119
125
 
120
126
        :param update_refs: Generate refs to fetch. Receives dictionary
121
127
            with old refs (git shas), returns dictionary of new names to
122
128
            git shas.
123
 
        :param lossy: Whether to roundtrip
124
129
        :return: old refs, new refs
125
130
        """
126
131
        raise NotImplementedError(self.fetch_refs)
182
187
            pb.finished()
183
188
        for sha1 in stop_sha1s:
184
189
            try:
185
 
                for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
186
 
                    missing.append(revid)
187
 
                    revid_sha_map[revid] = sha1
 
190
                (kind, (revid, tree_sha, verifiers)) = self.source_store.lookup_git_sha(sha1)
188
191
            except KeyError:
189
192
                continue
 
193
            else:
 
194
                missing.append(revid)
 
195
                revid_sha_map[revid] = sha1
190
196
        return graph.iter_topo_order(missing)
191
197
 
192
198
    def _get_target_bzr_refs(self):
196
202
            with Git SHA, Bazaar revid as values.
197
203
        """
198
204
        bzr_refs = {}
199
 
        refs = {}
200
 
        for k in self.target._git.refs.allkeys():
201
 
            v = self.target._git.refs.read_ref(k)
 
205
        refs = self.target._git.get_refs()
 
206
        for k, v in refs.iteritems():
202
207
            try:
203
 
                for (kind, type_data) in self.source_store.lookup_git_sha(v):
204
 
                    if kind == "commit" and self.source.has_revision(type_data[0]):
205
 
                        revid = type_data[0]
206
 
                        break
 
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]
207
214
                else:
208
215
                    revid = None
209
 
            except KeyError:
210
 
                revid = None
211
216
            bzr_refs[k] = (v, revid)
212
217
        return bzr_refs
213
218
 
214
 
    def fetch_refs(self, update_refs, lossy):
215
 
        self.source_store.lock_read()
216
 
        try:
217
 
            old_refs = self._get_target_bzr_refs()
218
 
            new_refs = update_refs(old_refs)
219
 
            revidmap = self.fetch_objects(new_refs.values(), roundtrip=not lossy)
 
219
    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()
 
227
        return old_refs, new_refs
 
228
 
 
229
    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())
220
235
            for name, (gitid, revid) in new_refs.iteritems():
221
236
                if gitid is None:
222
237
                    try:
223
 
                        gitid = revidmap[revid][0]
 
238
                        gitid = gitidmap[revid]
224
239
                    except KeyError:
225
240
                        gitid = self.source_store._lookup_revision_sha1(revid)
226
 
                assert len(gitid) == 40
227
 
                self.target_refs[name] = gitid
 
241
                self.target._git.refs[name] = gitid
 
242
                new_refs[name] = (gitid, self.source_store.lookup_git_sha(gitid)[1][0])
228
243
        finally:
229
 
            self.source_store.unlock()
 
244
            self.source.unlock()
230
245
        return revidmap, old_refs, new_refs
231
246
 
232
 
    def fetch_objects(self, revs, roundtrip):
233
 
        todo = list(self.missing_revisions(revs))
234
 
        revidmap = {}
235
 
        pb = ui.ui_factory.nested_progress_bar()
236
 
        try:
237
 
            object_generator = self._get_missing_objects_iterator(pb)
238
 
            for (old_revid, git_sha) in object_generator.import_revisions(
239
 
                todo, roundtrip=roundtrip):
240
 
                try:
241
 
                    self.mapping.revision_id_bzr_to_foreign(old_revid)
242
 
                except errors.InvalidRevisionId:
243
 
                    self.target_refs[self.mapping.revid_as_refname(old_revid)] = git_sha
244
 
                if not roundtrip:
245
 
                    new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
246
 
                else:
247
 
                    new_revid = old_revid
248
 
                revidmap[old_revid] = (git_sha, new_revid)
249
 
            self.target_store.add_objects(object_generator)
250
 
            return revidmap
251
 
        finally:
252
 
            pb.finished()
253
 
 
254
247
    def _get_missing_objects_iterator(self, pb):
255
248
        return MissingObjectsIterator(self.source_store, self.source, pb)
256
249
 
257
250
    def dfetch(self, stop_revisions):
258
251
        """Import the gist of the ancestry of a particular revision."""
 
252
        gitidmap = {}
 
253
        revidmap = {}
 
254
        self.source.lock_read()
 
255
        try:
 
256
            todo = list(self.missing_revisions(stop_revisions))
 
257
            pb = ui.ui_factory.nested_progress_bar()
 
258
            try:
 
259
                object_generator = self._get_missing_objects_iterator(pb)
 
260
                for old_bzr_revid, git_commit in object_generator.import_revisions(
 
261
                    todo, roundtrip=False):
 
262
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
 
263
                    revidmap[old_bzr_revid] = new_bzr_revid
 
264
                    gitidmap[old_bzr_revid] = git_commit
 
265
                self.target_store.add_objects(object_generator)
 
266
            finally:
 
267
                pb.finished()
 
268
        finally:
 
269
            self.source.unlock()
 
270
        return revidmap, gitidmap
259
271
 
260
272
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
261
273
            fetch_spec=None, mapped_refs=None):
262
 
        if not self.mapping.roundtripping:
263
 
            raise NoPushSupport()
264
 
        self.source_store.lock_read()
 
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)]
 
278
        elif fetch_spec is not None:
 
279
            stop_revisions = [(None, revid) for revid in fetch_spec.heads]
 
280
        else:
 
281
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
282
        self.source.lock_read()
265
283
        try:
266
 
            if mapped_refs is not None:
267
 
                stop_revisions = mapped_refs
268
 
            elif revision_id is not None:
269
 
                stop_revisions = [(None, revision_id)]
270
 
            elif fetch_spec is not None:
271
 
                stop_revisions = [(None, revid) for revid in fetch_spec.heads]
272
 
            else:
273
 
                stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
274
 
            self.fetch_objects(stop_revisions, roundtrip=True)
 
284
            todo = list(self.missing_revisions(stop_revisions))
 
285
            pb = ui.ui_factory.nested_progress_bar()
 
286
            try:
 
287
                object_generator = self._get_missing_objects_iterator(pb)
 
288
                for (revid, git_sha) in object_generator.import_revisions(
 
289
                    todo, roundtrip=True):
 
290
                    try:
 
291
                        self.mapping.revision_id_bzr_to_foreign(revid)
 
292
                    except errors.InvalidRevisionId:
 
293
                        self.target_refs[self.mapping.revid_as_refname(revid)] = git_sha
 
294
                self.target_store.add_objects(object_generator)
 
295
            finally:
 
296
                pb.finished()
275
297
        finally:
276
 
            self.source_store.unlock()
 
298
            self.source.unlock()
277
299
 
278
300
    @staticmethod
279
301
    def is_compatible(source, target):
284
306
 
285
307
class InterToRemoteGitRepository(InterToGitRepository):
286
308
 
287
 
    def fetch_refs(self, update_refs, lossy):
 
309
    def dfetch_refs(self, update_refs):
288
310
        """Import the gist of the ancestry of a particular revision."""
289
 
        if not lossy:
290
 
            raise NoPushSupport()
291
 
        unpeel_map = UnpeelMap.from_repository(self.source)
292
311
        revidmap = {}
293
312
        def determine_wants(old_refs):
294
313
            ret = {}
295
 
            self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
 
314
            self.old_refs = old_refs
296
315
            self.new_refs = update_refs(self.old_refs)
297
316
            for name, (gitid, revid) in self.new_refs.iteritems():
298
317
                if gitid is None:
299
 
                    git_sha = self.source_store._lookup_revision_sha1(revid)
300
 
                    ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
 
318
                    ret[name] = self.source_store._lookup_revision_sha1(revid)
301
319
                else:
302
320
                    ret[name] = gitid
303
321
            return ret
304
 
        self.source_store.lock_read()
 
322
        self.source.lock_read()
305
323
        try:
306
324
            new_refs = self.target.send_pack(determine_wants,
307
325
                    self.source_store.generate_lossy_pack_contents)
308
326
        finally:
309
 
            self.source_store.unlock()
310
 
        # FIXME: revidmap?
 
327
            self.source.unlock()
311
328
        return revidmap, self.old_refs, self.new_refs
312
329
 
 
330
    def fetch_refs(self, update_refs):
 
331
        raise NoPushSupport()
 
332
 
313
333
    @staticmethod
314
334
    def is_compatible(source, target):
315
335
        """Be compatible with GitRepository."""