/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Map from Git sha's to Bazaar objects."""
18
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
19
from dulwich.objects import (
20
    sha_to_hex,
21
    hex_to_sha,
22
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
23
import os
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
24
import threading
0.200.292 by Jelmer Vernooij
Fix formatting.
25
0.200.228 by Jelmer Vernooij
Split out map.
26
import bzrlib
0.200.528 by Jelmer Vernooij
Fix import.
27
from bzrlib import (
28
    trace,
29
    )
0.200.230 by Jelmer Vernooij
Implement sha cache.
30
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
31
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
32
def get_cache_dir():
33
    try:
34
        from xdg.BaseDirectory import xdg_cache_home
35
    except ImportError:
0.200.558 by Jelmer Vernooij
Create cache dir if it doesn't exist yet.
36
        from bzrlib.config import config_dir
37
        ret = os.path.join(config_dir(), "git")
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
38
    else:
0.200.558 by Jelmer Vernooij
Create cache dir if it doesn't exist yet.
39
        ret = os.path.join(xdg_cache_home, "bazaar", "git")
40
    if not os.path.isdir(ret):
41
        os.makedirs(ret)
42
    return ret
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
43
44
0.200.228 by Jelmer Vernooij
Split out map.
45
def check_pysqlite_version(sqlite3):
46
    """Check that sqlite library is compatible.
47
48
    """
0.200.675 by Jelmer Vernooij
Fix formatting.
49
    if (sqlite3.sqlite_version_info[0] < 3 or
50
            (sqlite3.sqlite_version_info[0] == 3 and
0.200.228 by Jelmer Vernooij
Split out map.
51
             sqlite3.sqlite_version_info[1] < 3)):
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
52
        trace.warning('Needs at least sqlite 3.3.x')
0.200.228 by Jelmer Vernooij
Split out map.
53
        raise bzrlib.errors.BzrError("incompatible sqlite library")
54
55
try:
56
    try:
57
        import sqlite3
58
        check_pysqlite_version(sqlite3)
0.200.675 by Jelmer Vernooij
Fix formatting.
59
    except (ImportError, bzrlib.errors.BzrError), e:
0.200.228 by Jelmer Vernooij
Split out map.
60
        from pysqlite2 import dbapi2 as sqlite3
61
        check_pysqlite_version(sqlite3)
62
except:
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
63
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
0.200.228 by Jelmer Vernooij
Split out map.
64
            'module')
65
    raise bzrlib.errors.BzrError("missing sqlite library")
66
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
67
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
68
_mapdbs = threading.local()
69
def mapdbs():
70
    """Get a cache for this thread's db connections."""
71
    try:
72
        return _mapdbs.cache
73
    except AttributeError:
74
        _mapdbs.cache = {}
75
        return _mapdbs.cache
76
77
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
78
class GitShaMap(object):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
79
    """Git<->Bzr revision id mapping database."""
80
81
    def add_entry(self, sha, type, type_data):
82
        """Add a new entry to the database.
83
        """
84
        raise NotImplementedError(self.add_entry)
85
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
86
    def add_entries(self, entries):
87
        """Add multiple new entries to the database.
88
        """
89
        for e in entries:
90
            self.add_entry(*e)
91
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
92
    def lookup_tree(self, fileid, revid):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
93
        """Lookup the SHA of a git tree."""
94
        raise NotImplementedError(self.lookup_tree)
95
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
96
    def lookup_blob(self, fileid, revid):
0.200.477 by Jelmer Vernooij
More tests for sha maps, fix cache misses in tdb.
97
        """Lookup a blob by the fileid it has in a bzr revision."""
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
98
        raise NotImplementedError(self.lookup_blob)
99
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
100
    def lookup_git_sha(self, sha):
101
        """Lookup a Git sha in the database.
102
103
        :param sha: Git object sha
104
        :return: (type, type_data) with type_data:
105
            revision: revid, tree sha
106
        """
107
        raise NotImplementedError(self.lookup_git_sha)
108
109
    def revids(self):
110
        """List the revision ids known."""
111
        raise NotImplementedError(self.revids)
112
0.200.677 by Jelmer Vernooij
Implement TdbCache.missing_revisions().
113
    def missing_revisions(self, revids):
114
        """Return set of all the revisions that are not present."""
115
        present_revids = set(self.revids())
