/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

Merge support for executable symlinks.

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
20
21
from bzrlib import (
21
22
    errors,
 
23
    graph,
22
24
    inventory,
23
25
    repository,
24
26
    revision,
45
47
    )
46
48
 
47
49
 
48
 
from dulwich.objects import (
49
 
    Commit,
50
 
    )
51
 
 
52
 
 
53
50
class GitRepository(ForeignRepository):
54
51
    """An adapter to git repositories for bzr."""
55
52
 
69
66
            repository.InterRepository.register_optimiser(optimiser)
70
67
 
71
68
    def is_shared(self):
72
 
        return False
 
69
        return True
73
70
 
74
71
    def supports_rich_root(self):
75
72
        return True
76
73
 
77
 
    def _warn_if_deprecated(self, branch=None):
 
74
    def _warn_if_deprecated(self):
78
75
        # This class isn't deprecated
79
76
        pass
80
77
 
82
79
        return default_mapping
83
80
 
84
81
    def make_working_trees(self):
85
 
        return not self._git.bare
 
82
        return True
86
83
 
87
84
    def revision_graph_can_have_wrong_parents(self):
88
85
        return False
91
88
        interrepo = repository.InterRepository.get(source, self)
92
89
        return interrepo.dfetch(stop_revision)
93
90
 
 
91
    def dfetch_refs(self, source, stop_revision):
 
92
        interrepo = repository.InterRepository.get(source, self)
 
93
        return interrepo.dfetch_refs(stop_revision)
 
94
 
94
95
 
95
96
class LocalGitRepository(GitRepository):
96
97
    """Git repository on the file system."""
97
98
 
98
99
    def __init__(self, gitdir, lockfiles):
 
100
        # FIXME: This also caches negatives. Need to be more careful
 
101
        # about this once we start writing to git
 
102
        self._parents_provider = graph.CachingParentsProvider(self)
99
103
        GitRepository.__init__(self, gitdir, lockfiles)
100
104
        self.base = gitdir.root_transport.base
101
105
        self._git = gitdir._git
104
108
        self.inventories = None
105
109
        self.texts = GitTexts(self)
106
110
 
107
 
    def _iter_revision_ids(self):
108
 
        mapping = self.get_mapping()
109
 
        for sha in self._git.object_store:
110
 
            o = self._git.object_store[sha]
111
 
            if not isinstance(o, Commit):
112
 
                continue
113
 
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
 
                self.lookup_foreign_revision_id)
115
 
            yield o.id, rev.revision_id, roundtrip_revid
116
 
 
117
111
    def all_revision_ids(self):
118
112
        ret = set([])
119
 
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
120
 
            ret.add(revid)
121
 
            if roundtrip_revid:
122
 
                ret.add(roundtrip_revid)
 
113
        heads = self._git.refs.as_dict('refs/heads')
 
114
        if heads == {}:
 
115
            return ret
 
116
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
 
117
        ret = set(bzr_heads)
 
118
        graph = self.get_graph()
 
119
        for rev, parents in graph.iter_ancestry(bzr_heads):
 
120
            ret.add(rev)
123
121
        return ret
124
122
 
 
123
    def _make_parents_provider(self):
 
124
        """See Repository._make_parents_provider()."""
 
125
        return self._parents_provider
 
126
 
125
127
    def get_parent_map(self, revids):
126
128
        parent_map = {}
127
129
        for revision_id in revids:
131
133
                continue
132
134
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
133
135
            try:
134
 
                commit = self._git[hexsha]
 
136
                commit = self._git.commit(hexsha)
135
137
            except KeyError:
136
138
                continue
137
 
            parent_map[revision_id] = [
138
 
                self.lookup_foreign_revision_id(p, mapping)
139
 
                for p in commit.parents]
 
139
            if commit is None:
 
140
                # Older versions of Dulwich used to return None rather than
 
141
                # raise KeyError.
 
142
                continue
 
143
            else:
 
144
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
140
145
        return parent_map
141
146
 
142
147
    def get_ancestry(self, revision_id, topo_sorted=True):
155
160
    def get_signature_text(self, revision_id):
156
161
        raise errors.NoSuchRevision(self, revision_id)
