/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
    )
25
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
26
from bzrlib.plugins.git.converter import (
27
    BazaarObjectStore,
28
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
29
from bzrlib.plugins.git.errors import (
30
    NoPushSupport,
31
    )
0.200.357 by Jelmer Vernooij
Move push code to push.py.
32
from bzrlib.plugins.git.mapping import (
33
    inventory_to_tree_and_blobs,
34
    revision_to_commit,
35
    )
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
36
from bzrlib.plugins.git.repository import (
37
    GitRepository,
38
    GitRepositoryFormat,
39
    )
40
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
41
42
class MissingObjectsIterator(object):
43
    """Iterate over git objects that are missing from a target repository.
44
45
    """
46
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
47
    def __init__(self, source, mapping, 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.
48
        """Create a new missing objects iterator.
49
50
        """
51
        self.source = source
52
        self._object_store = BazaarObjectStore(self.source, mapping)
53
        self._revids = set()
54
        self._sent_shas = set()
55
        self._pending = []
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
56
        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.
57
58
    def import_revisions(self, revids):
59
        self._revids.update(revids)
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
60
        for i, revid in enumerate(revids):
61
            if self.pb:
62
                self.pb.update("pushing revisions", i, len(revids))
63
            git_commit = self.import_revision(revid)
64
            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.
65
66
    def need_sha(self, sha):
67
        if sha in self._sent_shas:
68
            return False
69
        (type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
70
        assert type in ("blob", "tree")
71
        if revid in self._revids:
72
            # Not sent yet, and part of the set of revisions to send
73
            return True
74
        # Not changed in the revisions to send, so either not necessary
75
        # or already present remotely (as git doesn't do ghosts)
76
        return False
77
78
    def queue(self, sha, obj, path, ie=None, inv=None):
79
        if obj is None:
80
            obj = (ie, inv)
81
        self._pending.append((obj, path))
82
        self._sent_shas.add(sha)
83
84
    def import_revision(self, revid):
85
        """Import the gist of a revision into this Git repository.
86
87
        """
88
        inv = self.source.get_inventory(revid)
89
        todo = [inv.root]
90
        tree_sha = None
91
        while todo:
92
            ie = todo.pop()
93
            (sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
94
            if ie.parent_id is None:
95
                tree_sha = sha
96
            if not self.need_sha(sha):
97
                continue
98
            self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
99
            if ie.kind == "directory":
100
                todo.extend(ie.children.values())
101
        assert tree_sha is not None
102
        commit = self._object_store._get_commit(revid, tree_sha)
103
        self.queue(commit.id, commit, None)
104
        return commit.id
105
106
    def __len__(self):
107
        return len(self._pending)
108
109
    def __iter__(self):
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
110
        for i, (object, path) in enumerate(self._pending):
111
            if self.pb:
112
                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.
113
            if isinstance(object, tuple):
114
                object = self._object_store._get_ie_object(*object)
115
            yield (object, path)   
116
117
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
118
class InterToGitRepository(InterRepository):
119
    """InterRepository that copies into a Git repository."""
120
121
    _matching_repo_format = GitRepositoryFormat()
122
123
    @staticmethod
124
    def _get_repo_format_to_test():
125
        return None
126
127
    def copy_content(self, revision_id=None, pb=None):
128
        """See InterRepository.copy_content."""
129
        self.fetch(revision_id, pb, find_ghosts=False)
130
131
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
132
            fetch_spec=None):
133
        raise NoPushSupport()
134
0.200.360 by Jelmer Vernooij
Remove dpush ghost support - it makes no sense, git doesn't do ghosts.
135
    def missing_revisions(self, stop_revision=None):
0.200.357 by Jelmer Vernooij
Move push code to push.py.
136
        if stop_revision is not None:
137
            ancestry = [x for x in self.source.get_ancestry(stop_revision) if x is not None]
138
        else:
139
            ancestry = self.source.all_revision_ids()
140
        missing = []
141
        graph = self.source.get_graph()
142
        for revid in graph.iter_topo_order(ancestry):
143
            if not self.target.has_revision(revid):
144
                missing.append(revid)
145
        return missing
146
0.200.360 by Jelmer Vernooij
Remove dpush ghost support - it makes no sense, git doesn't do ghosts.
147
    def dfetch(self, stop_revision=None):
0.200.357 by Jelmer Vernooij
Move push code to push.py.
148
        """Import the gist of the ancestry of a particular revision."""
149
        revidmap = {}
150
        mapping = self.target.get_mapping()
0.200.367 by Jelmer Vernooij
In dfetch, skip fetching pushed revisions back, as cmd_dpush will already take care of that.
151
        self.source.lock_read()
0.200.357 by Jelmer Vernooij
Move push code to push.py.
152
        try:
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
153
            todo = self.missing_revisions(stop_revision)
0.200.369 by Jelmer Vernooij
Report on pack objects progress.
154
            pb = ui.ui_factory.nested_progress_bar()
155
            try:
156
                object_generator = MissingObjectsIterator(self.source, mapping, pb)
157
                for old_bzr_revid, git_commit in object_generator.import_revisions(
158
                    todo):
159
                    new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
160
                    revidmap[old_bzr_revid] = new_bzr_revid
161
                self.target._git.object_store.add_objects(object_generator) 
162
            finally:
163
                pb.finished()
0.200.357 by Jelmer Vernooij
Move push code to push.py.
164
        finally:
165
            self.source.unlock()
166
        return revidmap
167
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
168
    @staticmethod
169
    def is_compatible(source, target):
170
        """Be compatible with GitRepository."""
171
        return (not isinstance(source, GitRepository) and 
172
                isinstance(target, GitRepository))