116
        if not isinstance(revids, set):
117
            revids = set(revids)
118
        return revids - present_revids
119
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
120
    def sha1s(self):
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
121
        """List the SHA1s."""
122
        raise NotImplementedError(self.sha1s)
123
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
124
    def start_write_group(self):
125
        """Start writing changes."""
126
127
    def commit_write_group(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
128
        """Commit any pending changes."""
129
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
130
    def abort_write_group(self):
131
        """Abort any pending changes."""
132
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
133
134
class DictGitShaMap(GitShaMap):
135
136
    def __init__(self):
137
        self.dict = {}
138
139
    def add_entry(self, sha, type, type_data):
140
        self.dict[sha] = (type, type_data)
141
142
    def lookup_git_sha(self, sha):
143
        return self.dict[sha]
144
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
145
    def lookup_tree(self, fileid, revid):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
146
        for k, v in self.dict.iteritems():
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
147
            if v == ("tree", (fileid, revid)):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
148
                return k
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
149
        raise KeyError((fileid, revid))
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
150
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
151
    def lookup_blob(self, fileid, revid):
152
        for k, v in self.dict.iteritems():
153
            if v == ("blob", (fileid, revid)):
154
                return k
155
        raise KeyError((fileid, revid))
156
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
157
    def revids(self):
158
        for key, (type, type_data) in self.dict.iteritems():
159
            if type == "commit":
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
160
                yield type_data[0]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
161
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
162
    def sha1s(self):
163
        return self.dict.iterkeys()
164
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
165
166
class SqliteGitShaMap(GitShaMap):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
167
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
168
    def __init__(self, path=None):
169
        self.path = path
170
        if path is None:
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
171
            self.db = sqlite3.connect(":memory:")
172
        else:
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
173
            if not mapdbs().has_key(path):
174
                mapdbs()[path] = sqlite3.connect(path)
0.200.675 by Jelmer Vernooij
Fix formatting.
175
            self.db = mapdbs()[path]
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
176
        self.db.text_factory = str
0.200.230 by Jelmer Vernooij
Implement sha cache.
177
        self.db.executescript("""
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
178
        create table if not exists commits(
179
            sha1 text not null check(length(sha1) == 40),
180
            revid text not null,
181
            tree_sha text not null check(length(tree_sha) == 40)
182
        );
0.200.230 by Jelmer Vernooij
Implement sha cache.
183
        create index if not exists commit_sha1 on commits(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
184
        create unique index if not exists commit_revid on commits(revid);
0.200.691 by Jelmer Vernooij
Add extra constraints in sqlite tables.
185
        create table if not exists blobs(
186
            sha1 text not null check(length(sha1) == 40),
187
            fileid text not null,
188
            revid text not null
189
        );
0.200.230 by Jelmer Vernooij
Implement sha cache.
190
        create index if not exists blobs_sha1 on blobs(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
191
        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.
192
        create table if not exists trees(
193
            sha1 text not null check(length(sha1) == 40),
194
            fileid text not null,
195
            revid text not null
196
        );
0.200.230 by Jelmer Vernooij
Implement sha cache.
197
        create index if not exists trees_sha1 on trees(sha1);
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
198
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
199
""")
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
200
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
201
    @classmethod
202
    def from_repository(cls, repository):
0.200.533 by Jelmer Vernooij
Use user home dir cache when transport for sqlite cache is readonly.
203
        try:
204
            transport = getattr(repository, "_transport", None)
205
            if transport is not None:
206
                return cls(os.path.join(transport.local_abspath("."), "git.db"))
207
        except bzrlib.errors.NotLocalUrl:
208
            pass
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
209
        return cls(os.path.join(get_cache_dir(), "remote.db"))
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
210
0.200.487 by Jelmer Vernooij
Prevent deep recursion if the shamap is out of date.
211
    def lookup_commit(self, revid):
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
212
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
213
        if row is not None:
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
214
            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.
215
        raise KeyError
0.200.231 by Jelmer Vernooij
Partially fix pull.
216
0.200.687 by Jelmer Vernooij
Use start_write_group() / commit_write_group() mechanism when creating git SHA maps.
217
    def commit_write_group(self):
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
218
        self.db.commit()
219
0.200.377 by Jelmer Vernooij
use executemany when inserting sha map entries.
220
    def add_entries(self, entries):
221
        trees = []
222
        blobs = []
223
        for sha, type, type_data in entries:
0.200.410 by Jelmer Vernooij
Fix compatibility with python 2.4.
224
            assert isinstance(type_data[0], str)
225
            assert isinstance(type_data[1], str)
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
226
            entry = (sha, type_data[0], type_data[1])
0.200.377 by Jelmer Vernooij
use executemany when inserting sha map entries.
227
            if type == "tree":
0.200.410 by Jelmer Vernooij
Fix compatibility with python 2.4.
228
                trees.append(entry)
0.200.377 by Jelmer Vernooij
use executemany when inserting sha map entries.
229
            elif type == "blob":
0.200.410 by Jelmer Vernooij
Fix compatibility with python 2.4.
230
                blobs.append(entry)
0.200.377 by Jelmer Vernooij
use executemany when inserting sha map entries.
231
            else:
232
                raise AssertionError
233
        if trees:
234
            self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
235
        if blobs:
236
            self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
237
238
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
239
    def add_entry(self, sha, type, type_data):
240
        """Add a new entry to the database.
241
        """
0.200.231 by Jelmer Vernooij
Partially fix pull.
242
        assert isinstance(type_data, tuple)
0.200.705 by Jelmer Vernooij
Cope with imports.
243
        if sha is None:
244
            return
0.200.231 by Jelmer Vernooij
Partially fix pull.
245
        assert isinstance(sha, str), "type was %r" % sha
0.200.230 by Jelmer Vernooij
Implement sha cache.
246
        if type == "commit":
247
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
248
        elif type in ("blob", "tree"):
249
            self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
0.200.230 by Jelmer Vernooij
Implement sha cache.
250
        else:
251
            raise AssertionError("Unknown type %s" % type)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
252
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
253
    def lookup_tree(self, fileid, revid):
254
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
255
        if row is None:
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
256
            raise KeyError((fileid, revid))
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
257
        return row[0]
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
258
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
259
    def lookup_blob(self, fileid, revid):
0.230.3 by Jelmer Vernooij
Fix blob lookup.
260
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
261
        if row is None:
262
            raise KeyError((fileid, revid))
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
263
        return row[0]
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
264
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
265
    def lookup_git_sha(self, sha):
266
        """Lookup a Git sha in the database.
267
268
        :param sha: Git object sha
269
        :return: (type, type_data) with type_data:
270
            revision: revid, tree sha
271
        """
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
272
        def format(type, row):
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
273
            return (type, (row[0], row[1]))
0.200.230 by Jelmer Vernooij
Implement sha cache.
274
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
275
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
276
            return format("commit", row)
0.200.230 by Jelmer Vernooij
Implement sha cache.
277
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
278
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
279
            return format("blob", row)
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
280
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
0.200.230 by Jelmer Vernooij
Implement sha cache.
281
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
282
            return format("tree", row)
0.200.230 by Jelmer Vernooij
Implement sha cache.
283
        raise KeyError(sha)
284
285
    def revids(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
286
        """List the revision ids known."""
0.200.230 by Jelmer Vernooij
Implement sha cache.
287
        for row in self.db.execute("select revid from commits").fetchall():
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
288
            yield row[0]
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
289
290
    def sha1s(self):
291
        """List the SHA1s."""
292
        for table in ("blobs", "commits", "trees"):
293
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
0.200.688 by Jelmer Vernooij
Use str text factory rather than encoding/decoding each time.
294
                yield row[0]
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
295
296
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
297
TDB_MAP_VERSION = 2
0.239.2 by Jelmer Vernooij
Increase hash size.
298
TDB_HASH_SIZE = 50000
0.200.479 by Jelmer Vernooij
Version tdb sha map.
299
300
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
301
class TdbGitShaMap(GitShaMap):
302
    """SHA Map that uses a TDB database.
303
304
    Entries:
305
0.200.476 by Jelmer Vernooij
Fix Tdb backend, use tdb if possible by default.
306
    "git <sha1>" -> "<type> <type-data1> <type-data2>"
307
    "commit revid" -> "<sha1> <tree-id>"
0.200.477 by Jelmer Vernooij
More tests for sha maps, fix cache misses in tdb.
308
    "tree fileid revid" -> "<sha1>"
309
    "blob fileid revid" -> "<sha1>"
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
310
    """
311
312
    def __init__(self, path=None):
313
        import tdb
314
        self.path = path
315
        if path is None:
316
            self.db = {}
317
        else:
318
            if not mapdbs().has_key(path):
0.200.676 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
319
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
320
                                          os.O_RDWR|os.O_CREAT)
0.200.676 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
321
            self.db = mapdbs()[path]
322
        try:
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
323
            if int(self.db["version"]) != TDB_MAP_VERSION:
324
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
325
                              self.db["version"], TDB_MAP_VERSION)
