/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

Escape slashes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Push implementation that simply prints message saying push is not supported."""
18
18
 
 
19
from dulwich.objects import ZERO_SHA
 
20
from dulwich.walk import Walker
 
21
 
19
22
from bzrlib import (
20
23
    errors,
21
24
    ui,
31
34
    NoPushSupport,
32
35
    )
33
36
from bzrlib.plugins.git.object_store import (
34
 
    BazaarObjectStore,
 
37
    get_object_store,
35
38
    )
36
39
from bzrlib.plugins.git.repository import (
37
40
    GitRepository,
41
44
from bzrlib.plugins.git.remote import (
42
45
    RemoteGitRepository,
43
46
    )
 
47
from bzrlib.plugins.git.unpeel_map import (
 
48
    UnpeelMap,
 
49
    )
44
50
 
45
51
 
46
52
class MissingObjectsIterator(object):
100
106
    def __init__(self, source, target):
101
107
        super(InterToGitRepository, self).__init__(source, target)
102
108
        self.mapping = self.target.get_mapping()
103
 
        self.source_store = BazaarObjectStore(self.source, self.mapping)
 
109
        self.source_store = get_object_store(self.source, self.mapping)
104
110
 
105
111
    @staticmethod
106
112
    def _get_repo_format_to_test():
110
116
        """See InterRepository.copy_content."""
111
117
        self.fetch(revision_id, pb, find_ghosts=False)
112
118
 
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
    def fetch_refs(self, update_refs, lossy):
 
120
        """Fetch possibly roundtripped revisions into the target repository
 
121
        and update refs.
125
122
 
126
123
        :param update_refs: Generate refs to fetch. Receives dictionary
127
124
            with old refs (git shas), returns dictionary of new names to
128
125
            git shas.
 
126
        :param lossy: Whether to roundtrip
129
127
        :return: old refs, new refs
130
128
        """
131
129
        raise NotImplementedError(self.fetch_refs)
132
130
 
 
131
    def search_missing_revision_ids(self,
 
132
            find_ghosts=True, revision_ids=None, if_present_ids=None,
 
133
            limit=None):
 
134
        git_shas = []
 
135
        todo = []
 
136
        if revision_ids:
 
137
            todo.extend(revision_ids)
 
138
        if if_present_ids:
 
139
            todo.extend(revision_ids)
 
140
        self.source_store.lock_read()
 
141
        try:
 
142
            for revid in revision_ids:
 
143
                if revid == NULL_REVISION:
 
144
                    continue
 
145
                git_sha = self.source_store._lookup_revision_sha1(revid)
 
146
                git_shas.append(git_sha)
 
147
            walker = Walker(self.source_store,
 
148
                include=git_shas, exclude=[sha for sha in self.target._git.get_refs().values() if sha != ZERO_SHA])
 
149
            missing_revids = set()
 
150
            for entry in walker:
 
151
                for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
 
152
                    if kind == "commit":
 
153
                        missing_revids.add(type_data[0])
 
154
        finally:
 
155
            self.source_store.unlock()
 
156
        return self.source.revision_ids_to_search_result(missing_revids)
 
157
 
133
158
 
134
159
class InterToLocalGitRepository(InterToGitRepository):
135
160
    """InterBranch implementation between a Bazaar and a Git repository."""
187
212
            pb.finished()
188
213
        for sha1 in stop_sha1s:
189
214
            try:
190
 
                (kind, (revid, tree_sha, verifiers)) = self.source_store.lookup_git_sha(sha1)
 
215
                for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
 
216
                    missing.append(revid)
 
217
                    revid_sha_map[revid] = sha1
191
218
            except KeyError:
192
219
                continue
193
 
            else:
194
 
                missing.append(revid)
195
 
                revid_sha_map[revid] = sha1
196
220
        return graph.iter_topo_order(missing)
197
221
 
198
222
    def _get_target_bzr_refs(self):
202
226
            with Git SHA, Bazaar revid as values.
203
227
        """
204
228
        bzr_refs = {}
205
 
        refs = self.target._git.get_refs()
206
 
        for k, v in refs.iteritems():
 
229
        refs = {}
 
230
        for k in self.target._git.refs.allkeys():
 
231
            v = self.target._git.refs.read_ref(k)
207
232
            try:
208
 
                (kind, type_data) = self.source_store.lookup_git_sha(v)
 
233
                for (kind, type_data) in self.source_store.lookup_git_sha(v):
 
234
                    if kind == "commit" and self.source.has_revision(type_data[0]):
 
235
                        revid = type_data[0]
 
236
                        break
 
237
                else:
 
238
                    revid = None
209
239
            except KeyError:
210
240
                revid = None
211
 
            else:
212
 
                if kind == "commit":
213
 
                    revid = type_data[0]
214
 
                else:
215
 
                    revid = None
216
241
            bzr_refs[k] = (v, revid)
217
242
        return bzr_refs
218
243
 
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())
 
