/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
16
17
"""Map from Git sha's to Bazaar objects."""
18
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
19
from __future__ import absolute_import
20
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
21
from dulwich.objects import (
22
    sha_to_hex,
23
    hex_to_sha,
24
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
25
import os
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
26
import threading
0.200.292 by Jelmer Vernooij
Fix formatting.
27
0.254.44 by Jelmer Vernooij
Add knit-based content cache for trees.
28
from dulwich.objects import (
29
    ShaFile,
30
    )
31
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
32
from ... import (
33
    errors as bzr_errors,
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
34
    osutils,
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
35
    registry,
0.200.528 by Jelmer Vernooij
Fix import.
36
    trace,
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
37
    )
38
from ...bzr import (
39
    btree_index as _mod_btree_index,
40
    index as _mod_index,
0.254.31 by Jelmer Vernooij
Initial work on CHKMap support.
41
    versionedfile,
0.200.528 by Jelmer Vernooij
Fix import.
42
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
43
from ...transport import (
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
44
    get_transport,
45
    )
0.200.230 by Jelmer Vernooij
Implement sha cache.
46
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
47
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
48
def get_cache_dir():
49
    try:
50
        from xdg.BaseDirectory import xdg_cache_home
51
    except ImportError:
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
52
        from ...config import config_dir
0.200.558 by Jelmer Vernooij
Create cache dir if it doesn't exist yet.
53
        ret = os.path.join(config_dir(), "git")
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
54
    else:
0.417.1 by Jelmer Vernooij
Change cache directory name from bazaar => breezy.
55
        ret = os.path.join(xdg_cache_home, "breezy", "git")
0.200.558 by Jelmer Vernooij
Create cache dir if it doesn't exist yet.
56
    if not os.path.isdir(ret):
57
        os.makedirs(ret)
58
    return ret
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
59
60
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
61
def get_remote_cache_transport(repository):
0.200.1027 by Jelmer Vernooij
mark remote git directories as not supporting working trees.
62
    """Retrieve the transport to use when accessing (unwritable) remote 
63
    repositories.
64
    """
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
65
    uuid = getattr(repository, "uuid", None)
66
    if uuid is None:
67
        path = get_cache_dir()
68
    else:
69
        path = os.path.join(get_cache_dir(), uuid)
70
        if not os.path.isdir(path):
71
            os.mkdir(path)
72
    return get_transport(path)
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
73
74
0.200.228 by Jelmer Vernooij
Split out map.
75
def check_pysqlite_version(sqlite3):
76
    """Check that sqlite library is compatible.
77
78
    """
0.200.675 by Jelmer Vernooij
Fix formatting.
79
    if (sqlite3.sqlite_version_info[0] < 3 or
80
            (sqlite3.sqlite_version_info[0] == 3 and
0.200.228 by Jelmer Vernooij
Split out map.
81
             sqlite3.sqlite_version_info[1] < 3)):
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
82
        trace.warning('Needs at least sqlite 3.3.x')
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
83
        raise bzr_errors.BzrError("incompatible sqlite library")
0.200.228 by Jelmer Vernooij
Split out map.
84
85
try:
86
    try:
87
        import sqlite3
88
        check_pysqlite_version(sqlite3)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
89
    except (ImportError, bzr_errors.BzrError), e:
0.200.228 by Jelmer Vernooij
Split out map.
90
        from pysqlite2 import dbapi2 as sqlite3
91
        check_pysqlite_version(sqlite3)
92
except:
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
93
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
0.200.228 by Jelmer Vernooij
Split out map.
94
            'module')
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
95
    raise bzr_errors.BzrError("missing sqlite library")
0.200.228 by Jelmer Vernooij
Split out map.
96
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
97
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
98
_mapdbs = threading.local()
99
def mapdbs():
100
    """Get a cache for this thread's db connections."""
101
    try:
102
        return _mapdbs.cache
103
    except AttributeError:
104
        _mapdbs.cache = {}
105
        return _mapdbs.cache
106
107
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
108
class GitShaMap(object):
109
    """Git<->Bzr revision id mapping database."""
110
111
    def lookup_git_sha(self, sha):
112
        """Lookup a Git sha in the database.
113
        :param sha: Git object sha
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
114
        :return: list with (type, type_data) tuples with type_data:
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
115
            commit: revid, tree_sha, verifiers
116
            blob: fileid, revid
117
            tree: fileid, revid
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
118
        """
119
        raise NotImplementedError(self.lookup_git_sha)
120
0.200.835 by Jelmer Vernooij
Rename lookup_{tree,blob} -> lookup_{tree,blob}_id.
121
    def lookup_blob_id(self, file_id, revision):
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
122
        """Retrieve a Git blob SHA by file id.
123
124
        :param file_id: File id of the file/symlink
0.200.806 by Jelmer Vernooij
Make revision_hint mandatory.
125
        :param revision: revision in which the file was last changed.
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
126
        """
0.200.835 by Jelmer Vernooij
Rename lookup_{tree,blob} -> lookup_{tree,blob}_id.
127
        raise NotImplementedError(self.lookup_blob_id)
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
128
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
129
    def lookup_tree_id(self, file_id, revision):
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
130
        """Retrieve a Git tree SHA by file id.
131
        """
0.200.835 by Jelmer Vernooij
Rename lookup_{tree,blob} -> lookup_{tree,blob}_id.
132
        raise NotImplementedError(self.lookup_tree_id)
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
133
0.200.1039 by Jelmer Vernooij
Add stub.
134
    def lookup_commit(self, revid):
135
        """Retrieve a Git commit SHA by Bazaar revision id.
136
        """
137
        raise NotImplementedError(self.lookup_commit)
138
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
139
    def revids(self):
140
        """List the revision ids known."""
141
        raise NotImplementedError(self.revids)
142
0.200.677 by Jelmer Vernooij
Implement TdbCache.missing_revisions().
143
    def missing_revisions(self, revids):
144
        """Return set of all the revisions that are not present."""
145
        present_revids = set(self.revids())
146
        if not isinstance(revids, set):
147
            revids = set(revids)
148
        return revids - present_revids
149
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
150
    def sha1s(self):
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
151
        """List the SHA1s."""
152
        raise NotImplementedError(self.sha1s)
153
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
154
    def start_write_group(self):
155
        """Start writing changes."""
156
157
    def commit_write_group(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
158
        """Commit any pending changes."""
159
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
160
    def abort_write_group(self):
161
        """Abort any pending changes."""
162
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
163
0.254.44 by Jelmer Vernooij
Add knit-based content cache for trees.
164
class ContentCache(object):
165
    """Object that can cache Git objects."""
166
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
167
    def add(self, object):
168
        """Add an object."""
169
        raise NotImplementedError(self.add)
170
171
    def add_multi(self, objects):
172
        """Add multiple objects."""
173
        for obj in objects:
174
            self.add(obj)
175
0.254.44 by Jelmer Vernooij
Add knit-based content cache for trees.
176
    def __getitem__(self, sha):
177
        """Retrieve an item, by SHA."""
178
        raise NotImplementedError(self.__getitem__)
179
180
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
181
class BzrGitCacheFormat(object):
0.254.51 by Jelmer Vernooij
Add some docstrings.
182
    """Bazaar-Git Cache Format."""
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
183
184
    def get_format_string(self):
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
185
        """Return a single-line unique format string for this cache format."""
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
186
        raise NotImplementedError(self.get_format_string)
187
188
    def open(self, transport):
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
189
        """Open this format on a transport."""
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
190
        raise NotImplementedError(self.open)
191
192
    def initialize(self, transport):
0.254.51 by Jelmer Vernooij
Add some docstrings.
193
        """Create a new instance of this cache format at transport."""
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
194
        transport.put_bytes('format', self.get_format_string())
195
196
    @classmethod
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
197
    def from_transport(self, transport):
198
        """Open a cache file present on a transport, or initialize one.
199
200
        :param transport: Transport to use
201
        :return: A BzrGitCache instance
202
        """
203
        try:
204
            format_name = transport.get_bytes('format')
205
            format = formats.get(format_name)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
206
        except bzr_errors.NoSuchFile:
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
207
            format = formats.get('default')
208
            format.initialize(transport)
209
        return format.open(transport)
210
211
    @classmethod
212
    def from_repository(cls, repository):
213
        """Open a cache file for a repository.
214
215
        This will use the repository's transport to store the cache file, or
216
        use the users global cache directory if the repository has no 
217
        transport associated with it.
218
219
        :param repository: Repository to open the cache for
220
        :return: A `BzrGitCache`
221
        """
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
222
        from ...transport.local import LocalTransport
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
223
        repo_transport = getattr(repository, "_transport", None)
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
224
        if (repo_transport is not None and
225
            isinstance(repo_transport, LocalTransport)):
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
226
            # Even if we don't write to this repo, we should be able
0.200.865 by Jelmer Vernooij
Support serving without --allow-writes.
227
            # to update its cache.
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
228
            try:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
229
                repo_transport = remove_readonly_transport_decorator(repo_transport)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
230
            except bzr_errors.ReadOnlyError:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
231
                transport = None
232
            else:
233
                try:
234
                    repo_transport.mkdir('git')
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
235
                except bzr_errors.FileExists:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
236
                    pass
237
                transport = repo_transport.clone('git')
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
238
        else:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
239
            transport = None
240
        if transport is None:
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
241
            transport = get_remote_cache_transport(repository)
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
242
        return cls.from_transport(transport)
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
243
244
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
245
class CacheUpdater(object):
0.254.51 by Jelmer Vernooij
Add some docstrings.
246
    """Base class for objects that can update a bzr-git cache."""
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
247
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
248
    def add_object(self, obj, bzr_key_data, path):
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
249
        """Add an object.
250
251
        :param obj: Object type ("commit", "blob" or "tree")
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
252
        :param bzr_key_data: bzr key store data or testament_sha in case
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
253
            of commit
254
        :param path: Path of the object (optional)
255
        """
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
256
        raise NotImplementedError(self.add_object)
257
258
    def finish(self):
259
        raise NotImplementedError(self.finish)
260
261
262
class BzrGitCache(object):
263
    """Caching backend."""
264
265
    def __init__(self, idmap, content_cache, cache_updater_klass):
266
        self.idmap = idmap
267
        self.content_cache = content_cache
268
        self._cache_updater_klass = cache_updater_klass
269
270
    def get_updater(self, rev):
0.254.51 by Jelmer Vernooij
Add some docstrings.
271
        """Update an object that implements the CacheUpdater interface for 
272
        updating this cache.
273
        """
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
274
        return self._cache_updater_klass(self, rev)
275
276
277
DictBzrGitCache = lambda: BzrGitCache(DictGitShaMap(), None, DictCacheUpdater)
278
279
280
class DictCacheUpdater(CacheUpdater):
0.254.51 by Jelmer Vernooij
Add some docstrings.
281
    """Cache updater for dict-based caches."""
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
282
283
    def __init__(self, cache, rev):
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
284
        self.cache = cache
285
        self.revid = rev.revision_id
286
        self.parent_revids = rev.parent_ids
287
        self._commit = None
288
        self._entries = []
289
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
290
    def add_object(self, obj, bzr_key_data, path):
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
291
        if obj.type_name == "commit":
292
            self._commit = obj
0.361.1 by Jelmer Vernooij
Don't use assert.
293
            if type(bzr_key_data) is not dict:
294
                raise TypeError(bzr_key_data)
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
295
            key = self.revid
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
296
            type_data = (self.revid, self._commit.tree, bzr_key_data)
0.200.853 by Jelmer Vernooij
Fix lookup of commits in tdb.
297
            self.cache.idmap._by_revid[self.revid] = obj.id
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
298
        elif obj.type_name in ("blob", "tree"):
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
299
            if bzr_key_data is not None:
0.421.6 by Jelmer Vernooij
Some more simplifications.
300
                key = type_data = bzr_key_data
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
301
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
302
        else:
303
            raise AssertionError
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
304
        entry = (obj.type_name, type_data)
305
        self.cache.idmap._by_sha.setdefault(obj.id, {})[key] = entry
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
306
307
    def finish(self):
308
        if self._commit is None:
309
            raise AssertionError("No commit object added")
310
        return self._commit
311
312
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
313
class DictGitShaMap(GitShaMap):
0.254.51 by Jelmer Vernooij
Add some docstrings.
314
    """Git SHA map that uses a dictionary."""
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
315
316
    def __init__(self):
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
317
        self._by_sha = {}
318
        self._by_fileid = {}
0.200.853 by Jelmer Vernooij
Fix lookup of commits in tdb.
319
        self._by_revid = {}
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
320
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
321
    def lookup_blob_id(self, fileid, revision):
322
        return self._by_fileid[revision][fileid]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
323
324
    def lookup_git_sha(self, sha):
0.261.2 by Jelmer Vernooij
Fix cache tests.
325
        for entry in self._by_sha[sha].itervalues():
326
            yield entry
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
327
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
328
    def lookup_tree_id(self, fileid, revision):
0.200.860 by Jelmer Vernooij
Fix bugs in two lookup_tree_id implementations and add a test for it.
329
        return self._by_fileid[revision][fileid]
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
330
0.200.853 by Jelmer Vernooij
Fix lookup of commits in tdb.
331
    def lookup_commit(self, revid):
332
        return self._by_revid[revid]
333
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
334
    def revids(self):
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
335
        for key, entries in self._by_sha.iteritems():
336
            for (type, type_data) in entries.values():
337
                if type == "commit":
338
                    yield type_data[0]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
339
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
340
    def sha1s(self):
0.200.753 by Jelmer Vernooij
Move lookup_tree/lookup_blob to a separate object.
341
        return self._by_sha.iterkeys()
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
342
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
343
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
344
class SqliteCacheUpdater(CacheUpdater):
345
346
    def __init__(self, cache, rev):
347
        self.cache = cache
0.200.850 by Jelmer Vernooij
Fix tests.
348
        self.db = self.cache.idmap.db
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
349
        self.revid = rev.revision_id
350
        self._commit = None
351
        self._trees = []
352
        self._blobs = []
353
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
354
    def add_object(self, obj, bzr_key_data, path):
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
355
        if obj.type_name == "commit":
356
            self._commit = obj
0.361.1 by Jelmer Vernooij
Don't use assert.
357
            if type(bzr_key_data) is not dict:
358
                raise TypeError(bzr_key_data)
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
359
            self._testament3_sha1 = bzr_key_data.get("testament3-sha1")
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
360
        elif obj.type_name == "tree":
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
361
            if bzr_key_data is not None:
0.421.6 by Jelmer Vernooij
Some more simplifications.
362
                self._trees.append((obj.id, bzr_key_data[0], bzr_key_data[1]))
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
363
        elif obj.type_name == "blob":
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
364
            if bzr_key_data is not None:
365
                self._blobs.append((obj.id, bzr_key_data[0], bzr_key_data[1]))
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
366
        else:
367
            raise AssertionError
368
369
    def finish(self):
370
        if self._commit is None:
371
            raise AssertionError("No commit object added")
0.200.850 by Jelmer Vernooij
Fix tests.
372
        self.db.executemany(
373
            "replace into trees (sha1, fileid, revid) values (?, ?, ?)",
374
            self._trees)
375
        self.db.executemany(
376
            "replace into blobs (sha1, fileid, revid) values (?, ?, ?)",
377
            self._blobs)
378
        self.db.execute(
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
379
            "replace into commits (sha1, revid, tree_sha, testament3_sha1) values (?, ?, ?, ?)",
380
            (self._commit.id, self.revid, self._commit.tree, self._testament3_sha1))
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
381
        return self._commit
382
383
384
SqliteBzrGitCache = lambda p: BzrGitCache(SqliteGitShaMap(p), None, SqliteCacheUpdater)
385
386
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
387
class SqliteGitCacheFormat(BzrGitCacheFormat):
388
389
    def get_format_string(self):
390
        return 'bzr-git sha map version 1 using sqlite\n'
391
392
    def open(self, transport):
393
        try:
394
            basepath = transport.local_abspath(".")
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
395
        except bzr_errors.NotLocalUrl:
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
396
            basepath = get_cache_dir()
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
397
        return SqliteBzrGitCache(os.path.join(basepath, "idmap.db"))
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
398
399
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
400
class SqliteGitShaMap(GitShaMap):
0.254.51 by Jelmer Vernooij
Add some docstrings.
401
    """Bazaar GIT Sha map that uses a sqlite database for storage."""
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
402
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
403
    def __init__(self, path=None):
404
        self.path = path
405
        if path is None:
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
406
            self.db = sqlite3.connect(":memory:")
407
        else:
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
408
            if not mapdbs().has_key(path):
409
                mapdbs()[path] = sqlite3.connect(path)
0.200.675 by Jelmer Vernooij
Fix formatting.
410
            self.db = mapdbs()[path]
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
411
        self.db.text_factory = str
0.200.230 by Jelmer Vernooij
Implement sha cache.
412
        self.db.executescript("""
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
413
        create table if not exists commits(
414
            sha1 text not null check(length(sha1) == 40),
415
            revid text not null,
416
            tree_sha text not null check(length(tree_sha) == 40)
417
        );
0.200.230 by Jelmer Vernooij
Implement sha cache.
418
        create index if not exists commit_sha1 on commits(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
419
        create unique index if not exists commit_revid on commits(revid);
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
420
        create table if not exists blobs(
421
            sha1 text not null check(length(sha1) == 40),
422
            fileid text not null,
423
            revid text not null
424
        );
0.200.230 by Jelmer Vernooij
Implement sha cache.
425
        create index if not exists blobs_sha1 on blobs(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
426
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
427
        create table if not exists trees(
0.255.1 by Jelmer Vernooij
Remove use of lookup_tree.
428
            sha1 text unique not null check(length(sha1) == 40),
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
429
            fileid text not null,
430
            revid text not null
431
        );
0.255.1 by Jelmer Vernooij
Remove use of lookup_tree.
432
        create unique index if not exists trees_sha1 on trees(sha1);
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
433
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
434
""")
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
435
        try:
436
            self.db.executescript(
437
                "ALTER TABLE commits ADD testament3_sha1 TEXT;")
438
        except sqlite3.OperationalError:
439
            pass # Column already exists.
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
440
0.254.19 by Jelmer Vernooij
Support upgrading sha maps.
441
    def __repr__(self):
442
        return "%s(%r)" % (self.__class__.__name__, self.path)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
443
0.200.487 by Jelmer Vernooij
Prevent deep recursion if the shamap is out of date.
444
    def lookup_commit(self, revid):
0.254.51 by Jelmer Vernooij
Add some docstrings.
445
        cursor = self.db.execute("select sha1 from commits where revid = ?", 
446
            (revid,))
447
        row = cursor.fetchone()
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
448
        if row is not None:
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
449
            return row[0]
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
450
        raise KeyError
0.200.231 by Jelmer Vernooij
Partially fix pull.
451
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
452
    def commit_write_group(self):
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
453
        self.db.commit()
454
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
455
    def lookup_blob_id(self, fileid, revision):
456
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
457
        if row is not None:
458
            return row[0]
459
        raise KeyError(fileid)
460
461
    def lookup_tree_id(self, fileid, revision):
0.200.860 by Jelmer Vernooij
Fix bugs in two lookup_tree_id implementations and add a test for it.
462
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, revision)).fetchone()
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
463
        if row is not None:
