/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

Use blob.chunked.

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
    errors,
 
21
    ui,
 
22
    )
 
23
from bzrlib.repository import (
 
24
    InterRepository,
 
25
    )
 
26
from bzrlib.revision import (
 
27
    NULL_REVISION,
 
28
    )
 
29
 
 
30
from bzrlib.plugins.git.errors import (
 
31
    NoPushSupport,
 
32
    )
 
33
from bzrlib.plugins.git.object_store import (
 
34
    BazaarObjectStore,
 
35
    )
 
36
from bzrlib.plugins.git.repository import (
 
37
    GitRepository,
 
38
    LocalGitRepository,
 
39
    GitRepositoryFormat,
 
40
    )
 
41
from bzrlib.plugins.git.remote import (
 
42
    RemoteGitRepository,
 
43
    )
 
44
 
 
45
 
 
46
class MissingObjectsIterator(object):
 
47
    """Iterate over git objects that are missing from a target repository.
 
48
 
 
49
    """
 
50
 
 
51
    def __init__(self, store, source, pb=None):
 
52
        """Create a new missing objects iterator.
 
53
 
 
54
        """
 
55
        self.source = source
 
56
        self._object_store = store
 
57
        self._pending = []
 
58
        self.pb = pb
 
59
 
 
60
    def import_revisions(self, revids):
 
61
        for i, revid in enumerate(revids):
 
62
            if self.pb:
 
63
                self.pb.update("pushing revisions", i, len(revids))
 
64
            git_commit = self.import_revision(revid)
 
65
            yield (revid, git_commit)
 
66
 
 
67
    def import_revision(self, revid):
 
68
        """Import the gist of a revision into this Git repository.
 
69
 
 
70
        """
 
71
        inv = self._object_store.parent_invs_cache.get_inventory(revid)
 
72
        rev = self.source.get_revision(revid)
 
73
        commit = None
 
74
        for path, obj, ie in self._object_store._revision_to_objects(rev, inv):
 
75
            if obj.type_name == "commit":
 
76
                commit = obj
 
77
            self._pending.append((obj, path))
 
78
        return commit.id
 
79
 
 
80
    def __len__(self):
 
81
        return len(self._pending)
 
82
 
 
83
    def __iter__(self):
 
84
        return iter(self._pending)
 
85
 
 
86
 
 
87
class InterToGitRepository(InterRepository):
 
88
    """InterRepository that copies into a Git repository."""
 
89
 
 
90
    _matching_repo_format = GitRepositoryFormat()
 
91
 
 
92
    def __init__(self, source, target):
 
93
        super(InterToGitRepository, self).__init__(source, target)
 
94
        self.mapping = self.target.get_mapping()
 
95
        self.source_store = BazaarObjectStore(self.source, self.mapping)
 
96
 
 
97
    @staticmethod
 
98
    def _get_repo_format_to_test():
 
99
        return None
 
100
 
 
101
    def copy_content(self, revision_id=None, pb=None):
 
102
        """See InterRepository.copy_content."""
 
103
        self.fetch(revision_id, pb, find_ghosts=False)
 
104
 
 
105
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
 
106
            fetch_spec=None):
 
107
        raise NoPushSupport()
 
108
 
 
109
 
 
110
class InterToLocalGitRepository(InterToGitRepository):
 
111
 
 
112
    def missing_revisions(self, stop_revisions, check_revid):
 
113
        missing = []
 
114
        pb = ui.ui_factory.nested_progress_bar()
 
115
        try:
 
116
            graph = self.source.get_graph()
 
117
            for revid, _ in graph.iter_ancestry(stop_revisions):
 
118
                pb.update("determining revisions to fetch", len(missing))
 
119
                if not check_revid(revid):
 
120
                    missing.append(revid)
 
121
            return graph.iter_topo_order(missing)
 
122
        finally:
 
123
            pb.finished()
 
124
 
 
125
    def dfetch_refs(self, refs):
 
126
        old_refs = self.target._git.get_refs()
 
127
        new_refs = {}
 
128
        revidmap, gitidmap = self.dfetch(refs.values())
 
129
        for name, revid in refs.iteritems():
 
130
            if revid in gitidmap:
 
131
                gitid = gitidmap[revid]
 
132
            else:
 
133
                gitid = self.source_store._lookup_revision_sha1(revid)
 
134
            self.target._git.refs[name] = gitid
 
135
            new_refs[name] = gitid
 
136
        return revidmap, old_refs, new_refs
 
137
 
 
138
    def dfetch(self, stop_revisions):
 
139
        """Import the gist of the ancestry of a particular revision."""
 
140
        gitidmap = {}
 
141
        revidmap = {}
 
142
        self.source.lock_read()
 
143
        try:
 
144
            target_store = self.target._git.object_store
 
145
            def check_revid(revid):
 
146
                if revid == NULL_REVISION:
 
147
                    return True
 
148
                try:
 
149
                    return (self.source_store._lookup_revision_sha1(revid) in target_store)
 
150
                except errors.NoSuchRevision:
 
151
                    # Ghost, can't dpush
 
152
                    return True
 
153
            todo = list(self.missing_revisions(stop_revisions, check_revid))
 
154
            pb = ui.ui_factory.nested_progress_bar()
 
155
            try:
 
156
                object_generator = MissingObjectsIterator(self.source_store, self.source, pb)
 
157
                for old_bzr_revid, git_commit in object_generator.import_revisions(
 
158
                    todo):
 
159
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
 
160
                    revidmap[old_bzr_revid] = new_bzr_revid
 
161
                    gitidmap[old_bzr_revid] = git_commit
 
162
                target_store.add_objects(object_generator)
 
163
            finally:
 
164
                pb.finished()
 
165
        finally:
 
166
            self.source.unlock()
 
167
        return revidmap, gitidmap
 
168
 
 
169
    @staticmethod
 
170
    def is_compatible(source, target):
 
171
        """Be compatible with GitRepository."""
 
172
        return (not isinstance(source, GitRepository) and
 
173
                isinstance(target, LocalGitRepository))
 
174
 
 
175
 
 
176
class InterToRemoteGitRepository(InterToGitRepository):
 
177
 
 
178
    def dfetch_refs(self, new_refs):
 
179
        """Import the gist of the ancestry of a particular revision."""
 
180
        revidmap = {}
 
181
        old_refs = {}
 
182
        def determine_wants(refs):
 
183
            ret = {}
 
184
            old_refs.update(new_refs)
 
185
            for name, revid in new_refs.iteritems():
 
186
                ret[name] = self.source_store._lookup_revision_sha1(revid)
 
187
            return ret
 
188
        self.source.lock_read()
 
189
        try:
 
190
            new_refs = self.target.send_pack(determine_wants,
 
191
                    self.source_store.generate_pack_contents)
 
192
        finally:
 
193
            self.source.unlock()
 
194
        return revidmap, old_refs, new_refs
 
195
 
 
196
    @staticmethod
 
197
    def is_compatible(source, target):
 
198
        """Be compatible with GitRepository."""
 
199
        return (not isinstance(source, GitRepository) and
 
200
                isinstance(target, RemoteGitRepository))