/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-02 19:26:28 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100502192628-dy0lavjbjexavvmb
More work on roundtripping support.

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.append(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
            try:
 
172
                return self._git.refs["refs/bzr/%s" % bzr_revid], self.get_mapping()
 
173
            except KeyError:
 
174
                raise errors.NoSuchRevision(self, bzr_revid)
185
175
 
186
176
    def get_revision(self, revision_id):
187
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
177
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
188
178
        try:
189
 
            commit = self._git.commit(git_commit_id)
 
179
            commit = self._git[git_commit_id]
190
180
        except KeyError:
191
181
            raise errors.NoSuchRevision(self, revision_id)
192
182
        # print "fetched revision:", git_commit_id
196
186
 
197
187
    def has_revision(self, revision_id):
198
188
        try:
199
 
            self.get_revision(revision_id)
 
189
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
200
190
        except errors.NoSuchRevision:
201
191
            return False
202
 
        else:
203
 
            return True
 
192
        return (git_commit_id in self._git)
 
193
 
 
194
    def has_revisions(self, revision_ids):
 
195
        ret = set()
 
196
        for revid in revision_ids:
 
197
            if self.has_revision(revid):
 
198
                ret.add(revid)
 
199
        return ret
204
200
 
205
201
    def get_revisions(self, revids):
206
202
        return [self.get_revision(r) for r in revids]
228
224
        progress=None):
229
225
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
230
226
 
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
 
227
    def _get_versioned_file_checker(self, text_key_references=None,
 
228
                        ancestors=None):
 
229
        return GitVersionedFileChecker(self,
 
230
            text_key_references=text_key_references, ancestors=ancestors)
 
231
    
 
232
 
 
233
class GitVersionedFileChecker(repository._VersionedFileChecker):
 
234
 
 
235
    file_ids = []
 
236
 
 
237
    def _check_file_version_parents(self, texts, progress_bar):
 
238
        return {}, []
258
239
 
259
240
 
260
241
class GitRepositoryFormat(repository.RepositoryFormat):
267
248
        return "Git Repository"
268
249
 
269
250
    def initialize(self, url, shared=False, _internal=False):
270
 
        raise bzr_errors.UninitializableFormat(self)
 
251
        raise errors.UninitializableFormat(self)
271
252
 
272
253
    def check_conversion_target(self, target_repo_format):
273
254
        return target_repo_format.rich_root_data
274
255
 
 
256
    def get_foreign_tests_repository_factory(self):
 
257
        from bzrlib.plugins.git.tests.test_repository import (
 
258
            ForeignTestsRepositoryFactory,
 
259
            )
 
260
        return ForeignTestsRepositoryFactory()
 
261
 
275
262
    def network_name(self):
276
263
        return "git"