464
            return row[0]
465
        raise KeyError(fileid)
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
466
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
467
    def lookup_git_sha(self, sha):
468
        """Lookup a Git sha in the database.
469
470
        :param sha: Git object sha
471
        :return: (type, type_data) with type_data:
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
472
            commit: revid, tree sha, verifiers
473
            tree: fileid, revid
474
            blob: fileid, revid
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
475
        """
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
476
        found = False
477
        cursor = self.db.execute("select revid, tree_sha, testament3_sha1 from commits where sha1 = ?", (sha,))
478
        for row in cursor.fetchall():
479
            found = True
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
480
            if row[2] is not None:
481
                verifiers = {"testament3-sha1": row[2]}
482
            else:
483
                verifiers = {}
484
            yield ("commit", (row[0], row[1], verifiers))
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
485
        cursor = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,))
486
        for row in cursor.fetchall():
487
            found = True
488
            yield ("blob", row)
489
        cursor = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,))
490
        for row in cursor.fetchall():
491
            found = True
492
            yield ("tree", row)
493
        if not found:
494
            raise KeyError(sha)
0.200.230 by Jelmer Vernooij
Implement sha cache.
495
496
    def revids(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
497
        """List the revision ids known."""
0.248.7 by Jelmer Vernooij
Avoid fetching all sha1s at once.
498
        return (row for (row,) in self.db.execute("select revid from commits"))
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
499
500
    def sha1s(self):
501
        """List the SHA1s."""
502
        for table in ("blobs", "commits", "trees"):
0.254.26 by Jelmer Vernooij
Fix typo, cope with invalid shamaps a bit better.
503
            for (sha,) in self.db.execute("select sha1 from %s" % table):
504
                yield sha
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
505
506
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
507
class TdbCacheUpdater(CacheUpdater):
0.254.51 by Jelmer Vernooij
Add some docstrings.
508
    """Cache updater for tdb-based caches."""
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
509
510
    def __init__(self, cache, rev):
511
        self.cache = cache
512
        self.db = cache.idmap.db
513
        self.revid = rev.revision_id
514
        self.parent_revids = rev.parent_ids
515
        self._commit = None
516
        self._entries = []
517
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
518
    def add_object(self, obj, bzr_key_data, path):
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
519
        sha = obj.sha().digest()
520
        if obj.type_name == "commit":
0.200.853 by Jelmer Vernooij
Fix lookup of commits in tdb.
521
            self.db["commit\0" + self.revid] = "\0".join((sha, obj.tree))
0.361.1 by Jelmer Vernooij
Don't use assert.
522
            if type(bzr_key_data) is not dict:
523
                raise TypeError(bzr_key_data)
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
524
            type_data = (self.revid, obj.tree)
525
            try:
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
526
                type_data += (bzr_key_data["testament3-sha1"],)
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
527
            except KeyError:
528
                pass
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
529
            self._commit = obj
530
        elif obj.type_name == "blob":
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
531
            if bzr_key_data is None:
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
532
                return
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
533
            self.db["\0".join(("blob", bzr_key_data[0], bzr_key_data[1]))] = sha
534
            type_data = bzr_key_data
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
535
        elif obj.type_name == "tree":
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
536
            if bzr_key_data is None:
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
537
                return
0.421.6 by Jelmer Vernooij
Some more simplifications.
538
            type_data = bzr_key_data
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
539
        else:
540
            raise AssertionError
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
541
        entry = "\0".join((obj.type_name, ) + type_data) + "\n"
542
        key = "git\0" + sha
543
        try:
544
            oldval = self.db[key]
545
        except KeyError:
546
            self.db[key] = entry
547
        else:
0.261.3 by Jelmer Vernooij
Fix more tests.
548
            if oldval[-1] != "\n":
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
549
                self.db[key] = "".join([oldval, "\n", entry])
550
            else:
551
                self.db[key] = "".join([oldval, entry])
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
552
553
    def finish(self):
554
        if self._commit is None:
555
            raise AssertionError("No commit object added")
556
        return self._commit
557
558
559
TdbBzrGitCache = lambda p: BzrGitCache(TdbGitShaMap(p), None, TdbCacheUpdater)
0.200.479 by Jelmer Vernooij
Version tdb sha map.
560
0.200.1140 by Jelmer Vernooij
Update now that the control dir formats are no longer in __init__.
561
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
562
class TdbGitCacheFormat(BzrGitCacheFormat):
0.254.51 by Jelmer Vernooij
Add some docstrings.
563
    """Cache format for tdb-based caches."""
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
564
565
    def get_format_string(self):
566
        return 'bzr-git sha map version 3 using tdb\n'
567
568
    def open(self, transport):
569
        try:
0.200.1075 by Jelmer Vernooij
Fix compatibility with older versions of python-tdb.
570
            basepath = transport.local_abspath(".").encode(osutils._fs_enc)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
571
        except bzr_errors.NotLocalUrl:
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
572
            basepath = get_cache_dir()
0.361.1 by Jelmer Vernooij
Don't use assert.
573
        if type(basepath) is not str:
574
            raise TypeError(basepath)
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
575
        try:
0.200.850 by Jelmer Vernooij
Fix tests.
576
            return TdbBzrGitCache(os.path.join(basepath, "idmap.tdb"))
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
577
        except ImportError:
578
            raise ImportError(
579
                "Unable to open existing bzr-git cache because 'tdb' is not "
580
                "installed.")
581
582
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
583
class TdbGitShaMap(GitShaMap):
584
    """SHA Map that uses a TDB database.
585
586
    Entries:
587
0.200.476 by Jelmer Vernooij
Fix Tdb backend, use tdb if possible by default.
588
    "git <sha1>" -> "<type> <type-data1> <type-data2>"
589
    "commit revid" -> "<sha1> <tree-id>"
0.200.477 by Jelmer Vernooij
More tests for sha maps, fix cache misses in tdb.
590
    "tree fileid revid" -> "<sha1>"
591
    "blob fileid revid" -> "<sha1>"
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
592
    """
593
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
594
    TDB_MAP_VERSION = 3
595
    TDB_HASH_SIZE = 50000
596
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
597
    def __init__(self, path=None):
598
        import tdb
599
        self.path = path
600
        if path is None:
601
            self.db = {}
602
        else:
0.361.1 by Jelmer Vernooij
Don't use assert.
603
            if type(path) is not str:
604
                raise TypeError(path)
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
605
            if not mapdbs().has_key(path):
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
606
                mapdbs()[path] = tdb.Tdb(path, self.TDB_HASH_SIZE, tdb.DEFAULT,
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
607
                                          os.O_RDWR|os.O_CREAT)
0.200.676 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
608
            self.db = mapdbs()[path]
609
        try:
0.200.751 by Jelmer Vernooij
Unrelated small fixes - import, avoid storing tree info (no longer used).
610
            if int(self.db["version"]) not in (2, 3):
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
611
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
612
                              self.db["version"], self.TDB_MAP_VERSION)
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
613
                self.db.clear()
