49
50
class GitShaMap(object):
51
def __init__(self, transport):
51
"""Git<->Bzr revision id mapping database."""
53
def add_entry(self, sha, type, type_data):
54
"""Add a new entry to the database.
56
raise NotImplementedError(self.add_entry)
58
def lookup_tree(self, path, revid):
59
"""Lookup the SHA of a git tree."""
60
raise NotImplementedError(self.lookup_tree)
62
def lookup_git_sha(self, sha):
63
"""Lookup a Git sha in the database.
65
:param sha: Git object sha
66
:return: (type, type_data) with type_data:
67
revision: revid, tree sha
69
raise NotImplementedError(self.lookup_git_sha)
72
"""List the revision ids known."""
73
raise NotImplementedError(self.revids)
76
"""Commit any pending changes."""
79
class DictGitShaMap(GitShaMap):
84
def add_entry(self, sha, type, type_data):
85
self.dict[sha] = (type, type_data)
87
def lookup_git_sha(self, sha):
90
def lookup_tree(self, path, revid):
91
for k, v in self.dict.iteritems():
92
if v == ("tree", (path, revid)):
94
raise KeyError((path, revid))
97
for key, (type, type_data) in self.dict.iteritems():
102
class SqliteGitShaMap(GitShaMap):
104
def __init__(self, transport=None):
52
105
self.transport = transport
53
self.db = sqlite3.connect(
54
os.path.join(self.transport.local_abspath("."), "git.db"))
106
if transport is None:
107
self.db = sqlite3.connect(":memory:")
109
self.db = sqlite3.connect(
110
os.path.join(self.transport.local_abspath("."), "git.db"))
55
111
self.db.executescript("""
56
112
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
113
create index if not exists commit_sha1 on commits(sha1);
114
create unique index if not exists commit_revid on commits(revid);
58
115
create table if not exists blobs(sha1 text, fileid text, revid text);
59
116
create index if not exists blobs_sha1 on blobs(sha1);
60
create table if not exists trees(sha1 text, fileid text, revid text);
117
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
118
create table if not exists trees(sha1 text, path text, revid text);
61
119
create index if not exists trees_sha1 on trees(sha1);
120
create unique index if not exists trees_path_revid on trees(path, revid);
64
123
def _parent_lookup(self, revid):
65
124
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
67
129
def add_entry(self, sha, type, type_data):
68
130
"""Add a new entry to the database.
74
136
elif type == "blob":
75
137
self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
76
138
elif type == "tree":
77
self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
139
self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
79
141
raise AssertionError("Unknown type %s" % type)
143
def lookup_tree(self, path, revid):
144
row = self.db.execute("select sha1 from trees where path = ? and revid = ?", (path,revid)).fetchone()
146
raise KeyError((path, revid))
81
149
def lookup_git_sha(self, sha):
82
150
"""Lookup a Git sha in the database.