46
48
raise bzrlib.errors.BzrError("missing sqlite library")
51
_mapdbs = threading.local()
53
"""Get a cache for this thread's db connections."""
56
except AttributeError:
49
61
class GitShaMap(object):
51
def __init__(self, transport):
52
self.transport = transport
53
self.db = sqlite3.connect(
54
os.path.join(self.transport.local_abspath("."), "git.db"))
62
"""Git<->Bzr revision id mapping database."""
64
def add_entry(self, sha, type, type_data):
65
"""Add a new entry to the database.
67
raise NotImplementedError(self.add_entry)
69
def add_entries(self, entries):
70
"""Add multiple new entries to the database.
75
def lookup_tree(self, fileid, revid):
76
"""Lookup the SHA of a git tree."""
77
raise NotImplementedError(self.lookup_tree)
79
def lookup_blob(self, fileid, revid):
80
raise NotImplementedError(self.lookup_blob)
82
def lookup_git_sha(self, sha):
83
"""Lookup a Git sha in the database.
85
:param sha: Git object sha
86
:return: (type, type_data) with type_data:
87
revision: revid, tree sha
89
raise NotImplementedError(self.lookup_git_sha)
92
"""List the revision ids known."""
93
raise NotImplementedError(self.revids)
97
raise NotImplementedError(self.sha1s)
100
"""Commit any pending changes."""
103
class DictGitShaMap(GitShaMap):
108
def add_entry(self, sha, type, type_data):
109
self.dict[sha] = (type, type_data)
111
def lookup_git_sha(self, sha):
112
return self.dict[sha]
114
def lookup_tree(self, fileid, revid):
115
for k, v in self.dict.iteritems():
116
if v == ("tree", (fileid, revid)):
118
raise KeyError((fileid, revid))
120
def lookup_blob(self, fileid, revid):
121
for k, v in self.dict.iteritems():
122
if v == ("blob", (fileid, revid)):
124
raise KeyError((fileid, revid))
127
for key, (type, type_data) in self.dict.iteritems():
132
return self.dict.iterkeys()
135
class SqliteGitShaMap(GitShaMap):
137
def __init__(self, path=None):
140
self.db = sqlite3.connect(":memory:")
142
if not mapdbs().has_key(path):
143
mapdbs()[path] = sqlite3.connect(path)
144
self.db = mapdbs()[path]
55
145
self.db.executescript("""
56
146
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
147
create index if not exists commit_sha1 on commits(sha1);
148
create unique index if not exists commit_revid on commits(revid);
58
149
create table if not exists blobs(sha1 text, fileid text, revid text);
59
150
create index if not exists blobs_sha1 on blobs(sha1);
151
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
60
152
create table if not exists trees(sha1 text, fileid text, revid text);
61
153
create index if not exists trees_sha1 on trees(sha1);
154
create unique index if not exists trees_fileid_revid on trees(fileid, revid);
158
def from_repository(cls, repository):
159
return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
64
161
def _parent_lookup(self, revid):
65
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
162
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
164
return row[0].encode("utf-8")
170
def add_entries(self, entries):
173
for sha, type, type_data in entries:
174
assert isinstance(type_data[0], str)
175
assert isinstance(type_data[1], str)
176
entry = (sha.decode("utf-8"), type_data[0].decode("utf-8"),
177
type_data[1].decode("utf-8"))
185
self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
187
self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
67
190
def add_entry(self, sha, type, type_data):
68
191
"""Add a new entry to the database.
71
194
assert isinstance(sha, str), "type was %r" % sha
72
195
if type == "commit":
73
196
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]))
197
elif type in ("blob", "tree"):
198
self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
79
200
raise AssertionError("Unknown type %s" % type)
202
def lookup_tree(self, fileid, revid):
203
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
205
raise KeyError((fileid, revid))
206
return row[0].encode("utf-8")
208
def lookup_blob(self, fileid, revid):
209
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
211
raise KeyError((fileid, revid))
212
return row[0].encode("utf-8")
81
214
def lookup_git_sha(self, sha):
82
215
"""Lookup a Git sha in the database.
85
218
:return: (type, type_data) with type_data:
86
219
revision: revid, tree sha
221
def format(type, row):
222
return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
88
223
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
89
224
if row is not None:
90
return ("commit", row)
225
return format("commit", row)
91
226
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
227
if row is not None:
228
return format("blob", row)
94
229
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
95
230
if row is not None:
231
return format("tree", row)
97
232
raise KeyError(sha)
235
"""List the revision ids known."""
100
236
for row in self.db.execute("select revid from commits").fetchall():
237
yield row[0].encode("utf-8")
240
"""List the SHA1s."""
241
for table in ("blobs", "commits", "trees"):
242
for row in self.db.execute("select sha1 from %s" % table).fetchall():
243
yield row[0].encode("utf-8")
246
class TdbGitShaMap(GitShaMap):
247
"""SHA Map that uses a TDB database.
251
commit/sha1 -> revid tree-id
252
tree/sha1 -> revid file-id
253
blob/sha1 -> revid file-id
256
def __init__(self, path=None):
262
if not mapdbs().has_key(path):
263
mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT,
264
os.O_RDWR|os.O_CREAT)
265
self.db = mapdbs()[path]
268
def from_repository(cls, repository):
269
return cls(os.path.join(repository._transport.local_abspath("."), "git.tdb"))
271
def _parent_lookup(self, revid):
272
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
274
return row[0].encode("utf-8")
280
def add_entry(self, sha, type, type_data):
281
"""Add a new entry to the database.
283
self.db["%s/%s" % (type, sha)] = "%s %s" % type_data
285
def lookup_tree(self, fileid, revid):
286
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
288
raise KeyError((fileid, revid))
289
return row[0].encode("utf-8")
291
def lookup_blob(self, fileid, revid):
292
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
294
raise KeyError((fileid, revid))
295
return row[0].encode("utf-8")
297
def lookup_git_sha(self, sha):
298
"""Lookup a Git sha in the database.
300
:param sha: Git object sha
301
:return: (type, type_data) with type_data:
302
revision: revid, tree sha
304
for type in ("commit", "blob", "tree"):
306
return (type, tuple(self.db["%s/%s" % (type, sha)].split(" ")))
312
"""List the revision ids known."""
313
for key in self.db.iterkeys():
314
if key.startswith("commit/"):
315
yield self.db[key].split(" ")[0]
318
"""List the SHA1s."""
319
for key in self.db.iterkeys():
320
if (key.startswith("tree/") or key.startswith("blob/") or
321
key.startswith("commit/")):
322
yield key.split("/")[1]