326
                self.db.clear()
0.200.676 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
327
                self.db["version"] = str(TDB_MAP_VERSION)
328
        except KeyError:
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
329
            self.db["version"] = str(TDB_MAP_VERSION)
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
330
331
    @classmethod
332
    def from_repository(cls, repository):
0.200.482 by Jelmer Vernooij
Use tdb file in the config dir if it's remote.
333
        try:
0.200.500 by Jelmer Vernooij
Cope with repositories that don't have a _transport attribute.
334
            transport = getattr(repository, "_transport", None)
335
            if transport is not None:
336
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
0.200.482 by Jelmer Vernooij
Use tdb file in the config dir if it's remote.
337
        except bzrlib.errors.NotLocalUrl:
0.200.500 by Jelmer Vernooij
Cope with repositories that don't have a _transport attribute.
338
            pass
0.200.534 by Jelmer Vernooij
Use XDG cache directory if the python xdg module is available.
339
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
340
0.200.487 by Jelmer Vernooij
Prevent deep recursion if the shamap is out of date.
341
    def lookup_commit(self, revid):
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
342
        return sha_to_hex(self.db["commit\0" + revid][:20])
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
343
0.200.593 by Jelmer Vernooij
Avoid writing empty trees.
344
    def add_entry(self, hexsha, type, type_data):
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
345
        """Add a new entry to the database.
346
        """
