/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

ImportĀ RemoteGitBranch._get_config().

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