0.200.676 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
614
        except KeyError:
0.200.751 by Jelmer Vernooij
Unrelated small fixes - import, avoid storing tree info (no longer used).
615
            pass
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
616
        self.db["version"] = str(self.TDB_MAP_VERSION)
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
617
0.200.809 by Jelmer Vernooij
Use tdb transactions for write groups.
618
    def start_write_group(self):
619
        """Start writing changes."""
0.200.778 by Jelmer Vernooij
Use transactions in tdb.
620
        self.db.transaction_start()
0.200.809 by Jelmer Vernooij
Use tdb transactions for write groups.
621
622
    def commit_write_group(self):
623
        """Commit any pending changes."""
624
        self.db.transaction_commit()
625
626
    def abort_write_group(self):
627
        """Abort any pending changes."""
628
        self.db.transaction_cancel()
0.200.778 by Jelmer Vernooij
Use transactions in tdb.
629
0.200.750 by Jelmer Vernooij
Remove unused tree code, add mechanism for migrating between sha maps.
630
    def __repr__(self):
631
        return "%s(%r)" % (self.__class__.__name__, self.path)
632
0.200.487 by Jelmer Vernooij
Prevent deep recursion if the shamap is out of date.
633
    def lookup_commit(self, revid):
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
634
        try:
635
            return sha_to_hex(self.db["commit\0" + revid][:20])