157
162
 
158
 
    def pack(self, hint=None, clean_obsolete_packs=False):
159
 
        self._git.object_store.pack_loose_objects()
160
 
 
161
163
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
164
        """Lookup a revision id.
163
165
 
 
166
        :param revid: Bazaar revision id.
 
167
        :return: Tuple with git revisionid and mapping.
164
168
        """
165
 
        assert type(foreign_revid) is str
166
169
        if mapping is None:
167
170
            mapping = self.get_mapping()
168
 
        from dulwich.protocol import (
169
 
            ZERO_SHA,
170
 
            )
171
 
        if foreign_revid == ZERO_SHA:
172
 
            return revision.NULL_REVISION
173
 
        commit = self._git[foreign_revid]
174
 
        rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
175
 
            lambda x: None)
176
 
        # FIXME: check testament before doing this?
177
 
        if roundtrip_revid:
178
 
            return roundtrip_revid
179
 
        else:
180
 
            return rev.revision_id
 
171
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
181
172
 
182
173
    def has_signature_for_revision_id(self, revision_id):
183
174
        return False
184
175
 
185
 
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
 
176
    def lookup_bzr_revision_id(self, bzr_revid):
186
177
        try:
187
178
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
188
179
        except errors.InvalidRevisionId:
189
 
            if mapping is None:
190
 
                mapping = self.get_mapping()
191
 
            try:
192
 
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
193
 
                        mapping)
194
 
            except KeyError:
195
 
                # Update refs from Git commit objects
196
 
                # FIXME: Hitting this a lot will be very inefficient...
197
 
                for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
198
 
                    if not roundtrip_revid:
199
 
                        continue
200
 
                    refname = mapping.revid_as_refname(roundtrip_revid)
201
 
                    self._git.refs[refname] = git_sha
202
 
                    if roundtrip_revid == bzr_revid:
203
 
                        return git_sha, mapping
204
 
                raise errors.NoSuchRevision(self, bzr_revid)
 
180
            raise errors.NoSuchRevision(self, bzr_revid)
205
181
 
206
182
    def get_revision(self, revision_id):
207
183
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
208
184
        try:
209
 
            commit = self._git[git_commit_id]
 
185
            commit = self._git.commit(git_commit_id)
210
186
        except KeyError:
211
187
            raise errors.NoSuchRevision(self, revision_id)
212
 
        revision, roundtrip_revid, verifiers = mapping.import_commit(
213
 
            commit, self.lookup_foreign_revision_id)
 
188
        # print "fetched revision:", git_commit_id
 
189
        revision = mapping.import_commit(commit)
214
190
        assert revision is not None
215
 
        # FIXME: check verifiers ?
216
 
        if roundtrip_revid:
217
 
            revision.revision_id = roundtrip_revid
218
191
        return revision
219
192
 
220
193
    def has_revision(self, revision_id):
221
194
        try:
222
 
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
195
            self.get_revision(revision_id)
223
196
        except errors.NoSuchRevision:
224
197
            return False
225
 
        return (git_commit_id in self._git)
226
 
 
227
 
    def has_revisions(self, revision_ids):
228
 
        return set(filter(self.has_revision, revision_ids))
 
198
        else:
 
199
            return True
229
200
 
230
201
    def get_revisions(self, revids):
231
202
        return [self.get_revision(r) for r in revids]
257
228
                        ancestors=None):
258
229
        return GitVersionedFileChecker(self,
259
230
            text_key_references=text_key_references, ancestors=ancestors)
260
 
 
 
231
    
261
232
 
262
233
class GitVersionedFileChecker(repository._VersionedFileChecker):
263
234
 
283
254
        return target_repo_format.rich_root_data
284
255
 
285
256
    def get_foreign_tests_repository_factory(self):
286
 
        from bzrlib.plugins.git.tests.test_repository import (
287
 
            ForeignTestsRepositoryFactory,
288
 
            )
 
257
        from bzrlib.plugins.git.tests.test_repository import ForeignTestsRepositoryFactory
289
258
        return ForeignTestsRepositoryFactory()
290
259
 
291
260
    def network_name(self):