17
17
"""Map from Git sha's to Bazaar objects."""
21
19
from bzrlib.errors import NoSuchRevision
26
def check_pysqlite_version(sqlite3):
27
"""Check that sqlite library is compatible.
30
if (sqlite3.sqlite_version_info[0] < 3 or
31
(sqlite3.sqlite_version_info[0] == 3 and
32
sqlite3.sqlite_version_info[1] < 3)):
33
warning('Needs at least sqlite 3.3.x')
34
raise bzrlib.errors.BzrError("incompatible sqlite library")
39
check_pysqlite_version(sqlite3)
40
except (ImportError, bzrlib.errors.BzrError), e:
41
from pysqlite2 import dbapi2 as sqlite3
42
check_pysqlite_version(sqlite3)
44
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
46
raise bzrlib.errors.BzrError("missing sqlite library")
22
class GitObjectConverter(object):
24
def __init__(self, repository, mapping=None):
25
self.repository = repository
27
self.mapping = self.repository.get_mapping()
29
self.mapping = mapping
31
def __getitem__(self, sha):
33
revid = self.mapping.revision_id_foreign_to_bzr(sha)
35
rev = self.repository.get_revision(revid)
36
except NoSuchRevision:
39
return reconstruct_git_commit(rev)
41
# TODO: Yeah, this won't scale, but the only alternative is a
43
for (fileid, revision) in self.repository.texts.keys():
44
blob = self._get_blob(
49
48
class GitShaMap(object):
51
50
def __init__(self, transport):
52
51
self.transport = transport
53
self.db = sqlite3.connect(
54
os.path.join(self.transport.local_abspath("."), "git.db"))
55
self.db.executescript("""
56
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
create index if not exists commit_sha1 on commits(sha1);
58
create table if not exists blobs(sha1 text, fileid text, revid text);
59
create index if not exists blobs_sha1 on blobs(sha1);
60
create table if not exists trees(sha1 text, fileid text, revid text);
61
create index if not exists trees_sha1 on trees(sha1);
64
def _parent_lookup(self, revid):
65
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
67
53
def add_entry(self, sha, type, type_data):
68
54
"""Add a new entry to the database.
70
assert isinstance(type_data, tuple)
71
assert isinstance(sha, str), "type was %r" % sha
73
self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
75
self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
77
self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
79
raise AssertionError("Unknown type %s" % type)
56
raise NotImplementedError(self.add_entry)
81
58
def lookup_git_sha(self, sha):
82
59
"""Lookup a Git sha in the database.
85
62
:return: (type, type_data) with type_data:
86
63
revision: revid, tree sha
88
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
90
return ("commit", row)
91
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
94
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
100
for row in self.db.execute("select revid from commits").fetchall():
65
raise NotImplementedError(self.lookup_git_sha)
68
class MappedGitObjectConverter(GitObjectConverter):
70
def __init__(self, repository):
71
self.repository = repository
72
self._idmap = GitShaMap(self.repository._transport)
74
def _update_sha_map(self):
76
raise NotImplementedError(self._update_sha_map)
78
def __getitem__(self, sha):
79
# See if sha is in map
81
(type, type_data) = self._idmap.lookup_git_sha(sha)
83
# if not, see if there are any unconverted revisions and add them
84
# to the map, search for sha in map again
85
self._update_sha_map()
86
(type, type_data) = self._idmap.lookup_git_sha(sha)
87
# convert object to git object
88
if type == "revision":
89
return self._get_commit(*type_data)
91
raise AssertionError("Unknown object type '%s'" % type)