49
49
class GitShaMap(object):
51
def __init__(self, transport):
50
"""Git<->Bzr revision id mapping database."""
52
def add_entry(self, sha, type, type_data):
53
"""Add a new entry to the database.
55
raise NotImplementedError(self.add_entry)
57
def lookup_git_sha(self, sha):
58
"""Lookup a Git sha in the database.
60
:param sha: Git object sha
61
:return: (type, type_data) with type_data:
62
revision: revid, tree sha
64
raise NotImplementedError(self.lookup_git_sha)
67
"""List the revision ids known."""
68
raise NotImplementedError(self.revids)
71
"""Commit any pending changes."""
74
class DictGitShaMap(GitShaMap):
79
def add_entry(self, sha, type, type_data):
80
self.dict[sha] = (type, type_data)
82
def lookup_git_sha(self, sha):
86
for key, (type, type_data) in self.dict.iteritems():
91
class SqliteGitShaMap(GitShaMap):
93
def __init__(self, transport=None):
52
94
self.transport = transport
53
self.db = sqlite3.connect(
54
os.path.join(self.transport.local_abspath("."), "git.db"))
96
self.db = sqlite3.connect(":memory:")
98
self.db = sqlite3.connect(
99
os.path.join(self.transport.local_abspath("."), "git.db"))
55
100
self.db.executescript("""
56
101
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
102
create index if not exists commit_sha1 on commits(sha1);
58
103
create table if not exists blobs(sha1 text, fileid text, revid text);
59
104
create index if not exists blobs_sha1 on blobs(sha1);
60
create table if not exists trees(sha1 text, fileid text, revid text);
105
create table if not exists trees(sha1 text, path text, revid text);
61
106
create index if not exists trees_sha1 on trees(sha1);
64
109
def _parent_lookup(self, revid):
65
110
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
67
115
def add_entry(self, sha, type, type_data):
68
116
"""Add a new entry to the database.
74
122
elif type == "blob":
75
123
self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
76
124
elif type == "tree":
77
self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
125
self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
79
127
raise AssertionError("Unknown type %s" % type)
91
139
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
140
if row is not None:
93
141
return ("blob", row)
94
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
142
row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
95
143
if row is not None:
96
144
return ("tree", row)
97
145
raise KeyError(sha)
148
"""List the revision ids known."""
100
149
for row in self.db.execute("select revid from commits").fetchall():