/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

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Push implementation that simply prints message saying push is not supported."""
18
 
 
19
 
from dulwich.objects import ZERO_SHA
20
 
from dulwich.walk import Walker
21
 
 
22
 
from bzrlib import (
23
 
    errors,
24
 
    ui,
25
 
    )
26
 
from bzrlib.repository import (
27
 
    InterRepository,
28
 
    )
29
 
from bzrlib.revision import (
30
 
    NULL_REVISION,
31
 
    )
32
 
 
33
 
from bzrlib.plugins.git.errors import (
34
 
    NoPushSupport,
35
 
    )
36
 
from bzrlib.plugins.git.object_store import (
37
 
    get_object_store,
38
 
    )
39
 
from bzrlib.plugins.git.repository import (
40
 
    GitRepository,
41
 
    LocalGitRepository,
42
 
    GitRepositoryFormat,
43
 
    )
44
 
from bzrlib.plugins.git.remote import (
45
 
    RemoteGitRepository,
46
 
    )
47
 
from bzrlib.plugins.git.unpeel_map import (
48
 
    UnpeelMap,
49
 
    )
50
 
 
51
 
 
52
 
class MissingObjectsIterator(object):
53
 
    """Iterate over git objects that are missing from a target repository.
54
 
 
55
 
    """
56
 
 
57
 
    def __init__(self, store, source, pb=None):
58
 
        """Create a new missing objects iterator.
59
 
 
60
 
        """
61
 
        self.source = source
62
 
        self._object_store = store
63
 
        self._pending = []
64
 
        self.pb = pb
65
 
 
66
 
    def import_revisions(self, revids, roundtrip):
67
 
        """Import a set of revisions into this git repository.
68
 
 
69
 
        :param revids: Revision ids of revisions to import
70
 
        :param roundtrip: Whether to roundtrip bzr metadata
71
 
        """
72
 
        for i, revid in enumerate(revids):
73
 
            if self.pb:
74
 
                self.pb.update("pushing revisions", i, len(revids))
75
 
            git_commit = self.import_revision(revid, roundtrip)
76
 
            yield (revid, git_commit)
77
 
 
78
 
    def import_revision(self, revid, roundtrip):
79
 
        """Import a revision into this Git repository.
80
 
 
81
 
        :param revid: Revision id of the revision
82
 
        :param roundtrip: Whether to roundtrip bzr metadata
83
 
        """
84
 
        tree = self._object_store.tree_cache.revision_tree(revid)
85
 
        rev = self.source.get_revision(revid)
86
 
        commit = None
87
 
        for path, obj, ie in self._object_store._revision_to_objects(rev, tree,
88
 
            roundtrip):
89
 
            if obj.type_name == "commit":
90
 
                commit = obj
91
 
            self._pending.append((obj, path))
92
 
        return commit.id
93
 
 
94
 
    def __len__(self):
95
 
        return len(self._pending)
96
 
 
97
 
    def __iter__(self):
98
 
        return iter(self._pending)
99
 
 
100
 
 
101
 
class InterToGitRepository(InterRepository):
102
 
    """InterRepository that copies into a Git repository."""
103
 
 
104
 
    _matching_repo_format = GitRepositoryFormat()
105
 
 
106
 
    def __init__(self, source, target):
107
 
        super(InterToGitRepository, self).__init__(source, target)
108
 
        self.mapping = self.target.get_mapping()
109
 
        self.source_store = get_object_store(self.source, self.mapping)
110
 
 
111
 
    @staticmethod
112
 
    def _get_repo_format_to_test():
113
 
        return None
114
 
 
115
 
    def copy_content(self, revision_id=None, pb=None):
116
 
        """See InterRepository.copy_content."""
117
 
        self.fetch(revision_id, pb, find_ghosts=False)
118
 
 
119
 
    def fetch_refs(self, update_refs, lossy):
120
 
        """Fetch possibly roundtripped revisions into the target repository
121
 
        and update refs.
122
 
 
123
 
        :param update_refs: Generate refs to fetch. Receives dictionary
124
 
            with old refs (git shas), returns dictionary of new names to
125
 
            git shas.
126
 
        :param lossy: Whether to roundtrip
127
 
        :return: old refs, new refs
