17
17
"""Map from Git sha's to Bazaar objects."""
19
from dulwich.objects import (
23
30
from bzrlib.errors import (
37
from xdg.BaseDirectory import xdg_cache_home
41
return os.path.join(xdg_cache_home, "bazaar", "git")
42
from bzrlib.config import config_dir
43
return os.path.join(config_dir(), "git")
28
46
def check_pysqlite_version(sqlite3):
29
47
"""Check that sqlite library is compatible.
158
177
def from_repository(cls, repository):
159
return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
179
transport = getattr(repository, "_transport", None)
180
if transport is not None:
181
return cls(os.path.join(transport.local_abspath("."), "git.db"))
182
except bzrlib.errors.NotLocalUrl:
184
return cls(os.path.join(get_cache_dir(), "remote.db"))
161
def _parent_lookup(self, revid):
186
def lookup_commit(self, revid):
162
187
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
163
188
if row is not None:
164
189
return row[0].encode("utf-8")
241
266
for table in ("blobs", "commits", "trees"):
242
267
for row in self.db.execute("select sha1 from %s" % table).fetchall():
243
268
yield row[0].encode("utf-8")
272
TDB_HASH_SIZE = 10000
275
class TdbGitShaMap(GitShaMap):
276
"""SHA Map that uses a TDB database.
280
"git <sha1>" -> "<type> <type-data1> <type-data2>"
281
"commit revid" -> "<sha1> <tree-id>"
282
"tree fileid revid" -> "<sha1>"
283
"blob fileid revid" -> "<sha1>"
286
def __init__(self, path=None):
292
if not mapdbs().has_key(path):
293
mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
294
os.O_RDWR|os.O_CREAT)
295
self.db = mapdbs()[path]
296
if not "version" in self.db:
297
self.db["version"] = str(TDB_MAP_VERSION)
299
if int(self.db["version"]) != TDB_MAP_VERSION:
300
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
301
self.db["version"], TDB_MAP_VERSION)
303
self.db["version"] = str(TDB_MAP_VERSION)
306
def from_repository(cls, repository):
308
transport = getattr(repository, "_transport", None)
309
if transport is not None:
310
return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
311
except bzrlib.errors.NotLocalUrl:
313
return cls(os.path.join(get_cache_dir(), "remote.tdb"))
315
def lookup_commit(self, revid):
316
return sha_to_hex(self.db["commit\0" + revid][:20])
321
def add_entry(self, sha, type, type_data):
322
"""Add a new entry to the database.
324
self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
326
self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
328
self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
330
def lookup_tree(self, fileid, revid):
331
return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
333
def lookup_blob(self, fileid, revid):
334
return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
336
def lookup_git_sha(self, sha):
337
"""Lookup a Git sha in the database.
339
:param sha: Git object sha
340
:return: (type, type_data) with type_data:
341
revision: revid, tree sha
343
data = self.db["git\0" + hex_to_sha(sha)].split("\0")
344
return (data[0], (data[1], data[2]))
347
"""List the revision ids known."""
348
for key in self.db.iterkeys():
349
if key.startswith("commit\0"):
353
"""List the SHA1s."""
354
for key in self.db.iterkeys():
355
if key.startswith("git\0"):
356
yield sha_to_hex(key[4:])