/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
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
0.200.357 by Jelmer Vernooij
Move push code to push.py.
19
from bzrlib import (
20
    ui,
21
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
22
from bzrlib.repository import (
23
    InterRepository,
24
    )
0.200.371 by Jelmer Vernooij
Add progress bar when determining revisions to dpush
25
from bzrlib.revision import (
26
    NULL_REVISION,
27
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
28
29
from bzrlib.plugins.git.errors import (
30
    NoPushSupport,
31
    )
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
32
from bzrlib.plugins.git.object_store import (
33
    BazaarObjectStore,
34
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
35
from bzrlib.plugins.git.repository import (
36
    GitRepository,
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
37
    LocalGitRepository,
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
38
    GitRepositoryFormat,
39
    )
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
40
from bzrlib.plugins.git.remote import (
41
    RemoteGitRepository,
42
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
43
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
44
45
class MissingObjectsIterator(object):
46
    """Iterate over git objects that are missing from a target repository.
47
48
    """
49
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
50
    def __init__(self, store, source, pb=None):
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
51
        """Create a new missing objects iterator.
52
53
        """
54
        self.source = source
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
55
        self._object_store = store
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
56
        self._revids = set()
57
        self._sent_shas = set()
58
        self._pending = []
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
59
        self.pb = pb
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
60
61
    def import_revisions(self, revids):
62
        self._revids.update(revids)
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
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)
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
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):
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
113
        for i, (object, path) in enumerate(self._pending):
114
            if self.pb:
115
                self.pb.update("writing pack objects", i, len(self))
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
116
            if isinstance(object, tuple):
117
                object = self._object_store._get_ie_object(*object)
118
            yield (object, path)   
119
120
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
121
class InterToGitRepository(InterRepository):
122
    """InterRepository that copies into a Git repository."""
123
124
    _matching_repo_format = GitRepositoryFormat()
125
0.200.435 by Jelmer Vernooij
Remember mapping per InterRepository.
126
    def __init__(self, source, target):
127
        super(InterToGitRepository, self).__init__(source, target)
128
        self.mapping = self.target.get_mapping()
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
129
        self.source_store = BazaarObjectStore(self.source, self.mapping)
0.200.435 by Jelmer Vernooij
Remember mapping per InterRepository.
130
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
131
    @staticmethod
132
    def _get_repo_format_to_test():
133
        return None
134
135
    def copy_content(self, revision_id=None, pb=None):
136
        """See InterRepository.copy_content."""
137
        self.fetch(revision_id, pb, find_ghosts=False)
138
139
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
140
            fetch_spec=None):
141
        raise NoPushSupport()
142
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
143
144
class InterToLocalGitRepository(InterToGitRepository):
145
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
146
    def missing_revisions(self, stop_revisions, check_revid):
0.200.357 by Jelmer Vernooij
Move push code to push.py.
147
        missing = []
0.200.371 by Jelmer Vernooij
Add progress bar when determining revisions to dpush
148
        pb = ui.ui_factory.nested_progress_bar()
149
        try:
150
            graph = self.source.get_graph()
0.200.524 by Jelmer Vernooij
Simplify dpushing multiple heads.
151
            for revid, _ in graph.iter_ancestry(stop_revisions):
0.200.371 by Jelmer Vernooij
Add progress bar when determining revisions to dpush
152
                pb.update("determining revisions to fetch", len(missing))
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
153
                if not check_revid(revid):
0.200.371 by Jelmer Vernooij
Add progress bar when determining revisions to dpush
154
                    missing.append(revid)
155
            return graph.iter_topo_order(missing)
156
        finally:
157
            pb.finished()
0.200.357 by Jelmer Vernooij
Move push code to push.py.
158
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
159
    def dfetch_refs(self, refs):
0.200.438 by Jelmer Vernooij
Somewhat fix dpushing to remote repos.
160
        new_refs = {}
0.200.524 by Jelmer Vernooij
Simplify dpushing multiple heads.
161
        revidmap, gitidmap = self.dfetch(refs.values())
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
162
        for name, revid in refs.iteritems():
0.200.524 by Jelmer Vernooij
Simplify dpushing multiple heads.
163
            if revid in gitidmap:
164
                gitid = gitidmap[revid]
0.200.435 by Jelmer Vernooij
Remember mapping per InterRepository.
165
            else:
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
166
                gitid = self.source_store._lookup_revision_sha1(revid)
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
167
            self.target._git.refs[name] = gitid
0.200.438 by Jelmer Vernooij
Somewhat fix dpushing to remote repos.
168
            new_refs[name] = gitid
169
        return revidmap, new_refs
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
170
0.200.524 by Jelmer Vernooij
Simplify dpushing multiple heads.
171
    def dfetch(self, stop_revisions):
0.200.357 by Jelmer Vernooij
Move push code to push.py.
172
        """Import the gist of the ancestry of a particular revision."""
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
173
        gitidmap = {}
0.200.357 by Jelmer Vernooij
Move push code to push.py.
174
        revidmap = {}
0.200.367 by Jelmer Vernooij
In dfetch, skip fetching pushed revisions back, as cmd_dpush will already take care of that.
175
        self.source.lock_read()
0.200.357 by Jelmer Vernooij
Move push code to push.py.
176
        try:
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
177
            target_store = self.target._git.object_store
178
            def check_revid(revid):
179
                if revid == NULL_REVISION:
180
                    return True
181
                return (self.source_store._lookup_revision_sha1(revid) in target_store)
182
            todo = list(self.missing_revisions(stop_revisions, check_revid))
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
183
            pb = ui.ui_factory.nested_progress_bar()
184
            try:
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
185
                object_generator = MissingObjectsIterator(self.source_store, self.source, pb)
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
186
                for old_bzr_revid, git_commit in object_generator.import_revisions(
187
                    todo):
0.200.435 by Jelmer Vernooij
Remember mapping per InterRepository.
188
                    new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
189
                    revidmap[old_bzr_revid] = new_bzr_revid
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
190
                    gitidmap[old_bzr_revid] = git_commit
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
191
                target_store.add_objects(object_generator) 
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
192
            finally:
193
                pb.finished()
0.200.357 by Jelmer Vernooij
Move push code to push.py.
194
        finally:
195
            self.source.unlock()
0.200.428 by Jelmer Vernooij
use dfetch_refs, to prepare for dpush to remote repositories.
196
        return revidmap, gitidmap
0.200.357 by Jelmer Vernooij
Move push code to push.py.
197
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
198
    @staticmethod
199
    def is_compatible(source, target):
200
        """Be compatible with GitRepository."""
201
        return (not isinstance(source, GitRepository) and 
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
202
                isinstance(target, LocalGitRepository))
203
204
205
class InterToRemoteGitRepository(InterToGitRepository):
206
0.200.429 by Jelmer Vernooij
get remote dpush to a point where we now what to send.
207
    def dfetch_refs(self, new_refs):
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
208
        """Import the gist of the ancestry of a particular revision."""
209
        revidmap = {}
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
210
        def determine_wants(refs):
0.200.429 by Jelmer Vernooij
get remote dpush to a point where we now what to send.
211
            ret = {}
212
            for name, revid in new_refs.iteritems():
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
213
                ret[name] = self.source_store._lookup_revision_sha1(revid)
0.200.429 by Jelmer Vernooij
get remote dpush to a point where we now what to send.
214
            return ret
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
215
        self.source.lock_read()
216
        try:
0.200.460 by Jelmer Vernooij
Somewhat fix commit in git working trees.
217
            new_refs = self.target.send_pack(determine_wants,
0.200.525 by Jelmer Vernooij
Simplify push a bit further, make dpush without rebase faster.
218
                    self.source_store.generate_pack_contents)
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
219
        finally:
220
            self.source.unlock()
0.200.438 by Jelmer Vernooij
Somewhat fix dpushing to remote repos.
221
        return revidmap, new_refs
0.200.425 by Jelmer Vernooij
Split out push to remote git repositories.
222
223
    @staticmethod
224
    def is_compatible(source, target):
225
        """Be compatible with GitRepository."""
226
        return (not isinstance(source, GitRepository) and 
227
                isinstance(target, RemoteGitRepository))