/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-05 00:05:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5206.
  • Revision ID: robertc@robertcollins.net-20100505000529-ltmllyms5watqj5u
Make 'pydoc bzrlib.tests.build_tree_shape' useful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 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 bzrlib import (
20
 
    ui,
21
 
    )
22
 
from bzrlib.repository import (
23
 
    InterRepository,
24
 
    )
25
 
from bzrlib.revision import (
26
 
    NULL_REVISION,
27
 
    )
28
 
 
29
 
from bzrlib.plugins.git.errors import (
30
 
    NoPushSupport,
31
 
    )
32
 
from bzrlib.plugins.git.object_store import (
33
 
    BazaarObjectStore,
34
 
    )
35
 
from bzrlib.plugins.git.repository import (
36
 
    GitRepository,
37
 
    LocalGitRepository,
38
 
    GitRepositoryFormat,
39
 
    )
40
 
from bzrlib.plugins.git.remote import (
41
 
    RemoteGitRepository,
42
 
    )
43
 
 
44
 
 
45
 
class MissingObjectsIterator(object):
46
 
    """Iterate over git objects that are missing from a target repository.
47
 
 
48
 
    """
49
 
 
50
 
    def __init__(self, source, mapping, pb=None):
51
 
        """Create a new missing objects iterator.
52
 
 
53
 
        """
54
 
        self.source = source
55
 
        self._object_store = BazaarObjectStore(self.source, mapping)
56
 
        self._revids = set()
57
 
        self._sent_shas = set()
58
 
        self._pending = []
59
 
        self.pb = pb
60
 
 
61
 
    def import_revisions(self, revids):
62
 
        self._revids.update(revids)
63
 
        for i, revid in enumerate(revids):
64
 
            if self.pb:
65
 
                self.pb.update("pushing revisions", i, len(revids))
66
 
            git_commit = self.import_revision(revid)
67
 
            yield (revid, git_commit)
68
 
 
69
 
    def need_sha(self, sha):
70
 
        if sha in self._sent_shas:
71
 
            return False
