/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 fetch.py

Fix branch cloning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from bzrlib import osutils, ui, urlutils
18
 
from bzrlib.errors import InvalidRevisionId, NoSuchRevision
 
18
from bzrlib.errors import InvalidRevisionId
19
19
from bzrlib.inventory import Inventory
20
20
from bzrlib.repository import InterRepository
21
21
from bzrlib.trace import info
22
22
from bzrlib.tsort import topo_sort
23
23
 
 
24
from bzrlib.plugins.git import git
24
25
from bzrlib.plugins.git.repository import (
25
26
        LocalGitRepository, 
26
27
        GitRepository, 
27
28
        GitFormat,
28
29
        )
29
 
from bzrlib.plugins.git.converter import GitObjectConverter
30
30
from bzrlib.plugins.git.remote import RemoteGitRepository
31
31
 
32
 
import dulwich as git
33
 
from dulwich.client import SimpleFetchGraphWalker
34
32
from dulwich.objects import Commit
35
33
 
36
34
from cStringIO import StringIO
37
35
 
38
36
 
39
37
class BzrFetchGraphWalker(object):
40
 
    """GraphWalker implementation that uses a Bazaar repository."""
41
38
 
42
39
    def __init__(self, repository, mapping):
43
40
        self.repository = repository
46
43
        self.heads = set(repository.all_revision_ids())
47
44
        self.parents = {}
48
45
 
49
 
    def __iter__(self):
50
 
        return iter(self.next, None)
51
 
 
52
46
    def ack(self, sha):
53
47
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
54
48
        self.remove(revid)
55
49
 
56
50
    def remove(self, revid):
57
51
        self.done.add(revid)
58
 
        if revid in self.heads:
 
52
        if ref in self.heads:
59
53
            self.heads.remove(revid)
60
54
        if revid in self.parents:
61
55
            for p in self.parents[revid]:
69
63
            self.heads.update([p for p in ps if not p in self.done])
70
64
            try:
71
65
                self.done.add(ret)
72
 
                return self.mapping.revision_id_bzr_to_foreign(ret)[0]
 
66
                return self.mapping.revision_id_bzr_to_foreign(ret)
73
67
            except InvalidRevisionId:
74
68
                pass
75
69
        return None
76
70
 
77
71
 
78
 
def import_git_blob(repo, mapping, path, blob, inv, parent_invs, gitmap, executable):
 
72
def import_git_blob(repo, mapping, path, blob, inv, parent_invs, executable):
79
73
    """Import a git blob object into a bzr repository.
80
74
 
81
75
    :param repo: bzr repository
92
86
    ie.text_size = len(blob.data)
93
87
    ie.text_sha1 = osutils.sha_string(blob.data)
94
88
    ie.executable = executable
95
 
    gitmap._idmap.add_entry(blob.sha().hexdigest(), "blob", (ie.file_id, ie.revision))
96
 
 
97
 
 
98
 
def import_git_tree(repo, mapping, path, tree, inv, parent_invs, 
99
 
                    gitmap, lookup_object):
 
89
 
 
90
 
 
91
def import_git_tree(repo, mapping, path, tree, inv, parent_invs, lookup_object):
100
92
    """Import a git tree object into a bzr repository.
101
93
 
102
94
    :param repo: A Bzr repository object
111
103
        [])
112
104
    ie = inv.add_path(path, "directory", file_id)
113
105
    ie.revision = text_revision
114
 
    gitmap._idmap.add_entry(tree.sha().hexdigest(), "tree", (file_id, text_revision))
115
106
    for mode, name, hexsha in tree.entries():
116
107
        entry_kind = (mode & 0700000) / 0100000
117
108
        basename = name.decode("utf-8")
121
112
            child_path = urlutils.join(path, name)
122
113
        if entry_kind == 0:
123
114
            tree = lookup_object(hexsha)
124
 
            import_git_tree(repo, mapping, child_path, tree, inv, parent_invs, gitmap, lookup_object)
 
115
            import_git_tree(repo, mapping, child_path, tree, inv, parent_invs, lookup_object)
125
116
        elif entry_kind == 1:
126
117
            blob = lookup_object(hexsha)
127
118
            fs_mode = mode & 0777
128
 
            import_git_blob(repo, mapping, child_path, blob, inv, parent_invs, gitmap, bool(fs_mode & 0111))
 
119
            import_git_blob(repo, mapping, child_path, blob, inv, parent_invs, bool(fs_mode & 0111))
129
120
        else:
130
121
            raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
131
122
 
132
123
 
133
 
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever, 
134
 
        pb=None):
 
124
def import_git_objects(repo, mapping, object_iter, pb=None):
135
125
    """Import a set of git objects into a bzr repository.
136
126
 
137
127
    :param repo: Bazaar repository
139
129
    :param object_iter: Iterator over Git objects.
140
130
    """
141
131
    # TODO: a more (memory-)efficient implementation of this
 
132
    objects = {}
 
133
    for i, o in enumerate(object_iter):
 
134
        if pb is not None:
 
135
            pb.update("fetching objects", i) 
 
136
        objects[o.id] = o
142
137
    graph = []
143
138
    root_trees = {}
144
139
    revisions = {}
145
140
    # Find and convert commit objects
146
 
    for o in object_iter.iterobjects():
 
141
    for o in objects.itervalues():
147
142
        if isinstance(o, Commit):
148
143
            rev = mapping.import_commit(o)
149
 
            root_trees[rev.revision_id] = object_iter[o.tree]
 
144
            root_trees[rev.revision_id] = objects[o.tree]
150
145
            revisions[rev.revision_id] = rev
151
146
            graph.append((rev.revision_id, rev.parent_ids))