128
 
        """
129
 
        raise NotImplementedError(self.fetch_refs)
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
 
                # FIXME: This blindly takes the first revision
152
 
                missing_revids.add(self.source_store.lookup_git_sha(entry.commit.id)[0])
153
 
        finally:
154
 
            self.source_store.unlock()
155
 
        return self.source.revision_ids_to_search_result(missing_revids)
156
 
 
157
 
 
158
 
class InterToLocalGitRepository(InterToGitRepository):
159
 
    """InterBranch implementation between a Bazaar and a Git repository."""
160
 
 
161
 
    def __init__(self, source, target):
162
 
        super(InterToLocalGitRepository, self).__init__(source, target)
163
 
        self.target_store = self.target._git.object_store
164
 
        self.target_refs = self.target._git.refs
165
 
 
166
 
    def _revision_needs_fetching(self, sha_id, revid):
167
 
        if revid == NULL_REVISION:
168
 
            return False
169
 
        if sha_id is None:
170
 
            try:
171
 
                sha_id = self.source_store._lookup_revision_sha1(revid)
172
 
            except KeyError:
173
 
                return False
174
 
        try:
175
 
            return (sha_id not in self.target_store)
176
 
        except errors.NoSuchRevision:
177
 
            # Ghost, can't push
178
 
            return False
179
 
 
180
 
    def missing_revisions(self, stop_revisions):
181
 
        """Find the revisions that are missing from the target repository.
182
 
 
183
 
        :param stop_revisions: Revisions to check for (tuples with
184
 
            Git SHA1, bzr revid)
185
 
        :return: sequence of missing revisions, in topological order
186
 
        :raise: NoSuchRevision if the stop_revisions are not present in
187
 
            the source
188
 
        """
189
 
        revid_sha_map = {}
190
 
        stop_revids = []
191
 
        stop_sha1s = []
192
 
        for (sha1, revid) in stop_revisions:
193
 
            if sha1 is not None and revid is not None:
194
 
                revid_sha_map[revid] = sha1
195
 
            elif sha1 is not None:
196
 
                stop_sha1s.append(sha1)
197
 
            else:
198
 
                assert revid is not None
199
 
                stop_revids.append(revid)
200
 
        missing = []
201
 
        graph = self.source.get_graph()
202
 
        pb = ui.ui_factory.nested_progress_bar()
203
 
        try:
204
 
            for revid, _ in graph.iter_ancestry(stop_revids):
205
 
                assert type(revid) is str
206
 
                pb.update("determining revisions to fetch", len(missing))
207
 
                sha1 = revid_sha_map.get(revid)
208
 
                if self._revision_needs_fetching(sha1, revid):
209
 
                    missing.append(revid)
210
 
        finally:
211
 
            pb.finished()
212
 
        for sha1 in stop_sha1s:
213
 
            try:
214
 
                for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
215
 
                    missing.append(revid)
216
 
                    revid_sha_map[revid] = sha1
217
 
            except KeyError:
218
 
                continue
219
 
        return graph.iter_topo_order(missing)
220
 
 
221
 
    def _get_target_bzr_refs(self):
222
 
        """Return a dictionary with references.
223
 
 
224
 
        :return: Dictionary with reference names as keys and tuples
225
 
            with Git SHA, Bazaar revid as values.
