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

Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
16
17
 
17
18
"""An adapter between a Git Repository and a Bazaar Branch"""
18
19
 
19
 
import os
20
 
import time
21
 
 
22
20
import bzrlib
23
21
from bzrlib import (
24
 
    deprecated_graph,
25
22
    errors,
26
23
    graph,
27
24
    inventory,
29
26
    repository,
30
27
    revision,
31
28
    revisiontree,
 
29
    ui,
32
30
    urlutils,
33
 
    versionedfile,
34
31
    )
35
32
from bzrlib.foreign import (
36
33
        ForeignRepository,
41
38
from bzrlib.plugins.git.foreign import (
42
39
    versionedfiles,
43
40
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping
 
41
from bzrlib.plugins.git.mapping import (
 
42
    default_mapping,
 
43
    inventory_to_tree_and_blobs,
 
44
    mapping_registry,
 
45
    revision_to_commit,
 
46
    )
 
47
from bzrlib.plugins.git.versionedfiles import GitTexts
45
48
 
46
 
from bzrlib.plugins.git import git
 
49
import dulwich as git
 
50
import os
 
51
import time
47
52
 
48
53
 
49
54
class GitTags(object):
61
66
    _serializer = None
62
67
 
63
68
    def __init__(self, gitdir, lockfiles):
64
 
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
 
69
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
70
            lockfiles)
65
71
        from bzrlib.plugins.git import fetch
66
 
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
 
72
        for optimiser in [fetch.InterGitRepository, 
 
73
                          fetch.InterGitNonGitRepository]:
 
74
            repository.InterRepository.register_optimiser(optimiser)
67
75
 
68
76
    def is_shared(self):
69
77
        return True
83
91
 
84
92
 
85
93
class LocalGitRepository(GitRepository):
 
94
    """Git repository on the file system."""
86
95
 
87
96
    def __init__(self, gitdir, lockfiles):
88
97
        # FIXME: This also caches negatives. Need to be more careful 
94
103
        self.texts = None
95
104
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
96
105
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
 
106
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
107
        self.texts = GitTexts(self)
97
108
        self.tags = GitTags(self._git.get_tags())
98
109
 
99
110
    def all_revision_ids(self):
100
111
        ret = set([revision.NULL_REVISION])
101
 
        if self._git.heads() == []:
 
112
        heads = self._git.heads()
 
113
        if heads == {}:
102
114
            return ret
103
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
 
115
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
104
116
        ret = set(bzr_heads)
105
117
        graph = self.get_graph()
106
118
        for rev, parents in graph.iter_ancestry(bzr_heads):
124
136
            if revision_id == revision.NULL_REVISION:
125
137
                parent_map[revision_id] = ()
126
138
                continue
127
 
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
 
139
            hexsha, mapping = self.lookup_git_revid(revision_id)
128
140
            commit  = self._git.commit(hexsha)
129
141
            if commit is None:
130
142
                continue
131
143
            else:
132
 
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
 
144
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
133
145
        return parent_map
134
146
 
135
147
    def get_ancestry(self, revision_id, topo_sorted=True):
136
148
        """See Repository.get_ancestry().
137
149
        """
138
150
        if revision_id is None:
139
 
            return self._all_revision_ids()
 
151
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
140
152
        assert isinstance(revision_id, str)
141
153
        ancestry = []
142
154
        graph = self.get_graph()
143
155
        for rev, parents in graph.iter_ancestry([revision_id]):
144
 
            if rev == revision.NULL_REVISION:
145
 
                rev = None
146
156
            ancestry.append(rev)
147
157
        ancestry.reverse()
148
 
        return ancestry
 
158
        return [None] + ancestry
 
159
 
 
160
    def import_revision_gist(self, source, revid, parent_lookup):
 
161
        """Import the gist of a revision into this Git repository.
 
162
 
 
163
        """
 
164
        objects = []
 
165
        rev = source.get_revision(revid)
 
166
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
 
167
            if path == "":
 
168
                tree_sha = sha
 
169
            objects.append((object, path))
 
170
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
 
171
        objects.append((commit, None))
 
172
        self._git.object_store.add_objects(objects)
 
173
        return commit.sha().hexdigest()
 
174
 
 
175
    def dfetch(self, source, stop_revision):
 
176
        """Import the gist of the ancestry of a particular revision."""
 
177
        if stop_revision is None:
 
178
            raise NotImplementedError
 
179
        revidmap = {}
 
180
        gitidmap = {}
 
181
        def parent_lookup(revid):
 
182
            try:
 
183
                return gitidmap[revid]
 
184
            except KeyError:
 
185
                return self.lookup_git_revid(revid)[0]
 
186
        todo = []
 
187
        source.lock_write()
 
188
        try:
 
189
            graph = source.get_graph()
 
190
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
 
191
            for revid in graph.iter_topo_order(ancestry):
 
192
                if not self.has_revision(revid):
 
193
                    todo.append(revid)
 
194
            pb = ui.ui_factory.nested_progress_bar()
 
195
            try:
 
196
                for i, revid in enumerate(todo):
 
197
                    pb.update("pushing revisions", i, len(todo))
 
198
                    git_commit = self.import_revision_gist(source, revid,
 
199
                        parent_lookup)
 
200
                    gitidmap[revid] = git_commit
 
201
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(
 
202
                        git_commit)
 
203
                    revidmap[revid] = git_revid
 
204
            finally:
 
205
                pb.finished()
 
206
            source.fetch(self, revision_id=revidmap[stop_revision])
 
207
        finally:
 
208
            source.unlock()
 
209
        return revidmap
149
210
 
150
211
    def get_signature_text(self, revision_id):
151
212
        raise errors.NoSuchRevision(self, revision_id)
162
223
    def has_signature_for_revision_id(self, revision_id):
163
224
        return False
164
225
 
165
 
    def lookup_git_revid(self, bzr_revid, mapping):
 
226
    def lookup_git_revid(self, bzr_revid):
166
227
        try:
167
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
 
228
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
168
229
        except errors.InvalidRevisionId:
169
230
            raise errors.NoSuchRevision(self, bzr_revid)
170
231
 
171
232
    def get_revision(self, revision_id):
172
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
233
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
173
234
        try:
174
235
            commit = self._git.commit(git_commit_id)
175
236
        except KeyError:
176
237
            raise errors.NoSuchRevision(self, revision_id)
177
238
        # print "fetched revision:", git_commit_id
178
 
        revision = self.get_mapping().import_commit(commit)
 
239
        revision = mapping.import_commit(commit)
179
240
        assert revision is not None
180
241
        return revision
181
242
 
196
257
 
197
258
    def revision_tree(self, revision_id):
198
259
        revision_id = revision.ensure_null(revision_id)
199
 
 
200
260
        if revision_id == revision.NULL_REVISION:
201
261
            inv = inventory.Inventory(root_id=None)
202
262
            inv.revision_id = revision_id
203
263
            return revisiontree.RevisionTree(self, inv, revision_id)
204
 
 
205
 
        return GitRevisionTree(self, self.get_mapping(), revision_id)
 
264
        return GitRevisionTree(self, revision_id)
206
265
 
207
266
    def get_inventory(self, revision_id):
208
267
        assert revision_id != None
211
270
    def set_make_working_trees(self, trees):
212
271
        pass
213
272
 
214
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
273
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
274
        progress=None):
215
275
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
216
276
 
217
277
 
218
278
class GitRevisionTree(revisiontree.RevisionTree):
219
279
 
220
 
    def __init__(self, repository, mapping, revision_id):
 
280
    def __init__(self, repository, revision_id):
221
281
        self._repository = repository
222
282
        self.revision_id = revision_id
223
283
        assert isinstance(revision_id, str)
224
 
        self.mapping = mapping
225
 
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
 
284
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
285
        try:
227
286
            commit = repository._git.commit(git_id)
228
287
        except KeyError, r:
278
337
                self._build_inventory(hexsha, child_ie, child_path)
279
338
 
280
339
 
281
 
class GitFormat(object):
 
340
class GitRepositoryFormat(repository.RepositoryFormat):
282
341
 
283
342
    supports_tree_reference = False
284
343
    rich_root_data = True