/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 regression in git-import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""An adapter between a Git Repository and a Bazaar Branch"""
19
19
 
20
 
import bzrlib
21
20
from bzrlib import (
22
21
    errors,
23
 
    graph,
24
22
    inventory,
25
 
    osutils,
26
23
    repository,
27
24
    revision,
28
25
    revisiontree,
29
 
    ui,
30
 
    urlutils,
31
26
    )
32
27
from bzrlib.foreign import (
33
28
    ForeignRepository,
34
29
    )
35
 
from bzrlib.trace import (
36
 
    mutter,
37
 
    )
38
 
from bzrlib.transport import (
39
 
    get_transport,
40
 
    )
41
30
 
42
31
from bzrlib.plugins.git.commit import (
43
32
    GitCommitBuilder,
44
33
    )
45
 
from bzrlib.plugins.git.foreign import (
46
 
    versionedfiles,
47
 
    )
48
 
from bzrlib.plugins.git.inventory import (
49
 
    GitInventory,
50
 
    )
51
34
from bzrlib.plugins.git.mapping import (
52
35
    default_mapping,
53
36
    foreign_git,
54
37
    mapping_registry,
55
38
    )
 
39
from bzrlib.plugins.git.tree import (
 
40
    GitRevisionTree,
 
41
    )
56
42
from bzrlib.plugins.git.versionedfiles import (
 
43
    GitRevisions,
57
44
    GitTexts,
58
45
    )
59
46
 
60
47
 
 
48
from dulwich.objects import (
 
49
    Commit,
 
50
    )
 
51
 
 
52
 
61
53
class GitRepository(ForeignRepository):
62
54
    """An adapter to git repositories for bzr."""
63
55
 
66
58
    vcs = foreign_git
67
59
 
68
60
    def __init__(self, gitdir, lockfiles):
69
 
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
61
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
70
62
            lockfiles)
71
63
        from bzrlib.plugins.git import fetch, push
72
 
        for optimiser in [fetch.InterRemoteGitNonGitRepository, 
 
64
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
73
65
                          fetch.InterLocalGitNonGitRepository,
74
66
                          fetch.InterGitGitRepository,
75
67
                          push.InterToLocalGitRepository,
77
69
            repository.InterRepository.register_optimiser(optimiser)
78
70
 
79
71
    def is_shared(self):
80
 
        return True
 
72
        return False
81
73
 
82
74
    def supports_rich_root(self):
83
75
        return True
84
76
 
85
 
    def _warn_if_deprecated(self):
 
77
    def _warn_if_deprecated(self, branch=None):
86
78
        # This class isn't deprecated
87
79
        pass
88
80
 
92
84
    def make_working_trees(self):
93
85
        return True
94
86
 
 
87
    def revision_graph_can_have_wrong_parents(self):
 
88
        return False
 
89
 
95
90
    def dfetch(self, source, stop_revision):
96
91
        interrepo = repository.InterRepository.get(source, self)
97
92
        return interrepo.dfetch(stop_revision)
98
93
 
99
 
    def dfetch_refs(self, source, stop_revision):
100
 
        interrepo = repository.InterRepository.get(source, self)
101
 
        return interrepo.dfetch_refs(stop_revision)
102
 
 
103
94
 
104
95
class LocalGitRepository(GitRepository):
105
96
    """Git repository on the file system."""
106
97
 
107
98
    def __init__(self, gitdir, lockfiles):
108
 
        # FIXME: This also caches negatives. Need to be more careful 
109
 
        # about this once we start writing to git
110
 
        self._parents_provider = graph.CachingParentsProvider(self)
111
99
        GitRepository.__init__(self, gitdir, lockfiles)
112
100
        self.base = gitdir.root_transport.base
113
101
        self._git = gitdir._git
114
 
        self.texts = None
115
 
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
116
 
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
117
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
102
        self.signatures = None
 
103
        self.revisions = GitRevisions(self, self._git.object_store)
 
104
        self.inventories = None
118
105
        self.texts = GitTexts(self)
119
106
 
 
107
    def _iter_revision_ids(self):
 
108
        for sha in self._git.object_store:
 
109
            o = self._git.object_store[sha]
 
110
            if not isinstance(o, Commit):
 
111
                continue
 
112
            rev = self.get_mapping().import_commit(o,
 
113
                self.lookup_foreign_revision_id)
 
114
            yield o.id, rev.revision_id
 
115
 
120
116
    def all_revision_ids(self):
121
 
        ret = set([revision.NULL_REVISION])
122
 
        heads = self._git.refs.as_dict('refs/heads')
123
 
        if heads == {}:
124
 
            return ret
125
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
126
 
        ret = set(bzr_heads)
127
 
        graph = self.get_graph()
128
 
        for rev, parents in graph.iter_ancestry(bzr_heads):
129
 
            ret.add(rev)
 
117
        ret = set([])
 
118
        for git_sha, revid in self._iter_revision_ids():
 
119
            ret.add(revid)
130
120
        return ret
131
121
 
132
 
    def _make_parents_provider(self):
133
 
        """See Repository._make_parents_provider()."""
134
 
        return self._parents_provider
135
 
 
136
122
    def get_parent_map(self, revids):
137
123
        parent_map = {}
138
124
        for revision_id in revids:
140
126
            if revision_id == revision.NULL_REVISION:
141
127
                parent_map[revision_id] = ()
142
128
                continue
143
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
144
 
            commit  = self._git.commit(hexsha)
145
 
            if commit is None:
 
129
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
130
            try:
 
131
                commit = self._git[hexsha]
 
132
            except KeyError:
146
133
                continue
147
 
            else:
148
 
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
 
134
            parent_map[revision_id] = [self.lookup_foreign_revision_id(p, mapping) for p in commit.parents]
149
135
        return parent_map
150
136
 
151
137
    def get_ancestry(self, revision_id, topo_sorted=True):
164
150
    def get_signature_text(self, revision_id):
165
151
        raise errors.NoSuchRevision(self, revision_id)
166
152
 
167
 
    def lookup_revision_id(self, revid):
 
153
    def pack(self, hint=None, clean_obsolete_packs=False):
 
154
        self._git.object_store.pack_loose_objects()
 
155
 
 
156
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
168
157
        """Lookup a revision id.