226
 
        """
227
 
        bzr_refs = {}
228
 
        refs = {}
229
 
        for k in self.target._git.refs.allkeys():
230
 
            v = self.target._git.refs.read_ref(k)
231
 
            try:
232
 
                for (kind, type_data) in self.source_store.lookup_git_sha(v):
233
 
                    if kind == "commit" and self.source.has_revision(type_data[0]):
234
 
                        revid = type_data[0]
235
 
                        break
236
 
                else:
237
 
                    revid = None
238
 
            except KeyError:
239
 
                revid = None
240
 
            bzr_refs[k] = (v, revid)
241
 
        return bzr_refs
242
 
 
243
 
    def fetch_refs(self, update_refs, lossy):
244
 
        self.source_store.lock_read()
245
 
        try:
246
 
            old_refs = self._get_target_bzr_refs()
247
 
            new_refs = update_refs(old_refs)
248
 
            revidmap = self.fetch_objects(new_refs.values(), roundtrip=not lossy)
249
 
            for name, (gitid, revid) in new_refs.iteritems():
250
 
                if gitid is None:
251
 
                    try:
252
 
                        gitid = revidmap[revid][0]
253
 
                    except KeyError:
254
 
                        gitid = self.source_store._lookup_revision_sha1(revid)
255
 
                assert len(gitid) == 40
256
 
                self.target_refs[name] = gitid
257
 
        finally:
258
 
            self.source_store.unlock()
259
 
        return revidmap, old_refs, new_refs
260
 
 
261
 
    def fetch_objects(self, revs, roundtrip):
262
 
        todo = list(self.missing_revisions(revs))
263
 
        revidmap = {}
264
 
        pb = ui.ui_factory.nested_progress_bar()
265
 
        try:
266
 
            object_generator = self._get_missing_objects_iterator(pb)
267
 
            for (old_revid, git_sha) in object_generator.import_revisions(
268
 
                todo, roundtrip=roundtrip):
269
 
                try:
270
 
                    self.mapping.revision_id_bzr_to_foreign(old_revid)
271
 
                except errors.InvalidRevisionId:
272
 
                    self.target_refs[self.mapping.revid_as_refname(old_revid)] = git_sha
273
 
                if not roundtrip:
274
 
                    new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
275
 
                else:
276
 
                    new_revid = old_revid
277
 
                revidmap[old_revid] = (git_sha, new_revid)
278
 
            self.target_store.add_objects(object_generator)
279
 
            return revidmap
280
 
        finally:
281
 
            pb.finished()
282
 
 
283
 
    def _get_missing_objects_iterator(self, pb):
284
 
        return MissingObjectsIterator(self.source_store, self.source, pb)
285
 
 
286
 
    def dfetch(self, stop_revisions):
287
 
        """Import the gist of the ancestry of a particular revision."""
288
 
 
289
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
290
 
            fetch_spec=None, mapped_refs=None):
291
 
        if not self.mapping.roundtripping:
292
 
            raise NoPushSupport()
293
 
        self.source_store.lock_read()
294
 
        try:
295
 
            if mapped_refs is not None:
296
 
                stop_revisions = mapped_refs
297
 
            elif revision_id is not None:
298
 
                stop_revisions = [(None, revision_id)]
299
 
            elif fetch_spec is not None:
300
 
                stop_revisions = [(None, revid) for revid in fetch_spec.heads]
301
 
            else:
302
 
                stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
303
 
            self.fetch_objects(stop_revisions, roundtrip=True)
304
 
        finally:
305
 
            self.source_store.unlock()
306
 
 
307
 
    @staticmethod
308
 
    def is_compatible(source, target):
309
 
        """Be compatible with GitRepository."""
310
 
        return (not isinstance(source, GitRepository) and
311
 
                isinstance(target, LocalGitRepository))
312
 
 
313
 
 
314
 
class InterToRemoteGitRepository(InterToGitRepository):
315
 
 
316
 
    def fetch_refs(self, update_refs, lossy):
317
 
        """Import the gist of the ancestry of a particular revision."""
318
 
        if not lossy:
319
 
            raise NoPushSupport()
320
 
        unpeel_map = UnpeelMap.from_repository(self.source)
321
 
        revidmap = {}
322
 
        def determine_wants(old_refs):
323
 
            ret = {}
324
 
            self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
325
 
            self.new_refs = update_refs(self.old_refs)
326
 
            for name, (gitid, revid) in self.new_refs.iteritems():
327
 
                if gitid is None:
328
 
                    git_sha = self.source_store._lookup_revision_sha1(revid)
329
 
                    ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
330
 
                else:
331
 
                    ret[name] = gitid
332
 
            return ret
333
 
        self.source_store.lock_read()
334
 
        try:
335
 
            new_refs = self.target.send_pack(determine_wants,
336
 
                    self.source_store.generate_lossy_pack_contents)
337
 
        finally:
338
 
            self.source_store.unlock()
339
 
        # FIXME: revidmap?
340
 
        return revidmap, self.old_refs, self.new_refs
341
 
 
342
 
    @staticmethod
343
 
    def is_compatible(source, target):
344
 
        """Be compatible with GitRepository."""
345
 
        return (not isinstance(source, GitRepository) and
346
 
                isinstance(target, RemoteGitRepository))