/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

add basic test for tree_to_objects.

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
 
import dulwich as git
61
 
import os
62
 
import time
63
 
 
64
 
 
65
 
class GitTags(object):
66
 
 
67
 
    def __init__(self, tags):
68
 
        self._tags = tags
69
 
 
70
 
    def __iter__(self):
71
 
        return iter(self._tags)
 
47
 
 
48
from dulwich.objects import (
 
49
    Commit,
 
50
    )
72
51
 
73
52
 
74
53
class GitRepository(ForeignRepository):
79
58
    vcs = foreign_git
80
59
 
81
60
    def __init__(self, gitdir, lockfiles):
82
 
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
61
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
83
62
            lockfiles)
84
63
        from bzrlib.plugins.git import fetch, push
85
 
        for optimiser in [fetch.InterRemoteGitNonGitRepository, 
 
64
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
86
65
                          fetch.InterLocalGitNonGitRepository,
87
 
                          fetch.InterGitRepository,
88
 
                          push.InterToGitRepository]:
 
66
                          fetch.InterGitGitRepository,
 
67
                          push.InterToLocalGitRepository,
 
68
                          push.InterToRemoteGitRepository]:
89
69
            repository.InterRepository.register_optimiser(optimiser)
90
70
 
91
71
    def is_shared(self):
92
 
        return True
 
72
        return False
93
73
 
94
74
    def supports_rich_root(self):
95
75
        return True
96
76
 
97
 
    def _warn_if_deprecated(self):
 
77
    def _warn_if_deprecated(self, branch=None):
98
78
        # This class isn't deprecated
99
79
        pass
100
80
 
104
84
    def make_working_trees(self):
105
85
        return True
106
86
 
 
87
    def revision_graph_can_have_wrong_parents(self):
 
88
        return False
 
89
 
 
90
    def dfetch(self, source, stop_revision):
 
91
        interrepo = repository.InterRepository.get(source, self)
 
92
        return interrepo.dfetch(stop_revision)
 
93
 
107
94
 
108
95
class LocalGitRepository(GitRepository):
109
96
    """Git repository on the file system."""
110
97
 
111
98
    def __init__(self, gitdir, lockfiles):
112
 
        # FIXME: This also caches negatives. Need to be more careful 
113
 
        # about this once we start writing to git
114
 
        self._parents_provider = graph.CachingParentsProvider(self)
115
99
        GitRepository.__init__(self, gitdir, lockfiles)
116
100
        self.base = gitdir.root_transport.base
117
101
        self._git = gitdir._git
118
 
        self.texts = None
119
 
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
120
 
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
121
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
102
        self.signatures = None
 
103
        self.revisions = GitRevisions(self, self._git.object_store)
 
104
        self.inventories = None
122
105
        self.texts = GitTexts(self)
123
 
        self.tags = GitTags(self._git.get_tags())
 
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
124
115
 
125
116
    def all_revision_ids(self):
126
 
        ret = set([revision.NULL_REVISION])
127
 
        heads = self._git.heads()
128
 
        if heads == {}:
129
 
            return ret
130
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
131
 
        ret = set(bzr_heads)
132
 
        graph = self.get_graph()
133
 
        for rev, parents in graph.iter_ancestry(bzr_heads):
134
 
            ret.add(rev)
 
117
        ret = set([])
 
118
        for git_sha, revid in self._iter_revision_ids():
 
119
            ret.add(revid)
135
120
        return ret
136
121
 
137
 
    #def get_revision_delta(self, revision_id):
138
 
    #    parent_revid = self.get_revision(revision_id).parent_ids[0]
139
 
    #    diff = self._git.diff(ids.convert_revision_id_bzr_to_git(parent_revid),
140
 
    #                   ids.convert_revision_id_bzr_to_git(revision_id))
141
 
 
142
 
    def _make_parents_provider(self):
143
 
        """See Repository._make_parents_provider()."""
144
 
        return self._parents_provider
145
 
 
146
122
    def get_parent_map(self, revids):
147
123
        parent_map = {}
