/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

  • Committer: Jelmer Vernooij
  • Date: 2010-05-13 12:34:24 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100513123424-c1sk9vcg2ekrcsol
Some refactoring, support proper file ids in revision deltas.

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