/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

More tests for sha maps, fix cache misses in tdb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
16
17
 
17
18
"""An adapter between a Git Repository and a Bazaar Branch"""
18
19
 
19
 
import os
20
 
import time
21
 
 
22
20
import bzrlib
23
21
from bzrlib import (
24
 
    deprecated_graph,
25
22
    errors,
26
23
    graph,
27
24
    inventory,
29
26
    repository,
30
27
    revision,
31
28
    revisiontree,
 
29
    ui,
32
30
    urlutils,
33
 
    versionedfile,
34
31
    )
35
32
from bzrlib.foreign import (
36
 
        ForeignRepository,
37
 
        )
38
 
from bzrlib.trace import mutter
39
 
from bzrlib.transport import get_transport
 
33
    ForeignRepository,
 
34
    )
 
35
from bzrlib.trace import (
 
36
    mutter,
 
37
    )
 
38
from bzrlib.transport import (
 
39
    get_transport,
 
40
    )
40
41
 
 
42
from bzrlib.plugins.git.commit import (
 
43
    GitCommitBuilder,
 
44
    )
41
45
from bzrlib.plugins.git.foreign import (
42
46
    versionedfiles,
43
47
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping
45
 
 
46
 
from bzrlib.plugins.git import git
 
48
from bzrlib.plugins.git.inventory import (
 
49
    GitInventory,
 
50
    )
 
51
from bzrlib.plugins.git.mapping import (
 
52
    default_mapping,
 
53
    foreign_git,
 
54
    mapping_registry,
 
55
    )
 
56
from bzrlib.plugins.git.versionedfiles import (
 
57
    GitTexts,
 
58
    )
47
59
 
48
60
 
49
61
class GitTags(object):
59
71
    """An adapter to git repositories for bzr."""
60
72
 
61
73
    _serializer = None
 
74
    _commit_builder_class = GitCommitBuilder
 
75
    vcs = foreign_git
62
76
 
63
77
    def __init__(self, gitdir, lockfiles):
64
 
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
 
        from bzrlib.plugins.git import fetch
66
 
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
 
78
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
79
            lockfiles)
 
80
        from bzrlib.plugins.git import fetch, push
 
81
        for optimiser in [fetch.InterRemoteGitNonGitRepository, 
 
82
                          fetch.InterLocalGitNonGitRepository,
 
83
                          fetch.InterGitGitRepository,
 
84
                          push.InterToLocalGitRepository,
 
85
                          push.InterToRemoteGitRepository]:
 
86
            repository.InterRepository.register_optimiser(optimiser)
67
87
 
68
88
    def is_shared(self):
69
89
        return True
81
101
    def make_working_trees(self):
82
102
        return True
83
103
 
 
104
    def dfetch(self, source, stop_revision):
 
105
        interrepo = repository.InterRepository.get(source, self)
 
106
        return interrepo.dfetch(stop_revision)
 
107
 
 
108
    def dfetch_refs(self, source, stop_revision):
 
109
        interrepo = repository.InterRepository.get(source, self)
 
110
        return interrepo.dfetch_refs(stop_revision)
 
111
 
84
112
 
85
113
class LocalGitRepository(GitRepository):
 
114
    """Git repository on the file system."""
86
115
 
87
116
    def __init__(self, gitdir, lockfiles):
88
117
        # FIXME: This also caches negatives. Need to be more careful 
94
123
        self.texts = None
95
124
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
96
125
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
 
126
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
127
        self.texts = GitTexts(self)
97
128
        self.tags = GitTags(self._git.get_tags())
98
129
 
99
130
    def all_revision_ids(self):
100
131
        ret = set([revision.NULL_REVISION])
101
 
        if self._git.heads() == []:
 
132
        heads = self._git.heads()
 
133
        if heads == {}:
102
134
            return ret
103
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
 
135
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
104
136
        ret = set(bzr_heads)
105
137
        graph = self.get_graph()
106
138
        for rev, parents in graph.iter_ancestry(bzr_heads):
107
139
            ret.add(rev)
108
140
        return ret
109
141
 
110
 
    #def get_revision_delta(self, revision_id):
111
 
    #    parent_revid = self.get_revision(revision_id).parent_ids[0]
112
 
    #    diff = self._git.diff(ids.convert_revision_id_bzr_to_git(parent_revid),
113
 
    #                   ids.convert_revision_id_bzr_to_git(revision_id))
114
 
 
115
142
    def _make_parents_provider(self):
116
143
        """See Repository._make_parents_provider()."""
117
144
        return self._parents_provider
118
145
 
119
146
    def get_parent_map(self, revids):
120
147
        parent_map = {}
121
 
        mutter("get_parent_map(%r)", revids)
122
148
        for revision_id in revids:
123
149
            assert isinstance(revision_id, str)
124
150
            if revision_id == revision.NULL_REVISION:
125
151
                parent_map[revision_id] = ()
126
152
                continue
127
 
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
 
153
            hexsha, mapping = self.lookup_git_revid(revision_id)
128
154
            commit  = self._git.commit(hexsha)
129
155
            if commit is None:
130
156
                continue
131
157
            else:
132
 
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
 
158
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
133
159
        return parent_map
134
160
 
135
161
    def get_ancestry(self, revision_id, topo_sorted=True):
136
162
        """See Repository.get_ancestry().
137
163
        """
138
164
        if revision_id is None:
139
 
            return self._all_revision_ids()
 
165
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
140
166
        assert isinstance(revision_id, str)
141
167
        ancestry = []