152
 
            target_git_object_retriever._idmap.add_entry(o.sha().hexdigest(), "commit", (rev.revision_id, o._tree))
153
147
    # Order the revisions
154
148
    # Create the inventory objects
155
149
    for i, revid in enumerate(topo_sort(graph)):
163
157
        inv = Inventory()
164
158
        inv.revision_id = rev.revision_id
165
159
        def lookup_object(sha):
166
 
            if sha in object_iter:
167
 
                return object_iter[sha]
168
 
            return target_git_object_retriever[sha]
 
160
            if sha in objects:
 
161
                return objects[sha]
 
162
            return reconstruct_git_object(repo, mapping, sha)
169
163
        parent_invs = [repo.get_inventory(r) for r in rev.parent_ids]
170
 
        import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, 
171
 
            target_git_object_retriever, lookup_object)
 
164
        import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, lookup_object)
172
165
        repo.add_revision(rev.revision_id, rev, inv)
173
166
 
174
167
 
175
 
class InterGitNonGitRepository(InterRepository):
 
168
def reconstruct_git_commit(repo, rev):
 
169
    raise NotImplementedError(self.reconstruct_git_commit)
 
170
 
 
171
 
 
172
def reconstruct_git_object(repo, mapping, sha):
 
173
    # Commit
 
174
    revid = mapping.revision_id_foreign_to_bzr(sha)
 
175
    try:
 
176
        rev = repo.get_revision(revid)
 
177
    except NoSuchRevision:
 
178
        pass
 
179
    else:
 
180
        return reconstruct_git_commit(rev)
 
181
 
 
182
    # TODO: Tree
 
183
    # TODO: Blob
 
184
    raise KeyError("No such object %s" % sha)
 
185
 
 
186
 
 
187
class InterGitRepository(InterRepository):
176
188
 
177
189
    _matching_repo_format = GitFormat()
178
190
 
184
196
        """See InterRepository.copy_content."""
185
197
        self.fetch(revision_id, pb, find_ghosts=False)
186
198
 
187
 
    def fetch_objects(self, determine_wants, mapping, pb=None):
 
199
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
 
200
              mapping=None):
 
201
        if mapping is None:
 
202
            mapping = self.source.get_mapping()
188
203
        def progress(text):
189
 
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
 
204
            pb.note("git: %s", text)
 
205
        def determine_wants(heads):
 
206
            if revision_id is None:
 
207
                ret = heads.values()
 
208
            else:
 
209
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
 
210
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
190
211
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
191
212
        create_pb = None
192
213
        if pb is None:
193
214
            create_pb = pb = ui.ui_factory.nested_progress_bar()
194
 
        target_git_object_retriever = GitObjectConverter(self.target, mapping)
195
 
        
196
215
        try:
197
216
            self.target.lock_write()
198
217
            try:
199
218
                self.target.start_write_group()
200
219
                try:
201
 
                    objects_iter = self.source.fetch_objects(determine_wants, 
202
 
                                graph_walker, 
203
 
                                target_git_object_retriever.__getitem__, 
204
 
                                progress)
205
 
                    import_git_objects(self.target, mapping, objects_iter, 
206
 
                            target_git_object_retriever, pb)
 
220
                    import_git_objects(self.target, mapping,
 
221
                        iter(self.source.fetch_objects(determine_wants, graph_walker, 
 
222
                            progress)), pb)
207
223
                finally:
208
224
                    self.target.commit_write_group()
209
225
            finally:
212
228
            if create_pb:
213
229
                create_pb.finished()
214
230
 
215
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
216
 
              mapping=None):
217
 
        if mapping is None:
218
 
            mapping = self.source.get_mapping()
219
 
        def determine_wants(heads):
220
 
            if revision_id is None:
221
 
                ret = heads.values()
222
 
            else:
223
 
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
224
 
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
225
 
        return self.fetch_objects(determine_wants, mapping, pb)
226
 
 
227
231
    @staticmethod
228
232
    def is_compatible(source, target):
229
233
        """Be compatible with GitRepository."""
230
234
        # FIXME: Also check target uses VersionedFile
231
235
        return (isinstance(source, GitRepository) and 
232
 
                target.supports_rich_root() and
233
 
                not isinstance(target, GitRepository))
234
 
 
235
 
 
236
 
class InterGitRepository(InterRepository):
237
 
 
238
 
    _matching_repo_format = GitFormat()
239
 
 
240
 
    @staticmethod
241
 
    def _get_repo_format_to_test():
242
 
        return None
243
 
 
244
 
    def copy_content(self, revision_id=None, pb=None):
245
 
        """See InterRepository.copy_content."""
246
 
        self.fetch(revision_id, pb, find_ghosts=False)
247
 
 
248
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
249
 
              mapping=None):
250
 
        if mapping is None:
251
 
            mapping = self.source.get_mapping()
252
 
        def progress(text):
253
 
            info("git: %s", text)
254
 
        r = self.target._git
255
 
        if revision_id is None:
256
 
            determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
257
 
        else:
258
 
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
259
 
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
260
 
 
261
 
        graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
262
 
        f, commit = r.object_store.add_pack()
263
 
        try:
264
 
            self.source._git.fetch_pack(path, determine_wants, graphwalker, f.write, progress)
265
 
            f.close()
266
 
            commit()
267
 
        except:
268
 
            f.close()
269
 
            raise
270
 
 
271
 
    @staticmethod
272
 
    def is_compatible(source, target):
273
 
        """Be compatible with GitRepository."""
274
 
        return (isinstance(source, GitRepository) and 
275
 
                isinstance(target, GitRepository))
 
236
                target.supports_rich_root())