/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:
20
20
    errors,
21
21
    ui,
22
22
    )
23
 
from bzrlib.graph import (
24
 
    PendingAncestryResult,
25
 
    )
26
23
from bzrlib.repository import (
27
24
    InterRepository,
28
25
    )
61
58
        self.pb = pb
62
59
 
63
60
    def import_revisions(self, revids, roundtrip):
 
61
        """Import a set of revisions into this git repository.
 
62
 
 
63
        :param revids: Revision ids of revisions to import
 
64
        :param roundtrip: Whether to roundtrip bzr metadata
 
65
        """
64
66
        for i, revid in enumerate(revids):
65
67
            if self.pb:
66
68
                self.pb.update("pushing revisions", i, len(revids))
68
70
            yield (revid, git_commit)
69
71
 
70
72
    def import_revision(self, revid, roundtrip):
71
 
        """Import the gist of a revision into this Git repository.
 
73
        """Import a revision into this Git repository.
72
74
 
 
75
        :param revid: Revision id of the revision
 
76
        :param roundtrip: Whether to roundtrip bzr metadata
73
77
        """
74
78
        tree = self._object_store.tree_cache.revision_tree(revid)
75
79
        rev = self.source.get_revision(revid)
106
110
        """See InterRepository.copy_content."""
107
111
        self.fetch(revision_id, pb, find_ghosts=False)
108
112
 
 
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.
 
125
 
 
126
        :param update_refs: Generate refs to fetch. Receives dictionary
 
127
            with old refs (git shas), returns dictionary of new names to
 
128
            git shas.
 
129
        :return: old refs, new refs
 
130
        """
 
131
        raise NotImplementedError(self.fetch_refs)
 
132
 
109
133
 
110
134
class InterToLocalGitRepository(InterToGitRepository):
 
135
    """InterBranch implementation between a Bazaar and a Git repository."""
111
136
 
112
137
    def __init__(self, source, target):
113
138
        super(InterToLocalGitRepository, self).__init__(source, target)
114
139
        self.target_store = self.target._git.object_store
115
140
        self.target_refs = self.target._git.refs
116
141
 
117
 
    def missing_revisions(self, stop_revisions, check_revid):
 
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)
118
176
        missing = []
119
177
        graph = self.source.get_graph()
120
178
        pb = ui.ui_factory.nested_progress_bar()
121
179
        try:
122
 
            for revid, _ in graph.iter_ancestry(stop_revisions):
 
180
            for revid, _ in graph.iter_ancestry(stop_revids):
 
181
                assert type(revid) is str
123
182
                pb.update("determining revisions to fetch", len(missing))
124
 
                if not check_revid(revid):
 
183
                sha1 = revid_sha_map.get(revid)
 
184
                if self._revision_needs_fetching(sha1, revid):
125
185
                    missing.append(revid)
126
186
        finally:
127
187
            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
128
196
        return graph.iter_topo_order(missing)
129
197
 
130
 
    def fetch_refs(self, refs):
131
 
        fetch_spec = PendingAncestryResult(refs.values(), self.source)
132
 
        self.fetch(fetch_spec=fetch_spec)
 
198
    def _get_target_bzr_refs(self):
 
199
        """Return a dictionary with references.
133
200
 
134
 
    def dfetch_refs(self, refs):
135
 
        old_refs = self.target._git.get_refs()
136
 
        new_refs = dict(old_refs)
137
 
        revidmap, gitidmap = self.dfetch(refs.values())
138
 
        for name, revid in refs.iteritems():
 
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():
139
207
            try:
140
 
                gitid = gitidmap[revid]
 
208
                (kind, type_data) = self.source_store.lookup_git_sha(v)
141
209
            except KeyError:
142
 
                gitid = self.source_store._lookup_revision_sha1(revid)
143
 
            self.target._git.refs[name] = gitid
144
 
            new_refs[name] = gitid
 
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
    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())
 
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()
145
245
        return revidmap, old_refs, new_refs
146
246
 
147
 
    def _find_missing_revs(self, stop_revisions):
148
 
        def check_revid(revid):
149
 
            if revid == NULL_REVISION:
150
 
                return True
151
 
            sha_id = self.source_store._lookup_revision_sha1(revid)
152
 
            try:
153
 
                return (sha_id in self.target_store)
154
 
            except errors.NoSuchRevision:
155
 
                # Ghost, can't push
156
 
                return True
157
 
        return list(self.missing_revisions(stop_revisions, check_revid))
 
247
    def _get_missing_objects_iterator(self, pb):
 
248
        return MissingObjectsIterator(self.source_store, self.source, pb)
158
249
 
159
250
    def dfetch(self, stop_revisions):
160
251
        """Import the gist of the ancestry of a particular revision."""
162
253
        revidmap = {}
163
254
        self.source.lock_read()
164
255
        try:
165
 
            todo = self._find_missing_revs(stop_revisions)
 
256
            todo = list(self.missing_revisions(stop_revisions))
166
257
            pb = ui.ui_factory.nested_progress_bar()
167
258
            try:
168
 
                object_generator = MissingObjectsIterator(self.source_store,
169
 
                    self.source, pb)
 
259
                object_generator = self._get_missing_objects_iterator(pb)
170
260
                for old_bzr_revid, git_commit in object_generator.import_revisions(
171
261
                    todo, roundtrip=False):
172
262
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
180
270
        return revidmap, gitidmap
181
271
 
182
272
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
183
 
            fetch_spec=None):
184
 
        if revision_id is not None:
185
 
            stop_revisions = [revision_id]
 
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)]
186
278
        elif fetch_spec is not None:
187
 
            stop_revisions = fetch_spec.heads
 
279
            stop_revisions = [(None, revid) for revid in fetch_spec.heads]
188
280
        else:
189
 
            stop_revisions = self.source.all_revision_ids()
 
281
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
190
282
        self.source.lock_read()
191
283
        try:
192
 
            todo = self._find_missing_revs(stop_revisions)
 
284
            todo = list(self.missing_revisions(stop_revisions))
193
285
            pb = ui.ui_factory.nested_progress_bar()
194
286
            try:
195
 
                object_generator = MissingObjectsIterator(self.source_store,
196
 
                    self.source, pb)
 
287
                object_generator = self._get_missing_objects_iterator(pb)
197
288
                for (revid, git_sha) in object_generator.import_revisions(
198
289
                    todo, roundtrip=True):
199
290
                    try:
215
306
 
216
307
class InterToRemoteGitRepository(InterToGitRepository):
217
308
 
218
 
    def dfetch_refs(self, new_refs):
 
309
    def dfetch_refs(self, update_refs):
219
310
        """Import the gist of the ancestry of a particular revision."""
220
311
        revidmap = {}
221
 
        old_refs = {}
222
 
        def determine_wants(refs):
 
312
        def determine_wants(old_refs):
223
313
            ret = {}
224
 
            old_refs.update(new_refs)
225
 
            for name, revid in new_refs.iteritems():
226
 
                ret[name] = self.source_store._lookup_revision_sha1(revid)
 
314
            self.old_refs = old_refs
 
315
            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
227
321
            return ret
228
322
        self.source.lock_read()
229
323
        try:
231
325
                    self.source_store.generate_lossy_pack_contents)
232
326
        finally:
233
327
            self.source.unlock()
234
 
        return revidmap, old_refs, new_refs
 
328
        return revidmap, self.old_refs, self.new_refs
235
329
 
236
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
237
 
            fetch_spec=None):
 
330
    def fetch_refs(self, update_refs):
238
331
        raise NoPushSupport()
239
332
 
240
333
    @staticmethod