/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 08:40:18 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100513084018-xp9tbrasde6gih3o
Checks for roundtripping.

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)
100
95
        interrepo = repository.InterRepository.get(source, self)
101
96
        return interrepo.dfetch_refs(stop_revision)
102
97
 
 
98
    def fetch_refs(self, source, stop_revision):
 
99
        interrepo = repository.InterRepository.get(source, self)
 
100
        return interrepo.fetch_refs(stop_revision)
 
101
 
103
102
 
104
103
class LocalGitRepository(GitRepository):
105
104
    """Git repository on the file system."""
106
105
 
107
106
    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
107
        GitRepository.__init__(self, gitdir, lockfiles)
112
108
        self.base = gitdir.root_transport.base
113
109
        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)
 
110
        self.signatures = None
 
111
        self.revisions = GitRevisions(self, self._git.object_store)
 
112
        self.inventories = None
118
113
        self.texts = GitTexts(self)
119
114
 
120
115
    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)
 
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.append(rev.revision_id)
130
123
        return ret
131
124
 
132
 
    def _make_parents_provider(self):
133
 
        """See Repository._make_parents_provider()."""
134
 
        return self._parents_provider
135
 
 
136
125
    def get_parent_map(self, revids):
137
126
        parent_map = {}
138
127
        for revision_id in revids:
140
129
            if revision_id == revision.NULL_REVISION:
141
130
                parent_map[revision_id] = ()
142
131
                continue
143
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
144
 
            commit  = self._git.commit(hexsha)
145
 
            if commit is None:
 
132
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
133
            try:
 
134
                commit = self._git[hexsha]
 
135
            except KeyError:
146
136
                continue
147
 
            else:
148
 
                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]
149
138
        return parent_map
150
139
 
151
140
    def get_ancestry(self, revision_id, topo_sorted=True):
164
153
    def get_signature_text(self, revision_id):
165
154
        raise errors.NoSuchRevision(self, revision_id)
166
155
 
167
 
    def lookup_revision_id(self, revid):
 
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
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
175
163
 
176
164
    def has_signature_for_revision_id(self, revision_id):
177
165
        return False
178
166
 
179
 
    def lookup_git_revid(self, bzr_revid):
 
167
    def lookup_bzr_revision_id(self, bzr_revid):
180
168
        try:
181
169
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
182
170
        except errors.InvalidRevisionId:
183
 
            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)
184
176
 
185
177
    def get_revision(self, revision_id):
186
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
178
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
187
179
        try:
188
 
            commit = self._git.commit(git_commit_id)
 
180
            commit = self._git[git_commit_id]
189
181
        except KeyError:
190
182
            raise errors.NoSuchRevision(self, revision_id)
191
183
        # print "fetched revision:", git_commit_id
195
187
 
196
188
    def has_revision(self, revision_id):
197
189
        try:
198
 
            self.get_revision(revision_id)
 
190
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
199
191
        except errors.NoSuchRevision:
200
192
            return False
201
 
        else:
202
 
            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
203
201
 
204
202
    def get_revisions(self, revids):
205
203
        return [self.get_revision(r) for r in revids]
227
225
        progress=None):
228
226
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
229
227
 
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
 
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 {}, []
252
240
 
253
241
 
254
242
class GitRepositoryFormat(repository.RepositoryFormat):
261
249
        return "Git Repository"
262
250
 
263
251
    def initialize(self, url, shared=False, _internal=False):
264
 
        raise bzr_errors.UninitializableFormat(self)
 
252
        raise errors.UninitializableFormat(self)
265
253
 
266
254
    def check_conversion_target(self, target_repo_format):
267
255
        return target_repo_format.rich_root_data
 
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
 
 
263
    def network_name(self):
 
264
        return "git"