636
        except KeyError:
637
            raise KeyError("No cache entry for %r" % revid)
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
638
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
639
    def lookup_blob_id(self, fileid, revision):
640
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
641
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
642
    def lookup_git_sha(self, sha):
643
        """Lookup a Git sha in the database.
644
645
        :param sha: Git object sha
646
        :return: (type, type_data) with type_data:
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
647
            commit: revid, tree sha
648
            blob: fileid, revid
649
            tree: fileid, revid
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
650
        """
0.200.564 by Jelmer Vernooij
Accept 'binary' shas.
651
        if len(sha) == 40:
652
            sha = hex_to_sha(sha)
0.261.2 by Jelmer Vernooij
Fix cache tests.
653
        value = self.db["git\0" + sha]
654
        for data in value.splitlines():
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
655
            data = data.split("\0")
656
            if data[0] == "commit":
657
                if len(data) == 3:
658
                    yield (data[0], (data[1], data[2], {}))
659
                else:
660
                    yield (data[0], (data[1], data[2], {"testament3-sha1": data[3]}))
0.261.2 by Jelmer Vernooij
Fix cache tests.
661
            elif data[0] in ("tree", "blob"):
662
                yield (data[0], tuple(data[1:]))
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
663
            else:
0.261.2 by Jelmer Vernooij
Fix cache tests.
664
                raise AssertionError("unknown type %r" % data[0])
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
665
0.200.677 by Jelmer Vernooij
Implement TdbCache.missing_revisions().
666
    def missing_revisions(self, revids):
