17
17
"""Map from Git sha's to Bazaar objects."""
19
from dulwich.objects import (
23
from bzrlib.errors import (
34
from xdg.BaseDirectory import xdg_cache_home
36
from bzrlib.config import config_dir
37
ret = os.path.join(config_dir(), "git")
39
ret = os.path.join(xdg_cache_home, "bazaar", "git")
40
if not os.path.isdir(ret):
28
45
def check_pysqlite_version(sqlite3):
29
46
"""Check that sqlite library is compatible.
32
49
if (sqlite3.sqlite_version_info[0] < 3 or
33
50
(sqlite3.sqlite_version_info[0] == 3 and
34
51
sqlite3.sqlite_version_info[1] < 3)):
35
warning('Needs at least sqlite 3.3.x')
52
trace.warning('Needs at least sqlite 3.3.x')
36
53
raise bzrlib.errors.BzrError("incompatible sqlite library")
43
60
from pysqlite2 import dbapi2 as sqlite3
44
61
check_pysqlite_version(sqlite3)
46
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
63
trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
48
65
raise bzrlib.errors.BzrError("missing sqlite library")
159
176
def from_repository(cls, repository):
160
return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
178
transport = getattr(repository, "_transport", None)
179
if transport is not None:
180
return cls(os.path.join(transport.local_abspath("."), "git.db"))
181
except bzrlib.errors.NotLocalUrl:
183
return cls(os.path.join(get_cache_dir(), "remote.db"))
162
185
def lookup_commit(self, revid):
163
186
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
267
291
if not mapdbs().has_key(path):
268
mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT,
292
mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
269
293
os.O_RDWR|os.O_CREAT)
270
294
self.db = mapdbs()[path]
271
295
if not "version" in self.db:
272
296
self.db["version"] = str(TDB_MAP_VERSION)
274
assert int(self.db["version"]) == TDB_MAP_VERSION
298
if int(self.db["version"]) != TDB_MAP_VERSION:
299
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
300
self.db["version"], TDB_MAP_VERSION)
302
self.db["version"] = str(TDB_MAP_VERSION)
277
305
def from_repository(cls, repository):
281
309
return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
282
310
except bzrlib.errors.NotLocalUrl:
284
from bzrlib.config import config_dir
285
return cls(os.path.join(config_dir(), "remote-git.tdb"))
312
return cls(os.path.join(get_cache_dir(), "remote.tdb"))
287
314
def lookup_commit(self, revid):
288
return self.db["commit %s" % revid].split(" ")[0]
315
return sha_to_hex(self.db["commit\0" + revid][:20])
290
317
def commit(self):
293
def add_entry(self, sha, type, type_data):
320
def add_entry(self, hexsha, type, type_data):
294
321
"""Add a new entry to the database.
296
self.db["git %s" % sha] = "%s %s %s" % (type, type_data[0], type_data[1])
326
sha = hex_to_sha(hexsha)
327
self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
297
328
if type == "commit":
298
self.db["commit %s" % type_data[0]] = "%s %s" % (sha, type_data[1])
329
self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
300
self.db["%s %s %s" % (type, type_data[0], type_data[1])] = sha
331
self.db["\0".join((type, type_data[0], type_data[1]))] = sha
302
333
def lookup_tree(self, fileid, revid):
303
return self.db["tree %s %s" % (fileid, revid)]
334
sha = self.db["\0".join(("tree", fileid, revid))]
338
return sha_to_hex(sha)
305
340
def lookup_blob(self, fileid, revid):
306
return self.db["blob %s %s" % (fileid, revid)]
341
return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
308
343
def lookup_git_sha(self, sha):
309
344
"""Lookup a Git sha in the database.
312
347
:return: (type, type_data) with type_data:
313
348
revision: revid, tree sha
315
data = self.db["git %s" % sha].split(" ")
351
sha = hex_to_sha(sha)
352
data = self.db["git\0" + sha].split("\0")
316
353
return (data[0], (data[1], data[2]))
318
355
def revids(self):
319
356
"""List the revision ids known."""
320
357
for key in self.db.iterkeys():
321
if key.startswith("commit "):
322
yield key.split(" ")[1]
358
if key.startswith("commit\0"):
325
362
"""List the SHA1s."""
326
363
for key in self.db.iterkeys():
327
if key.startswith("git "):
328
yield key.split(" ")[1]
364
if key.startswith("git\0"):
365
yield sha_to_hex(key[4:])