72
 
        (type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
73
 
        assert type in ("blob", "tree")
74
 
        if revid in self._revids:
75
 
            # Not sent yet, and part of the set of revisions to send
76
 
            return True
77
 
        # Not changed in the revisions to send, so either not necessary
78
 
        # or already present remotely (as git doesn't do ghosts)
79
 
        return False
80
 
 
81
 
    def queue(self, sha, obj, path, ie=None, inv=None):
82
 
        if obj is None:
83
 
            obj = (ie, inv)
84
 
        self._pending.append((obj, path))
85
 
        self._sent_shas.add(sha)
86
 
 
87
 
    def import_revision(self, revid):
88
 
        """Import the gist of a revision into this Git repository.
89
 
 
90
 
        """
91
 
        inv = self.source.get_inventory(revid)
92
 
        todo = [inv.root]
93
 
        tree_sha = None
94
 
        while todo:
95
 
            ie = todo.pop()
96
 
            (sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
97
 
            if ie.parent_id is None:
98
 
                tree_sha = sha
99
 
            if not self.need_sha(sha):
100
 
                continue
101
 
            self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
102
 
            if ie.kind == "directory":
103
 
                todo.extend(ie.children.values())
104
 
        assert tree_sha is not None
105
 
        commit = self._object_store._get_commit(revid, tree_sha)
106
 
        self.queue(commit.id, commit, None)
107
 
        return commit.id
108
 
 
109
 
    def __len__(self):
110
 
        return len(self._pending)
111
 
 
112
 
    def __iter__(self):
113
 
        for i, (object, path) in enumerate(self._pending):
114
 
            if self.pb:
115
 
                self.pb.update("writing pack objects", i, len(self))
116
 
            if isinstance(object, tuple):
117
 
                object = self._object_store._get_ie_object(*object)
118
 
            yield (object, path)   
119
 
 
120
 
 
121
 
class InterToGitRepository(InterRepository):
122
 
    """InterRepository that copies into a Git repository."""
123
 
 
124
 
    _matching_repo_format = GitRepositoryFormat()
125
 
 
126
 
    def __init__(self, source, target):
127
 
        super(InterToGitRepository, self).__init__(source, target)
128
 
        self.mapping = self.target.get_mapping()
129
 
 
130
 
    @staticmethod
131
 
    def _get_repo_format_to_test():
132
 
        return None
133
 
 
134
 
    def copy_content(self, revision_id=None, pb=None):
135
 
        """See InterRepository.copy_content."""
136
 
        self.fetch(revision_id, pb, find_ghosts=False)
137
 
 
138
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
139
 
            fetch_spec=None):
140
 
        raise NoPushSupport()
141
 
 
142
 
 
143
 
class InterToLocalGitRepository(InterToGitRepository):
144
 
 
145
 
    def missing_revisions(self, stop_revision):
146
 
        if stop_revision is None:
147
 
            raise NotImplementedError
148
 
        missing = []
149
 
        pb = ui.ui_factory.nested_progress_bar()
150
 
        try:
151
 
            graph = self.source.get_graph()
152
 
            for revid, _ in graph.iter_ancestry([stop_revision]):
153
 
                pb.update("determining revisions to fetch", len(missing))
154
 
                if not self.target.has_revision(revid):
155
 
                    missing.append(revid)
156
 
            return graph.iter_topo_order(missing)
157
 
        finally:
158
 
            pb.finished()
159
 
 
160
 
    def dfetch_refs(self, refs):
161
 
        revidmap = {}
162
 
        new_refs = {}
163
 
        for name, revid in refs.iteritems():
164
 
            newrevidmap, newgitidmap = self.dfetch(revid)
165
 
            revidmap.update(newrevidmap)
166
 
            if revid in newgitidmap:
167
 
                gitid = newgitidmap[revid]
168
 
            else:
169
 
                gitid, _ = self.mapping.revision_id_bzr_to_foreign(revid)
170
 
            self.target._git.refs[name] = gitid
171
 
            new_refs[name] = gitid
172
 
        return revidmap, new_refs
173
 
 
174
 
    def dfetch(self, stop_revision=None):
175
 
        """Import the gist of the ancestry of a particular revision."""
176
 
        gitidmap = {}
177
 
        revidmap = {}
178
 
        self.source.lock_read()
179
 
        try:
180
 
            todo = [revid for revid in self.missing_revisions(stop_revision) if revid != NULL_REVISION]
181
 
            pb = ui.ui_factory.nested_progress_bar()
182
 
            try:
183
 
                object_generator = MissingObjectsIterator(self.source, self.mapping, pb)
184
 
                for old_bzr_revid, git_commit in object_generator.import_revisions(
185
 
                    todo):
186
 
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
187
 
                    revidmap[old_bzr_revid] = new_bzr_revid
188
 
                    gitidmap[old_bzr_revid] = git_commit
189
 
                self.target._git.object_store.add_objects(object_generator) 
190
 
            finally:
191
 
                pb.finished()
192
 
        finally:
193
 
            self.source.unlock()
194
 
        return revidmap, gitidmap
195
 
 
196
 
    @staticmethod
197
 
    def is_compatible(source, target):
198
 
        """Be compatible with GitRepository."""
199
 
        return (not isinstance(source, GitRepository) and 
200
 
                isinstance(target, LocalGitRepository))
201
 
 
202
 
 
203
 
class InterToRemoteGitRepository(InterToGitRepository):
204
 
 
205
 
    def dfetch_refs(self, new_refs):
206
 
        """Import the gist of the ancestry of a particular revision."""
207
 
        revidmap = {}
208
 
        def determine_wants(refs):
209
 
            ret = {}
210
 
            for name, revid in new_refs.iteritems():
211
 
                ret[name] = store._lookup_revision_sha1(revid)
212
 
            return ret
213
 
        self.source.lock_read()
214
 
        try:
215
 
            store = BazaarObjectStore(self.source, self.mapping)
216
 
            def generate_blob_contents(have, want):
217
 
                return store.iter_shas(store.find_missing_objects(have, want))
218
 
            new_refs = self.target.send_pack(determine_wants,
219
 
                    generate_blob_contents)
220
 
        finally:
221
 
            self.source.unlock()
222
 
        return revidmap, new_refs
223
 
 
224
 
    @staticmethod
225
 
    def is_compatible(source, target):
226
 
        """Be compatible with GitRepository."""
227
 
        return (not isinstance(source, GitRepository) and 
228
 
                isinstance(target, RemoteGitRepository))