667
        ret = set()
668
        for revid in revids:
669
            if self.db.get("commit\0" + revid) is None:
670
                ret.add(revid)
671
        return ret
672
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
673
    def revids(self):
674
        """List the revision ids known."""
675
        for key in self.db.iterkeys():
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
676
            if key.startswith("commit\0"):
677
                yield key[7:]
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
678
679
    def sha1s(self):
680
        """List the SHA1s."""
681
        for key in self.db.iterkeys():
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
682
            if key.startswith("git\0"):
683
                yield sha_to_hex(key[4:])
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
684
0.200.750 by Jelmer Vernooij
Remove unused tree code, add mechanism for migrating between sha maps.
685
0.254.44 by Jelmer Vernooij
Add knit-based content cache for trees.
686
class VersionedFilesContentCache(ContentCache):
687
688
    def __init__(self, vf):
689
        self._vf = vf
690
691
    def add(self, obj):
692
        self._vf.insert_record_stream(
693
            [versionedfile.ChunkedContentFactory((obj.id,), [], None,
694
                obj.as_legacy_object_chunks())])
695
696
    def __getitem__(self, sha):
697
        stream = self._vf.get_record_stream([(sha,)], 'unordered', True)
698
        entry = stream.next() 
699
        if entry.storage_kind == 'absent':
