1
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
 
 
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.
 
 
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.
 
 
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
 
 
17
"""Push implementation that simply prints message saying push is not supported."""
 
 
23
from bzrlib.repository import (
 
 
26
from bzrlib.revision import (
 
 
30
from bzrlib.plugins.git.errors import (
 
 
33
from bzrlib.plugins.git.object_store import (
 
 
36
from bzrlib.plugins.git.repository import (
 
 
41
from bzrlib.plugins.git.remote import (
 
 
46
class MissingObjectsIterator(object):
 
 
47
    """Iterate over git objects that are missing from a target repository.
 
 
51
    def __init__(self, store, source, pb=None):
 
 
52
        """Create a new missing objects iterator.
 
 
56
        self._object_store = store
 
 
60
    def import_revisions(self, revids, roundtrip):
 
 
61
        """Import a set of revisions into this git repository.
 
 
63
        :param revids: Revision ids of revisions to import
 
 
64
        :param roundtrip: Whether to roundtrip bzr metadata
 
 
66
        for i, revid in enumerate(revids):
 
 
68
                self.pb.update("pushing revisions", i, len(revids))
 
 
69
            git_commit = self.import_revision(revid, roundtrip)
 
 
70
            yield (revid, git_commit)
 
 
72
    def import_revision(self, revid, roundtrip):
 
 
73
        """Import a revision into this Git repository.
 
 
75
        :param revid: Revision id of the revision
 
 
76
        :param roundtrip: Whether to roundtrip bzr metadata
 
 
78
        tree = self._object_store.tree_cache.revision_tree(revid)
 
 
79
        rev = self.source.get_revision(revid)
 
 
81
        for path, obj, ie in self._object_store._revision_to_objects(rev, tree,
 
 
83
            if obj.type_name == "commit":
 
 
85
            self._pending.append((obj, path))
 
 
89
        return len(self._pending)
 
 
92
        return iter(self._pending)
 
 
95
class InterToGitRepository(InterRepository):
 
 
96
    """InterRepository that copies into a Git repository."""
 
 
98
    _matching_repo_format = GitRepositoryFormat()
 
 
100
    def __init__(self, source, target):
 
 
101
        super(InterToGitRepository, self).__init__(source, target)
 
 
102
        self.mapping = self.target.get_mapping()
 
 
103
        self.source_store = BazaarObjectStore(self.source, self.mapping)
 
 
106
    def _get_repo_format_to_test():
 
 
109
    def copy_content(self, revision_id=None, pb=None):
 
 
110
        """See InterRepository.copy_content."""
 
 
111
        self.fetch(revision_id, pb, find_ghosts=False)
 
 
113
    def dfetch_refs(self, update_refs):
 
 
114
        """Fetch non-roundtripped revisions into the target repository.
 
 
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
 
 
121
        raise NotImplementedError(self.dfetch_refs)
 
 
123
    def fetch_refs(self, update_refs):
 
 
124
        """Fetch possibly roundtripped revisions into the target repository.
 
 
126
        :param update_refs: Generate refs to fetch. Receives dictionary
 
 
127
            with old refs (git shas), returns dictionary of new names to
 
 
129
        :return: old refs, new refs
 
 
131
        raise NotImplementedError(self.fetch_refs)
 
 
134
class InterToLocalGitRepository(InterToGitRepository):
 
 
135
    """InterBranch implementation between a Bazaar and a Git repository."""
 
 
137
    def __init__(self, source, target):
 
 
138
        super(InterToLocalGitRepository, self).__init__(source, target)
 
 
139
        self.target_store = self.target._git.object_store
 
 
140
        self.target_refs = self.target._git.refs
 
 
142
    def _revision_needs_fetching(self, sha_id, revid):
 
 
143
        if revid == NULL_REVISION:
 
 
147
                sha_id = self.source_store._lookup_revision_sha1(revid)
 
 
151
            return (sha_id not in self.target_store)
 
 
152
        except errors.NoSuchRevision:
 
 
156
    def missing_revisions(self, stop_revisions):
 
 
157
        """Find the revisions that are missing from the target repository.
 
 
159
        :param stop_revisions: Revisions to check for (tuples with
 
 
161
        :return: sequence of missing revisions, in topological order
 
 
162
        :raise: NoSuchRevision if the stop_revisions are not present in
 
 
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)
 
 
174
                assert revid is not None
 
 
175
                stop_revids.append(revid)
 
 
177
        graph = self.source.get_graph()
 
 
178
        pb = ui.ui_factory.nested_progress_bar()
 
 
180
            for revid, _ in graph.iter_ancestry(stop_revids):
 
 
181
                assert type(revid) is str
 
 
182
                pb.update("determining revisions to fetch", len(missing))
 
 
183
                sha1 = revid_sha_map.get(revid)
 
 
184
                if self._revision_needs_fetching(sha1, revid):
 
 
185
                    missing.append(revid)
 
 
188
        for sha1 in stop_sha1s:
 
 
190
                (kind, (revid, tree_sha, verifiers)) = self.source_store.lookup_git_sha(sha1)
 
 
194
                missing.append(revid)
 
 
195
                revid_sha_map[revid] = sha1
 
 
196
        return graph.iter_topo_order(missing)
 
 
198
    def _get_target_bzr_refs(self):
 
 
199
        """Return a dictionary with references.
 
 
201
        :return: Dictionary with reference names as keys and tuples
 
 
202
            with Git SHA, Bazaar revid as values.
 
 
205
        refs = self.target._git.get_refs()
 
 
206
        for k, v in refs.iteritems():
 
 
208
                (kind, type_data) = self.source_store.lookup_git_sha(v)
 
 
216
            bzr_refs[k] = (v, revid)
 
 
219
    def fetch_refs(self, update_refs):
 
 
220
        self.source.lock_read()
 
 
222
            old_refs = self._get_target_bzr_refs()
 
 
223
            new_refs = update_refs(old_refs)
 
 
224
            self.fetch(mapped_refs=new_refs.values())
 
 
227
        return old_refs, new_refs
 
 
229
    def dfetch_refs(self, update_refs):
 
 
230
        self.source.lock_read()
 
 
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():
 
 
238
                        gitid = gitidmap[revid]
 
 
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])
 
 
245
        return revidmap, old_refs, new_refs
 
 
247
    def _get_missing_objects_iterator(self, pb):
 
 
248
        return MissingObjectsIterator(self.source_store, self.source, pb)
 
 
250
    def dfetch(self, stop_revisions):
 
 
251
        """Import the gist of the ancestry of a particular revision."""
 
 
254
        self.source.lock_read()
 
 
256
            todo = list(self.missing_revisions(stop_revisions))
 
 
257
            pb = ui.ui_factory.nested_progress_bar()
 
 
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)
 
 
270
        return revidmap, gitidmap
 
 
272
    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)]
 
 
278
        elif fetch_spec is not None:
 
 
279
            stop_revisions = [(None, revid) for revid in fetch_spec.heads]
 
 
281
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
 
282
        self.source.lock_read()
 
 
284
            todo = list(self.missing_revisions(stop_revisions))
 
 
285
            pb = ui.ui_factory.nested_progress_bar()
 
 
287
                object_generator = self._get_missing_objects_iterator(pb)
 
 
288
                for (revid, git_sha) in object_generator.import_revisions(
 
 
289
                    todo, roundtrip=True):
 
 
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)
 
 
301
    def is_compatible(source, target):
 
 
302
        """Be compatible with GitRepository."""
 
 
303
        return (not isinstance(source, GitRepository) and
 
 
304
                isinstance(target, LocalGitRepository))
 
 
307
class InterToRemoteGitRepository(InterToGitRepository):
 
 
309
    def dfetch_refs(self, update_refs):
 
 
310
        """Import the gist of the ancestry of a particular revision."""
 
 
312
        def determine_wants(old_refs):
 
 
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():
 
 
318
                    ret[name] = self.source_store._lookup_revision_sha1(revid)
 
 
322
        self.source.lock_read()
 
 
324
            new_refs = self.target.send_pack(determine_wants,
 
 
325
                    self.source_store.generate_lossy_pack_contents)
 
 
328
        return revidmap, self.old_refs, self.new_refs
 
 
330
    def fetch_refs(self, update_refs):
 
 
331
        raise NoPushSupport()
 
 
334
    def is_compatible(source, target):
 
 
335
        """Be compatible with GitRepository."""
 
 
336
        return (not isinstance(source, GitRepository) and
 
 
337
                isinstance(target, RemoteGitRepository))