142
168
        graph = self.get_graph()
143
169
        for rev, parents in graph.iter_ancestry([revision_id]):
144
 
            if rev == revision.NULL_REVISION:
145
 
                rev = None
146
170
            ancestry.append(rev)
147
171
        ancestry.reverse()
148
 
        return ancestry
 
172
        return [None] + ancestry
149
173
 
150
174
    def get_signature_text(self, revision_id):
151
175
        raise errors.NoSuchRevision(self, revision_id)
162
186
    def has_signature_for_revision_id(self, revision_id):
163
187
        return False
164
188
 
165
 
    def lookup_git_revid(self, bzr_revid, mapping):
 
189
    def lookup_git_revid(self, bzr_revid):
166
190
        try:
167
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
 
191
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
168
192
        except errors.InvalidRevisionId:
169
193
            raise errors.NoSuchRevision(self, bzr_revid)
170
194
 
171
195
    def get_revision(self, revision_id):
172
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
196
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
173
197
        try:
174
198
            commit = self._git.commit(git_commit_id)
175
199
        except KeyError:
176
200
            raise errors.NoSuchRevision(self, revision_id)
177
201
        # print "fetched revision:", git_commit_id
178
 
        revision = self.get_mapping().import_commit(commit)
 
202
        revision = mapping.import_commit(commit)
179
203
        assert revision is not None
180
204
        return revision
181
205
 
196
220
 
197
221
    def revision_tree(self, revision_id):
198
222
        revision_id = revision.ensure_null(revision_id)
199
 
 
200
223
        if revision_id == revision.NULL_REVISION:
201
224
            inv = inventory.Inventory(root_id=None)
202
225
            inv.revision_id = revision_id
203
226
            return revisiontree.RevisionTree(self, inv, revision_id)
204
 
 
205
 
        return GitRevisionTree(self, self.get_mapping(), revision_id)
 
227
        return GitRevisionTree(self, revision_id)
206
228
 
207
229
    def get_inventory(self, revision_id):
208
230
        assert revision_id != None
211
233
    def set_make_working_trees(self, trees):
212
234
        pass
213
235
 
214
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
236
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
237
        progress=None):
215
238
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
216
239
 
217
240
 
218
241
class GitRevisionTree(revisiontree.RevisionTree):
219
242
 
220
 
    def __init__(self, repository, mapping, revision_id):
 
243
    def __init__(self, repository, revision_id):
221
244
        self._repository = repository
222
 
        self.revision_id = revision_id
 
245
        self._revision_id = revision_id
223
246
        assert isinstance(revision_id, str)
224
 
        self.mapping = mapping
225
 
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
 
247
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
248
        try:
227
249
            commit = repository._git.commit(git_id)
228
250
        except KeyError, r:
229
251
            raise errors.NoSuchRevision(repository, revision_id)
230
252
        self.tree = commit.tree
231
 
        self._inventory = inventory.Inventory(revision_id=revision_id)
232
 
        self._inventory.root.revision = revision_id
233
 
        self._build_inventory(self.tree, self._inventory.root, "")
 
253
        self._inventory = GitInventory(self.tree, self.mapping, repository._git.object_store, revision_id)
234
254
 
235
255
    def get_revision_id(self):
236
 
        return self.revision_id
 
256
        return self._revision_id
237
257
 
238
258
    def get_file_text(self, file_id):
239
259
        entry = self._inventory[file_id]
240
260
        if entry.kind == 'directory': return ""
241
 
        return self._repository._git.get_blob(entry.text_id).data
242
 
 
243
 
    def _build_inventory(self, tree_id, ie, path):
244
 
        assert isinstance(path, str)
245
 
        tree = self._repository._git.tree(tree_id)
246
 
        for mode, name, hexsha in tree.entries():
247
 
            basename = name.decode("utf-8")
248
 
            if path == "":
249
 
                child_path = name
250
 
            else:
251
 
                child_path = urlutils.join(path, name)
252
 
            file_id = self.mapping.generate_file_id(child_path)
253
 
            entry_kind = (mode & 0700000) / 0100000
254
 
            if entry_kind == 0:
255
 
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
256
 
            elif entry_kind == 1:
257
 
                file_kind = (mode & 070000) / 010000
258
 
                b = self._repository._git.get_blob(hexsha)
259
 
                if file_kind == 0:
260
 
                    child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
261
 
                    child_ie.text_sha1 = osutils.sha_string(b.data)
262
 
                elif file_kind == 2:
263
 
                    child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
264
 
                    child_ie.text_sha1 = osutils.sha_string("")
265
 
                else:
266
 
                    raise AssertionError(
267
 
                        "Unknown file kind, perms=%o." % (mode,))
268
 
                child_ie.text_id = b.id
269
 
                child_ie.text_size = len(b.data)
270
 
            else:
271
 
                raise AssertionError(
272
 
                    "Unknown blob kind, perms=%r." % (mode,))
273
 
            fs_mode = mode & 0777
274
 
            child_ie.executable = bool(fs_mode & 0111)
275
 
            child_ie.revision = self.revision_id
276
 
            self._inventory.add(child_ie)
277
 
            if entry_kind == 0:
278
 
                self._build_inventory(hexsha, child_ie, child_path)
279
 
 
280
 
 
281
 
class GitFormat(object):
 
261
        return entry.object.data
 
262
 
 
263
 
 
264
class GitRepositoryFormat(repository.RepositoryFormat):
 
265
    """Git repository format."""
282
266
 
283
267
    supports_tree_reference = False
284
268
    rich_root_data = True