700
            raise KeyError(sha)
701
        return ShaFile._parse_legacy_object(entry.get_bytes_as('fulltext'))
702
703
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
704
class GitObjectStoreContentCache(ContentCache):
705
706
    def __init__(self, store):
707
        self.store = store
708
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
709
    def add_multi(self, objs):
710
        self.store.add_objects(objs)
711
712
    def add(self, obj, path):
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
713
        self.store.add_object(obj)
714
715
    def __getitem__(self, sha):
716
        return self.store[sha]
717
718
0.254.46 by Jelmer Vernooij
Merge trunk.
719
class IndexCacheUpdater(CacheUpdater):
720
721
    def __init__(self, cache, rev):
722
        self.cache = cache
723
        self.revid = rev.revision_id
724
        self.parent_revids = rev.parent_ids
725
        self._commit = None
726
        self._entries = []
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
727
        self._cache_objs = set()
0.254.46 by Jelmer Vernooij
Merge trunk.
728
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
729
    def add_object(self, obj, bzr_key_data, path):
0.254.46 by Jelmer Vernooij
Merge trunk.
730
        if obj.type_name == "commit":
731
            self._commit = obj
0.361.1 by Jelmer Vernooij
Don't use assert.
732
            if type(bzr_key_data) is not dict:
733
                raise TypeError(bzr_key_data)
0.254.47 by Jelmer Vernooij
Merge trunk.
734
            self.cache.idmap._add_git_sha(obj.id, "commit",
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
735
                (self.revid, obj.tree, bzr_key_data))
0.254.47 by Jelmer Vernooij
Merge trunk.
736
            self.cache.idmap._add_node(("commit", self.revid, "X"),
0.254.46 by Jelmer Vernooij
Merge trunk.
737
                " ".join((obj.id, obj.tree)))
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
738
            self._cache_objs.add((obj, path))
0.254.46 by Jelmer Vernooij
Merge trunk.
739
        elif obj.type_name == "blob":
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
740
            self.cache.idmap._add_git_sha(obj.id, "blob", bzr_key_data)
741
            self.cache.idmap._add_node(("blob", bzr_key_data[0],
742
                bzr_key_data[1]), obj.id)
0.254.46 by Jelmer Vernooij
Merge trunk.
743
        elif obj.type_name == "tree":
0.421.6 by Jelmer Vernooij
Some more simplifications.
744
            self.cache.idmap._add_git_sha(obj.id, "tree", bzr_key_data)
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
745
            self._cache_objs.add((obj, path))
0.254.46 by Jelmer Vernooij
Merge trunk.
746
        else:
747
            raise AssertionError
748
749
    def finish(self):
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
750
        self.cache.content_cache.add_multi(self._cache_objs)
0.254.46 by Jelmer Vernooij
Merge trunk.
751
        return self._commit
752
753
754
class IndexBzrGitCache(BzrGitCache):
755
756
    def __init__(self, transport=None):
757
        mapper = versionedfile.ConstantMapper("trees")
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
758
        shamap = IndexGitShaMap(transport.clone('index'))
759
        #trees_store = knit.make_file_factory(True, mapper)(transport)
760
        #content_cache = VersionedFilesContentCache(trees_store)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
761
        from .transportgit import TransportObjectStore
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
762
        store = TransportObjectStore(transport.clone('objects'))
763
        content_cache = GitObjectStoreContentCache(store)
764
        super(IndexBzrGitCache, self).__init__(shamap, content_cache,
0.254.47 by Jelmer Vernooij
Merge trunk.
765
                IndexCacheUpdater)
0.254.46 by Jelmer Vernooij
Merge trunk.
766
767
0.254.43 by Jelmer Vernooij
Merge trunk.
768
class IndexGitCacheFormat(BzrGitCacheFormat):
769
770
    def get_format_string(self):
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
771
        return 'bzr-git sha map with git object cache version 1\n'
0.254.43 by Jelmer Vernooij
Merge trunk.
772
773
    def initialize(self, transport):
774
        super(IndexGitCacheFormat, self).initialize(transport)
775
        transport.mkdir('index')
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
776
        transport.mkdir('objects')
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
777
        from .transportgit import TransportObjectStore
0.254.52 by Jelmer Vernooij
Merge trunk, use git objects to cache tree objects.
778
        TransportObjectStore.init(transport.clone('objects'))
0.254.43 by Jelmer Vernooij
Merge trunk.
779
780
    def open(self, transport):
0.254.46 by Jelmer Vernooij
Merge trunk.
781
        return IndexBzrGitCache(transport)
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
782
783
784
class IndexGitShaMap(GitShaMap):
0.254.31 by Jelmer Vernooij
Initial work on CHKMap support.
785
    """SHA Map that uses the Bazaar APIs to store a cache.
786
787
    BTree Index file with the following contents:
788
789
    ("git", <sha1>) -> "<type> <type-data1> <type-data2>"
790
    ("commit", <revid>) -> "<sha1> <tree-id>"
0.254.36 by Jelmer Vernooij
Merge trunk.
791
    ("blob", <fileid>, <revid>) -> <sha1>
792
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
793
    """
794
795
    def __init__(self, transport=None):
796
        if transport is None:
0.254.43 by Jelmer Vernooij
Merge trunk.
797
            self._transport = None
0.254.36 by Jelmer Vernooij
Merge trunk.
798
            self._index = _mod_index.InMemoryGraphIndex(0, key_elements=3)
