/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

Share sha map cache connections inside threads.

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
 
 
26
from bzrlib.plugins.git.converter import (
 
27
    BazaarObjectStore,
 
28
    )
 
29
from bzrlib.plugins.git.errors import (
 
30
    NoPushSupport,
 
31
    )
 
32
from bzrlib.plugins.git.mapping import (
 
33
    inventory_to_tree_and_blobs,
 
34
    revision_to_commit,
 
35
    )
 
36
from bzrlib.plugins.git.repository import (
 
37
    GitRepository,
 
38
    GitRepositoryFormat,
 
39
    )
 
40
 
 
41
 
 
42
class MissingObjectsIterator(object):
 
43
    """Iterate over git objects that are missing from a target repository.
 
44
 
 
45
    """
 
46
 
 
47
    def __init__(self, source, mapping):
 
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 = []
 
56
 
 
57
    def import_revisions(self, revids):
 
58
        self._revids.update(revids)
 
59
        pb = ui.ui_factory.nested_progress_bar()
 
60
        try:
 
61
            for i, revid in enumerate(revids):
 
62
                pb.update("pushing revisions", i, len(revids))
 
63
                git_commit = self.import_revision(revid)
 
64
                yield (revid, git_commit)
 
65
        finally:
 
66
            pb.finished()
 
67
 
 
68
    def need_sha(self, sha):
 
69
        if sha in self._sent_shas:
 
70
            return False
 
71
        (type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
 
72
        assert type in ("blob", "tree")
 
73
        if revid in self._revids:
 
74
            # Not sent yet, and part of the set of revisions to send
 
75
            return True
 
76
        # Not changed in the revisions to send, so either not necessary
 
77
        # or already present remotely (as git doesn't do ghosts)
 
78
        return False
 
79
 
 
80
    def queue(self, sha, obj, path, ie=None, inv=None):
 
81
        if obj is None:
 
82
            obj = (ie, inv)
 
83
        self._pending.append((obj, path))
 
84
        self._sent_shas.add(sha)
 
85
 
 
86
    def import_revision(self, revid):
 
87
        """Import the gist of a revision into this Git repository.
 
88
 
 
89
        """
 
90
        inv = self.source.get_inventory(revid)
 
91
        todo = [inv.root]
 
92
        tree_sha = None
 
93
        while todo:
 
94
            ie = todo.pop()
 
95
            (sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
 
96
            if ie.parent_id is None:
 
97
                tree_sha = sha
 
98
            if not self.need_sha(sha):
 
99
                continue
 
100
            self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
 
101
            if ie.kind == "directory":
 
102
                todo.extend(ie.children.values())
 
103
        assert tree_sha is not None
 
104
        commit = self._object_store._get_commit(revid, tree_sha)
 
105
        self.queue(commit.id, commit, None)
 
106
        return commit.id
 
107
 
 
108
    def __len__(self):
 
109
        return len(self._pending)
 
110
 
 
111
    def __iter__(self):
 
112
        for (object, path) in self._pending:
 
113
            if isinstance(object, tuple):
 
114
                object = self._object_store._get_ie_object(*object)
 
115
            yield (object, path)   
 
116
 
 
117
 
 
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
 
 
135
    def missing_revisions(self, stop_revision=None):
 
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
 
 
147
    def dfetch(self, stop_revision=None):
 
148
        """Import the gist of the ancestry of a particular revision."""
 
149
        revidmap = {}
 
150
        mapping = self.target.get_mapping()
 
151
        self.source.lock_write()
 
152
        try:
 
153
            todo = self.missing_revisions(stop_revision)
 
154
            object_generator = MissingObjectsIterator(self.source, mapping)
 
155
            for old_bzr_revid, git_commit in object_generator.import_revisions(
 
156
                todo):
 
157
                new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
 
158
                revidmap[old_bzr_revid] = new_bzr_revid
 
159
            self.target._git.object_store.add_objects(object_generator) 
 
160
            if revidmap != {}:
 
161
                self.source.fetch(self.target, 
 
162
                        revision_id=revidmap[stop_revision])
 
163
        finally:
 
164
            self.source.unlock()
 
165
        return revidmap
 
166
 
 
167
    @staticmethod
 
168
    def is_compatible(source, target):
 
169
        """Be compatible with GitRepository."""
 
170
        return (not isinstance(source, GitRepository) and 
 
171
                isinstance(target, GitRepository))