244
    def fetch_refs(self, update_refs, lossy):
 
245
        self.source_store.lock_read()
 
246
        try:
 
247
            old_refs = self._get_target_bzr_refs()
 
248
            new_refs = update_refs(old_refs)
 
249
            revidmap = self.fetch_objects(new_refs.values(), roundtrip=not lossy)
235
250
            for name, (gitid, revid) in new_refs.iteritems():
236
251
                if gitid is None:
237
252
                    try:
238
 
                        gitid = gitidmap[revid]
 
253
                        gitid = revidmap[revid][0]
239
254
                    except KeyError:
240
255
                        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])
 
256
                assert len(gitid) == 40
 
257
                self.target_refs[name] = gitid
243
258
        finally:
244
 
            self.source.unlock()
 
259
            self.source_store.unlock()
245
260
        return revidmap, old_refs, new_refs
246
261
 
 
262
    def fetch_objects(self, revs, roundtrip):
 
263
        todo = list(self.missing_revisions(revs))
 
264
        revidmap = {}
 
265
        pb = ui.ui_factory.nested_progress_bar()
 
266
        try:
 
267
            object_generator = self._get_missing_objects_iterator(pb)
 
268
            for (old_revid, git_sha) in object_generator.import_revisions(
 
269
                todo, roundtrip=roundtrip):
 
270
                try:
 
271
                    self.mapping.revision_id_bzr_to_foreign(old_revid)
 
272
                except errors.InvalidRevisionId:
 
273
                    self.target_refs[self.mapping.revid_as_refname(old_revid)] = git_sha
 
274
                if not roundtrip:
 
275
                    new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
 
276
                else:
 
277
                    new_revid = old_revid
 
278
                revidmap[old_revid] = (git_sha, new_revid)
 
279
            self.target_store.add_objects(object_generator)
 
280
            return revidmap
 
281
        finally:
 
282
            pb.finished()
 
283
 
247
284
    def _get_missing_objects_iterator(self, pb):
248
285
        return MissingObjectsIterator(self.source_store, self.source, pb)
249
286
 
250
287
    def dfetch(self, stop_revisions):
251
288
        """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
271
289
 
272
290
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
273
291
            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)]
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()
 
292
        if not self.mapping.roundtripping:
 
293
            raise NoPushSupport()
 
294
        self.source_store.lock_read()
283
295
        try:
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()
 
296
            if mapped_refs is not None:
 
297
                stop_revisions = mapped_refs
 
298
            elif revision_id is not None:
 
299
                stop_revisions = [(None, revision_id)]
 
300
            elif fetch_spec is not None:
 
301
                recipe = fetch_spec.get_recipe()
 
302
                if recipe[0] in ("search", "proxy-search"):
 
303
                    stop_revisions = [(None, revid) for revid in recipe[1]]
 
304
                else:
 
305
                    raise AssertionError("Unsupported search result type %s" % recipe[0])
 
306
            else:
 
307
                stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
308
            self.fetch_objects(stop_revisions, roundtrip=True)
297
309
        finally:
298
 
            self.source.unlock()
 
310
            self.source_store.unlock()
299
311
 
300
312
    @staticmethod
301
313
    def is_compatible(source, target):
306
318
 
307
319
class InterToRemoteGitRepository(InterToGitRepository):
308
320
 
309
 
    def dfetch_refs(self, update_refs):
 
321
    def fetch_refs(self, update_refs, lossy):
310
322
        """Import the gist of the ancestry of a particular revision."""
 
323
        if not lossy:
 
324
            raise NoPushSupport()
 
325
        unpeel_map = UnpeelMap.from_repository(self.source)
311
326
        revidmap = {}
312
327
        def determine_wants(old_refs):
313
328
            ret = {}
314
 
            self.old_refs = old_refs
 
329
            self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
315
330
            self.new_refs = update_refs(self.old_refs)
316
331
            for name, (gitid, revid) in self.new_refs.iteritems():
317
332
                if gitid is None:
318
 
                    ret[name] = self.source_store._lookup_revision_sha1(revid)
 
333
                    git_sha = self.source_store._lookup_revision_sha1(revid)
 
334
                    ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
319
335
                else:
320
336
                    ret[name] = gitid
321
337
            return ret
322
 
        self.source.lock_read()
 
338
        self.source_store.lock_read()
323
339
        try:
324
340
            new_refs = self.target.send_pack(determine_wants,
325
341
                    self.source_store.generate_lossy_pack_contents)
326
342
        finally:
327
 
            self.source.unlock()
 
343
            self.source_store.unlock()
 
344
        # FIXME: revidmap?
328
345
        return revidmap, self.old_refs, self.new_refs
329
346
 
330
 
    def fetch_refs(self, update_refs):
331
 
        raise NoPushSupport()
332
 
 
333
347
    @staticmethod
334
348
    def is_compatible(source, target):
335
349
        """Be compatible with GitRepository."""