0.254.2 by jelmer
use btree indexes
799
            self._builder = self._index
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
800
        else:
0.254.30 by Jelmer Vernooij
Move index to separate dir.
801
            self._builder = None
0.254.43 by Jelmer Vernooij
Merge trunk.
802
            self._transport = transport
0.254.2 by jelmer
use btree indexes
803
            self._index = _mod_index.CombinedGraphIndex([])
0.254.43 by Jelmer Vernooij
Merge trunk.
804
            for name in self._transport.list_dir("."):
0.254.2 by jelmer
use btree indexes
805
                if not name.endswith(".rix"):
806
                    continue
0.254.43 by Jelmer Vernooij
Merge trunk.
807
                x = _mod_btree_index.BTreeGraphIndex(self._transport, name,
808
                    self._transport.stat(name).st_size)
0.254.2 by jelmer
use btree indexes
809
                self._index.insert_index(0, x)
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
810
811
    @classmethod
812
    def from_repository(cls, repository):
813
        transport = getattr(repository, "_transport", None)
814
        if transport is not None:
0.254.2 by jelmer
use btree indexes
815
            try:
816
                transport.mkdir('git')
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
817
            except bzr_errors.FileExists:
0.254.2 by jelmer
use btree indexes
818
                pass
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
819
            return cls(transport.clone('git'))
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
820
        from ...transport import get_transport
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
821
        return cls(get_transport(get_cache_dir()))
822
0.254.19 by Jelmer Vernooij
Support upgrading sha maps.
823
    def __repr__(self):
824
        if self._transport is not None:
825
            return "%s(%r)" % (self.__class__.__name__, self._transport.base)
826
        else:
827
            return "%s()" % (self.__class__.__name__)
828
0.254.3 by John Arbash Meinel
Add repack function.
829
    def repack(self):
0.361.1 by Jelmer Vernooij
Don't use assert.
830
        if self._builder is not None:
831
            raise errors.BzrError('builder already open')
0.254.3 by John Arbash Meinel
Add repack function.
832
        self.start_write_group()
833
        for _, key, value in self._index.iter_all_entries():
834
            self._builder.add_node(key, value)
835
        to_remove = []
0.254.43 by Jelmer Vernooij
Merge trunk.
836
        for name in self._transport.list_dir('.'):
0.254.3 by John Arbash Meinel
Add repack function.
837
            if name.endswith('.rix'):
838
                to_remove.append(name)
839
        self.commit_write_group()
840
        del self._index.indices[1:]
841
        for name in to_remove:
0.254.43 by Jelmer Vernooij
Merge trunk.
842
            self._transport.rename(name, name + '.old')
0.254.3 by John Arbash Meinel
Add repack function.
843
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
844
    def start_write_group(self):
0.361.1 by Jelmer Vernooij
Don't use assert.
845
        if self._builder is not None:
846
            raise errors.BzrError('builder already open')
0.254.36 by Jelmer Vernooij
Merge trunk.
847
        self._builder = _mod_btree_index.BTreeBuilder(0, key_elements=3)
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
848
        self._name = osutils.sha()
849
850
    def commit_write_group(self):
0.361.1 by Jelmer Vernooij
Don't use assert.
851
        if self._builder is None:
852
            raise errors.BzrError('builder not open')
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
853
        stream = self._builder.finish()
0.254.2 by jelmer
use btree indexes
854
        name = self._name.hexdigest() + ".rix"
0.254.43 by Jelmer Vernooij
Merge trunk.
855
        size = self._transport.put_file(name, stream)
856
        index = _mod_btree_index.BTreeGraphIndex(self._transport, name, size)
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
857
        self._index.insert_index(0, index)
858
        self._builder = None
859
        self._name = None
860
861
    def abort_write_group(self):
0.361.1 by Jelmer Vernooij
Don't use assert.
862
        if self._builder is None:
863
            raise errors.BzrError('builder not open')
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
864
        self._builder = None
865
        self._name = None
866
0.254.15 by Jelmer Vernooij
Convenience function for adding index nodes.
867
    def _add_node(self, key, value):
868
        try:
869
            self._builder.add_node(key, value)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
870
        except bzr_errors.BadIndexDuplicateKey:
0.254.26 by Jelmer Vernooij
Fix typo, cope with invalid shamaps a bit better.
871
            # Multiple bzr objects can have the same contents
872
            return True
873
        else:
874
            return False
0.254.15 by Jelmer Vernooij
Convenience function for adding index nodes.
875
0.254.2 by jelmer
use btree indexes
876
    def _get_entry(self, key):
877
        entries = self._index.iter_entries([key])
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
878
        try:
0.254.2 by jelmer
use btree indexes
879
            return entries.next()[2]
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
880
        except StopIteration:
0.254.2 by jelmer
use btree indexes
881
            if self._builder is None:
882
                raise KeyError
883
            entries = self._builder.iter_entries([key])
884
            try:
885
                return entries.next()[2]
886
            except StopIteration:
887
                raise KeyError
888
0.261.2 by Jelmer Vernooij
Fix cache tests.
889
    def _iter_entries_prefix(self, prefix):
0.254.2 by jelmer
use btree indexes
890
        for entry in self._index.iter_entries_prefix([prefix]):
0.261.2 by Jelmer Vernooij
Fix cache tests.
891
            yield (entry[1], entry[2])
0.254.2 by jelmer
use btree indexes
892
        if self._builder is not None:
893
            for entry in self._builder.iter_entries_prefix([prefix]):
0.261.2 by Jelmer Vernooij
Fix cache tests.
894
                yield (entry[1], entry[2])
0.254.2 by jelmer
use btree indexes
895
896
    def lookup_commit(self, revid):
0.254.36 by Jelmer Vernooij
Merge trunk.
897
        return self._get_entry(("commit", revid, "X"))[:40]
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
898
0.254.33 by Jelmer Vernooij
Merge trunk.
899
    def _add_git_sha(self, hexsha, type, type_data):
0.254.2 by jelmer
use btree indexes
900
        if hexsha is not None:
901
            self._name.update(hexsha)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