148
124
        for revision_id in revids:
150
126
            if revision_id == revision.NULL_REVISION:
151
127
                parent_map[revision_id] = ()
152
128
                continue
153
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
154
 
            commit  = self._git.commit(hexsha)
155
 
            if commit is None:
 
129
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
130
            try:
 
131
                commit = self._git[hexsha]
 
132
            except KeyError:
156
133
                continue
157
 
            else:
158
 
                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]
159
135
        return parent_map
160
136
 
161
137
    def get_ancestry(self, revision_id, topo_sorted=True):
171
147
        ancestry.reverse()
172
148
        return [None] + ancestry
173
149
 
174
 
    def dfetch(self, source, stop_revision):
175
 
        interrepo = repository.InterRepository.get(source, self)
176
 
        return interrepo.dfetch(stop_revision)
177
 
 
178
150
    def get_signature_text(self, revision_id):
179
151
        raise errors.NoSuchRevision(self, revision_id)
180
152
 
181
 
    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):
182
157
        """Lookup a revision id.
183
 
        
184
 
        :param revid: Bazaar revision id.
185
 
        :return: Tuple with git revisionid and mapping.
 
158
 
186
159
        """
187
 
        # Yes, this doesn't really work, but good enough as a stub
188
 
        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
189
170
 
190
171
    def has_signature_for_revision_id(self, revision_id):
191
172
        return False
192
173
 
193
 
    def lookup_git_revid(self, bzr_revid):
 
174
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
194
175
        try:
195
176
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
196
177
        except errors.InvalidRevisionId:
197
 
            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)
198
190
 
199
191
    def get_revision(self, revision_id):
200
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
192
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
201
193
        try:
202
 
            commit = self._git.commit(git_commit_id)
 
194
            commit = self._git[git_commit_id]
203
195
        except KeyError:
204
196
            raise errors.NoSuchRevision(self, revision_id)
205
197
        # print "fetched revision:", git_commit_id
206
 
        revision = mapping.import_commit(commit)
 
198
        revision = mapping.import_commit(commit,
 
199
            self.lookup_foreign_revision_id)
207
200
        assert revision is not None
208
201
        return revision
209
202
 
210
203
    def has_revision(self, revision_id):
211
204
        try:
212
 
            self.get_revision(revision_id)
 
205
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
213
206
        except errors.NoSuchRevision:
214
207
            return False
215
 
        else:
216
 
            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))
217
212
 
218
213
    def get_revisions(self, revids):
219
214
        return [self.get_revision(r) for r in revids]
241
236
        progress=None):
242
237
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
243
238
 
244
 
 
245
 
class GitRevisionTree(revisiontree.RevisionTree):
246
 
 
247
 
    def __init__(self, repository, revision_id):
248
 
        self._repository = repository
249
 
        self._revision_id = revision_id
250
 
        assert isinstance(revision_id, str)
251
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
252
 
        try:
253
 
            commit = repository._git.commit(git_id)
254
 
        except KeyError, r:
255
 
            raise errors.NoSuchRevision(repository, revision_id)
256
 
        self.tree = commit.tree
257
 
        self._inventory = GitInventory(self.tree, self.mapping, repository._git.object_store, revision_id)
258
 
 
259
 
    def get_revision_id(self):
260
 
        return self._revision_id
261
 
 
262
 
    def get_file_text(self, file_id):
263
 
        entry = self._inventory[file_id]
264
 
        if entry.kind == 'directory': return ""
265
 
        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 {}, []
 
251
 
266
252
 
267
253
class GitRepositoryFormat(repository.RepositoryFormat):
 
254
    """Git repository format."""
268
255
 
269
256
    supports_tree_reference = False
270
257
    rich_root_data = True
273
260
        return "Git Repository"
274
261
 
275
262
    def initialize(self, url, shared=False, _internal=False):
276
 
        raise bzr_errors.UninitializableFormat(self)
 
263
        raise errors.UninitializableFormat(self)
277
264
 
278
265
    def check_conversion_target(self, target_repo_format):
279
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"