0.200.593 by Jelmer Vernooij
Avoid writing empty trees.
347
        if hexsha is None:
348
            sha = ""
349
        else:
350
            sha = hex_to_sha(hexsha)
351
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
0.200.476 by Jelmer Vernooij
Fix Tdb backend, use tdb if possible by default.
352
        if type == "commit":
0.200.593 by Jelmer Vernooij
Avoid writing empty trees.
353
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
0.200.476 by Jelmer Vernooij
Fix Tdb backend, use tdb if possible by default.
354
        else:
0.200.593 by Jelmer Vernooij
Avoid writing empty trees.
355
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
356
357
    def lookup_tree(self, fileid, revid):
0.200.593 by Jelmer Vernooij
Avoid writing empty trees.
358
        sha = self.db["\0".join(("tree", fileid, revid))]
359
        if sha == "":
360
            return None
361
        else:
362
            return sha_to_hex(sha)
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
363
364
    def lookup_blob(self, fileid, revid):
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
365
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
366
367
    def lookup_git_sha(self, sha):
368
        """Lookup a Git sha in the database.
369
370
        :param sha: Git object sha
371
        :return: (type, type_data) with type_data:
372
            revision: revid, tree sha
373
        """
0.200.564 by Jelmer Vernooij
Accept 'binary' shas.
374
        if len(sha) == 40:
375
            sha = hex_to_sha(sha)
376
        data = self.db["git\0" + sha].split("\0")
0.200.476 by Jelmer Vernooij
Fix Tdb backend, use tdb if possible by default.
377
        return (data[0], (data[1], data[2]))
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
378
0.200.677 by Jelmer Vernooij
Implement TdbCache.missing_revisions().
379
    def missing_revisions(self, revids):
380
        ret = set()
381
        for revid in revids:
382
            if self.db.get("commit\0" + revid) is None:
383
                ret.add(revid)
384
        return ret
385
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
386
    def revids(self):
387
        """List the revision ids known."""
388
        for key in self.db.iterkeys():
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
389
            if key.startswith("commit\0"):
390
                yield key[7:]
0.200.475 by Jelmer Vernooij
Add Tdb database backend.
391
392
    def sha1s(self):
393
        """List the SHA1s."""
394
        for key in self.db.iterkeys():
0.235.1 by Jelmer Vernooij
Store sha map more efficiently.
395
            if key.startswith("git\0"):
396
                yield sha_to_hex(key[4:])