902
            if type == "commit":
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
903
                td = (type_data[0], type_data[1])
904
                try:
905
                    td += (type_data[2]["testament3-sha1"],)
906
                except KeyError:
907
                    pass
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
908
            else:
909
                td = type_data
910
            self._add_node(("git", hexsha, "X"), " ".join((type,) + td))
0.254.2 by jelmer
use btree indexes
911
        else:
912
            # This object is not represented in Git - perhaps an empty
913
            # directory?
914
            self._name.update(type + " ".join(type_data))
0.254.33 by Jelmer Vernooij
Merge trunk.
915
0.254.42 by Jelmer Vernooij
Merge trunk.
916
    def lookup_blob_id(self, fileid, revision):
917
        return self._get_entry(("blob", fileid, revision))
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
918
919
    def lookup_git_sha(self, sha):
920
        if len(sha) == 20:
921
            sha = sha_to_hex(sha)
0.261.2 by Jelmer Vernooij
Fix cache tests.
922
        found = False
923
        for key, value in self._iter_entries_prefix(("git", sha, None)):
924
            found = True
925
            data = value.split(" ", 3)
926
            if data[0] == "commit":
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
927
                if data[3]:
928
                    verifiers = {"testament3-sha1": data[3]}
929
                else:
930
                    verifiers = {}
931
                yield ("commit", (data[1], data[2], verifiers))
0.261.2 by Jelmer Vernooij
Fix cache tests.
932
            else:
933
                yield (data[0], tuple(data[1:]))
934
        if not found:
935
            raise KeyError(sha)
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
936
937
    def revids(self):
938
        """List the revision ids known."""
0.261.2 by Jelmer Vernooij
Fix cache tests.
939
        for key, value in self._iter_entries_prefix(("commit", None, None)):
0.254.2 by jelmer
use btree indexes
940
            yield key[1]
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
941
0.254.21 by Jelmer Vernooij
Implement faster missing_revisions.
942
    def missing_revisions(self, revids):
943
        """Return set of all the revisions that are not present."""
944
        missing_revids = set(revids)
945
        for _, key, value in self._index.iter_entries((
0.254.37 by Jelmer Vernooij
merge trunk
946
            ("commit", revid, "X") for revid in revids)):
0.254.21 by Jelmer Vernooij
Implement faster missing_revisions.
947
            missing_revids.remove(key[1])
948
        return missing_revids
949
0.254.1 by Jelmer Vernooij
Add trivial index-based sha map.
950
    def sha1s(self):
951
        """List the SHA1s."""
0.261.2 by Jelmer Vernooij
Fix cache tests.
952
        for key, value in self._iter_entries_prefix(("git", None, None)):
0.254.2 by jelmer
use btree indexes
953
            yield key[1]
0.254.19 by Jelmer Vernooij
Support upgrading sha maps.
954
955
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
956
formats = registry.Registry()
957
formats.register(TdbGitCacheFormat().get_format_string(),
958
    TdbGitCacheFormat())
959
formats.register(SqliteGitCacheFormat().get_format_string(),
960
    SqliteGitCacheFormat())
0.254.43 by Jelmer Vernooij
Merge trunk.
961
formats.register(IndexGitCacheFormat().get_format_string(),
962
    IndexGitCacheFormat())
0.200.951 by Jelmer Vernooij
merge support for git object store-based caching mechanism.
963
# In the future, this will become the default:
964
# formats.register('default', IndexGitCacheFormat())
965
try:
966
    import tdb
967
except ImportError:
968
    formats.register('default', SqliteGitCacheFormat())
969
else:
970
    formats.register('default', TdbGitCacheFormat())
971
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
972
973
974
def migrate_ancient_formats(repo_transport):
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
975
    # Migrate older cache formats
976
    repo_transport = remove_readonly_transport_decorator(repo_transport)
977
    has_sqlite = repo_transport.has("git.db")
978
    has_tdb = repo_transport.has("git.tdb")
979
    if not has_sqlite or has_tdb:
980
        return
981
    try:
982
        repo_transport.mkdir("git")
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
983
    except bzr_errors.FileExists:
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
984
        return
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
985
    # Prefer migrating git.db over git.tdb, since the latter may not 
986
    # be openable on some platforms.
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
987
    if has_sqlite:
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
988
        SqliteGitCacheFormat().initialize(repo_transport.clone("git"))
989
        repo_transport.rename("git.db", "git/idmap.db")
0.200.1221 by Jelmer Vernooij
Support cache for non-local transport properly.
990
    elif has_tdb:
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
991
        TdbGitCacheFormat().initialize(repo_transport.clone("git"))
992
        repo_transport.rename("git.tdb", "git/idmap.tdb")
993
994
0.200.865 by Jelmer Vernooij
Support serving without --allow-writes.
995
def remove_readonly_transport_decorator(transport):
996
    if transport.is_readonly():
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
997
        try:
998
            return transport._decorated
999
        except AttributeError:
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
1000
            raise bzr_errors.ReadOnlyError(transport)
0.200.865 by Jelmer Vernooij
Support serving without --allow-writes.
1001
    return transport
1002
1003
0.254.19 by Jelmer Vernooij
Support upgrading sha maps.
1004
def from_repository(repository):
0.200.866 by Jelmer Vernooij
More docstrings, prefer migrating git.db to migrating git.tdb.
1005
    """Open a cache file for a repository.
1006
1007
    If the repository is remote and there is no transport available from it
1008
    this will use a local file in the users cache directory
1009
    (typically ~/.cache/bazaar/git/)
1010
1011
    :param repository: A repository object
1012
    """
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
1013
    repo_transport = getattr(repository, "_transport", None)
1014
    if repo_transport is not None:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
1015
        try:
1016
            migrate_ancient_formats(repo_transport)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
1017
        except bzr_errors.ReadOnlyError:
0.200.1438 by Jelmer Vernooij
Cope with remote branches not being readonly at all better.
1018
            pass # Not much we can do
0.200.844 by Jelmer Vernooij
Add infrastructure for multiple cache formats.
1019
    return BzrGitCacheFormat.from_repository(repository)