169
 
        
170
 
        :param revid: Bazaar revision id.
171
 
        :return: Tuple with git revisionid and mapping.
 
158
 
172
159
        """
173
 
        # Yes, this doesn't really work, but good enough as a stub
174
 
        return osutils.sha(rev_id).hexdigest(), self.get_mapping()
 
160
        if mapping is None:
 
161
            mapping = self.get_mapping()
 
162
        from dulwich.protocol import (
 
163
            ZERO_SHA,
 
164
            )
 
165
        if foreign_revid == ZERO_SHA:
 
166
            return revision.NULL_REVISION
 
167
        commit = self._git[foreign_revid]
 
168
        rev = mapping.import_commit(commit, lambda x: None)
 
169
        return rev.revision_id
175
170
 
176
171
    def has_signature_for_revision_id(self, revision_id):
177
172
        return False
178
173
 
179
 
    def lookup_git_revid(self, bzr_revid):
 
174
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
180
175
        try:
181
176
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
182
177
        except errors.InvalidRevisionId:
183
 
            raise errors.NoSuchRevision(self, bzr_revid)
 
178
            if mapping is None:
 
179
                mapping = self.get_mapping()
 
180
            try:
 
181
                return self._git.refs[mapping.revid_as_refname(bzr_revid)], mapping
 
182
            except KeyError:
 
183
                # Update refs from Git commit objects
 
184
                # FIXME: Hitting this a lot will be very inefficient...
 
185
                for git_sha, revid in self._iter_revision_ids():
 
186
                    self._git.refs[mapping.revid_as_refname(revid)] = git_sha
 
187
                    if revid == bzr_revid:
 
188
                        return git_sha, mapping
 
189
                raise errors.NoSuchRevision(self, bzr_revid)
184
190
 
185
191
    def get_revision(self, revision_id):
186
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
192
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
187
193
        try:
188
 
            commit = self._git.commit(git_commit_id)
 
194
            commit = self._git[git_commit_id]
189
195
        except KeyError:
190
196
            raise errors.NoSuchRevision(self, revision_id)
191
197
        # print "fetched revision:", git_commit_id
192
 
        revision = mapping.import_commit(commit)
 
198
        revision = mapping.import_commit(commit,
 
199
            self.lookup_foreign_revision_id)
193
200
        assert revision is not None
194
201
        return revision
195
202
 
196
203
    def has_revision(self, revision_id):
197
204
        try:
198
 
            self.get_revision(revision_id)
 
205
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
199
206
        except errors.NoSuchRevision:
200
207
            return False
201
 
        else:
202
 
            return True
 
208
        return (git_commit_id in self._git)
 
209
 
 
210
    def has_revisions(self, revision_ids):
 
211
        return set(filter(self.has_revision, revision_ids))
203
212
 
204
213
    def get_revisions(self, revids):
205
214
        return [self.get_revision(r) for r in revids]
227
236
        progress=None):
228
237
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
229
238
 
230
 
 
231
 
class GitRevisionTree(revisiontree.RevisionTree):
232
 
 
233
 
    def __init__(self, repository, revision_id):
234
 
        self._repository = repository
235
 
        self._revision_id = revision_id
236
 
        assert isinstance(revision_id, str)
237
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
238
 
        try:
239
 
            commit = repository._git.commit(git_id)
240
 
        except KeyError, r:
241
 
            raise errors.NoSuchRevision(repository, revision_id)
242
 
        self.tree = commit.tree
243
 
        self._inventory = GitInventory(self.tree, self.mapping, repository._git.object_store, revision_id)
244
 
 
245
 
    def get_revision_id(self):
246
 
        return self._revision_id
247
 
 
248
 
    def get_file_text(self, file_id):
249
 
        entry = self._inventory[file_id]
250
 
        if entry.kind == 'directory': return ""
251
 
        return entry.object.data
 
239
    def _get_versioned_file_checker(self, text_key_references=None,
 
240
                        ancestors=None):
 
241
        return GitVersionedFileChecker(self,
 
242
            text_key_references=text_key_references, ancestors=ancestors)
 
243
    
 
244
 
 
245
class GitVersionedFileChecker(repository._VersionedFileChecker):
 
246
 
 
247
    file_ids = []
 
248
 
 
249
    def _check_file_version_parents(self, texts, progress_bar):
 
250
        return {}, []
252
251
 
253
252
 
254
253
class GitRepositoryFormat(repository.RepositoryFormat):
261
260
        return "Git Repository"
262
261
 
263
262
    def initialize(self, url, shared=False, _internal=False):
264
 
        raise bzr_errors.UninitializableFormat(self)
 
263
        raise errors.UninitializableFormat(self)
265
264
 
266
265
    def check_conversion_target(self, target_repo_format):
267
266
        return target_repo_format.rich_root_data
 
267
 
 
268
    def get_foreign_tests_repository_factory(self):
 
269
        from bzrlib.plugins.git.tests.test_repository import (
 
270
            ForeignTestsRepositoryFactory,
 
271
            )
 
272
        return ForeignTestsRepositoryFactory()
 
273
 
 
274
    def network_name(self):
 
275
        return "git"