46
68
raise bzrlib.errors.BzrError("missing sqlite library")
71
_mapdbs = threading.local()
73
"""Get a cache for this thread's db connections."""
76
except AttributeError:
49
81
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"))
82
"""Git<->Bzr revision id mapping database."""
84
def add_entry(self, sha, type, type_data):
85
"""Add a new entry to the database.
87
raise NotImplementedError(self.add_entry)
89
def add_entries(self, entries):
90
"""Add multiple new entries to the database.
95
def lookup_tree(self, fileid, revid):
96
"""Lookup the SHA of a git tree."""
97
raise NotImplementedError(self.lookup_tree)
99
def lookup_blob(self, fileid, revid):
100
"""Lookup a blob by the fileid it has in a bzr revision."""
101
raise NotImplementedError(self.lookup_blob)
103
def lookup_git_sha(self, sha):
104
"""Lookup a Git sha in the database.
106
:param sha: Git object sha
107
:return: (type, type_data) with type_data:
108
revision: revid, tree sha
110
raise NotImplementedError(self.lookup_git_sha)
113
"""List the revision ids known."""
114
raise NotImplementedError(self.revids)
117
"""List the SHA1s."""
118
raise NotImplementedError(self.sha1s)
121
"""Commit any pending changes."""
124
class DictGitShaMap(GitShaMap):
129
def add_entry(self, sha, type, type_data):
130
self.dict[sha] = (type, type_data)
132
def lookup_git_sha(self, sha):
133
return self.dict[sha]
135
def lookup_tree(self, fileid, revid):
136
for k, v in self.dict.iteritems():
137
if v == ("tree", (fileid, revid)):
139
raise KeyError((fileid, revid))
141
def lookup_blob(self, fileid, revid):
142
for k, v in self.dict.iteritems():
143
if v == ("blob", (fileid, revid)):
145
raise KeyError((fileid, revid))
148
for key, (type, type_data) in self.dict.iteritems():
153
return self.dict.iterkeys()
156
class SqliteGitShaMap(GitShaMap):
158
def __init__(self, path=None):
161
self.db = sqlite3.connect(":memory:")
163
if not mapdbs().has_key(path):
164
mapdbs()[path] = sqlite3.connect(path)
165
self.db = mapdbs()[path]
55
166
self.db.executescript("""
56
167
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
168
create index if not exists commit_sha1 on commits(sha1);
169
create unique index if not exists commit_revid on commits(revid);
58
170
create table if not exists blobs(sha1 text, fileid text, revid text);
59
171
create index if not exists blobs_sha1 on blobs(sha1);
172
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
60
173
create table if not exists trees(sha1 text, fileid text, revid text);
61
174
create index if not exists trees_sha1 on trees(sha1);
175
create unique index if not exists trees_fileid_revid on trees(fileid, revid);
64
def _parent_lookup(self, revid):
65
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
179
def from_repository(cls, repository):
181
transport = getattr(repository, "_transport", None)
182
if transport is not None:
183
return cls(os.path.join(transport.local_abspath("."), "git.db"))
184
except bzrlib.errors.NotLocalUrl:
186
return cls(os.path.join(get_cache_dir(), "remote.db"))
188
def lookup_commit(self, revid):
189
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
191
return row[0].encode("utf-8")
197
def add_entries(self, entries):
200
for sha, type, type_data in entries:
201
assert isinstance(type_data[0], str)
202
assert isinstance(type_data[1], str)
203
entry = (sha.decode("utf-8"), type_data[0].decode("utf-8"),
204
type_data[1].decode("utf-8"))
212
self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
214
self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
67
217
def add_entry(self, sha, type, type_data):
68
218
"""Add a new entry to the database.
71
221
assert isinstance(sha, str), "type was %r" % sha
72
222
if type == "commit":
73
223
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]))
224
elif type in ("blob", "tree"):
225
self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
79
227
raise AssertionError("Unknown type %s" % type)
229
def lookup_tree(self, fileid, revid):
230
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
232
raise KeyError((fileid, revid))
233
return row[0].encode("utf-8")
235
def lookup_blob(self, fileid, revid):
236
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
238
raise KeyError((fileid, revid))
239
return row[0].encode("utf-8")
81
241
def lookup_git_sha(self, sha):
82
242
"""Lookup a Git sha in the database.
85
245
:return: (type, type_data) with type_data:
86
246
revision: revid, tree sha
248
def format(type, row):
249
return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
88
250
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
89
251
if row is not None:
90
return ("commit", row)
252
return format("commit", row)
91
253
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
254
if row is not None:
255
return format("blob", row)
94
256
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
95
257
if row is not None:
258
return format("tree", row)
97
259
raise KeyError(sha)
262
"""List the revision ids known."""
100
263
for row in self.db.execute("select revid from commits").fetchall():
264
yield row[0].encode("utf-8")
267
"""List the SHA1s."""
268
for table in ("blobs", "commits", "trees"):
269
for row in self.db.execute("select sha1 from %s" % table).fetchall():
270
yield row[0].encode("utf-8")
274
TDB_HASH_SIZE = 10000
277
class TdbGitShaMap(GitShaMap):
278
"""SHA Map that uses a TDB database.
282
"git <sha1>" -> "<type> <type-data1> <type-data2>"
283
"commit revid" -> "<sha1> <tree-id>"
284
"tree fileid revid" -> "<sha1>"
285
"blob fileid revid" -> "<sha1>"
288
def __init__(self, path=None):
294
if not mapdbs().has_key(path):
295
mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
296
os.O_RDWR|os.O_CREAT)
297
self.db = mapdbs()[path]
298
if not "version" in self.db:
299
self.db["version"] = str(TDB_MAP_VERSION)
301
if int(self.db["version"]) != TDB_MAP_VERSION:
302
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
303
self.db["version"], TDB_MAP_VERSION)
305
self.db["version"] = str(TDB_MAP_VERSION)
308
def from_repository(cls, repository):
310
transport = getattr(repository, "_transport", None)
311
if transport is not None:
312
return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
313
except bzrlib.errors.NotLocalUrl:
315
return cls(os.path.join(get_cache_dir(), "remote.tdb"))
317
def lookup_commit(self, revid):
318
return sha_to_hex(self.db["commit\0" + revid][:20])
323
def add_entry(self, sha, type, type_data):
324
"""Add a new entry to the database.
326
self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
328
self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
330
self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
332
def lookup_tree(self, fileid, revid):
333
return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
335
def lookup_blob(self, fileid, revid):
336
return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
338
def lookup_git_sha(self, sha):
339
"""Lookup a Git sha in the database.
341
:param sha: Git object sha
342
:return: (type, type_data) with type_data:
343
revision: revid, tree sha
346
sha = hex_to_sha(sha)
347
data = self.db["git\0" + sha].split("\0")
348
return (data[0], (data[1], data[2]))
351
"""List the revision ids known."""
352
for key in self.db.iterkeys():
353
if key.startswith("commit\0"):
357
"""List the SHA1s."""
358
for key in self.db.iterkeys():
359
if key.startswith("git\0"):
360
yield sha_to_hex(key[4:])