46
52
raise bzrlib.errors.BzrError("missing sqlite library")
55
_mapdbs = threading.local()
57
"""Get a cache for this thread's db connections."""
60
except AttributeError:
49
65
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"))
66
"""Git<->Bzr revision id mapping database."""
68
def add_entry(self, sha, type, type_data):
69
"""Add a new entry to the database.
71
raise NotImplementedError(self.add_entry)
73
def add_entries(self, entries):
74
"""Add multiple new entries to the database.
79
def lookup_tree(self, fileid, revid):
80
"""Lookup the SHA of a git tree."""
81
raise NotImplementedError(self.lookup_tree)
83
def lookup_blob(self, fileid, revid):
84
"""Lookup a blob by the fileid it has in a bzr revision."""
85
raise NotImplementedError(self.lookup_blob)
87
def lookup_git_sha(self, sha):
88
"""Lookup a Git sha in the database.
90
:param sha: Git object sha
91
:return: (type, type_data) with type_data:
92
revision: revid, tree sha
94
raise NotImplementedError(self.lookup_git_sha)
97
"""List the revision ids known."""
98
raise NotImplementedError(self.revids)
101
"""List the SHA1s."""
102
raise NotImplementedError(self.sha1s)
105
"""Commit any pending changes."""
108
class DictGitShaMap(GitShaMap):
113
def add_entry(self, sha, type, type_data):
114
self.dict[sha] = (type, type_data)
116
def lookup_git_sha(self, sha):
117
return self.dict[sha]
119
def lookup_tree(self, fileid, revid):
120
for k, v in self.dict.iteritems():
121
if v == ("tree", (fileid, revid)):
123
raise KeyError((fileid, revid))
125
def lookup_blob(self, fileid, revid):
126
for k, v in self.dict.iteritems():
127
if v == ("blob", (fileid, revid)):
129
raise KeyError((fileid, revid))
132
for key, (type, type_data) in self.dict.iteritems():
137
return self.dict.iterkeys()
140
class SqliteGitShaMap(GitShaMap):
142
def __init__(self, path=None):
145
self.db = sqlite3.connect(":memory:")
147
if not mapdbs().has_key(path):
148
mapdbs()[path] = sqlite3.connect(path)
149
self.db = mapdbs()[path]
55
150
self.db.executescript("""
56
151
create table if not exists commits(sha1 text, revid text, tree_sha text);
57
152
create index if not exists commit_sha1 on commits(sha1);
153
create unique index if not exists commit_revid on commits(revid);
58
154
create table if not exists blobs(sha1 text, fileid text, revid text);
59
155
create index if not exists blobs_sha1 on blobs(sha1);
156
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
60
157
create table if not exists trees(sha1 text, fileid text, revid text);
61
158
create index if not exists trees_sha1 on trees(sha1);
159
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")
163
def from_repository(cls, repository):
164
return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
166
def lookup_commit(self, revid):
167
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
169
return row[0].encode("utf-8")
175
def add_entries(self, entries):
178
for sha, type, type_data in entries:
179
assert isinstance(type_data[0], str)
180
assert isinstance(type_data[1], str)
181
entry = (sha.decode("utf-8"), type_data[0].decode("utf-8"),
182
type_data[1].decode("utf-8"))
190
self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
192
self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
67
195
def add_entry(self, sha, type, type_data):
68
196
"""Add a new entry to the database.
71
199
assert isinstance(sha, str), "type was %r" % sha
72
200
if type == "commit":
73
201
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]))
202
elif type in ("blob", "tree"):
203
self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
79
205
raise AssertionError("Unknown type %s" % type)
207
def lookup_tree(self, fileid, revid):
208
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
210
raise KeyError((fileid, revid))
211
return row[0].encode("utf-8")
213
def lookup_blob(self, fileid, revid):
214
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
216
raise KeyError((fileid, revid))
217
return row[0].encode("utf-8")
81
219
def lookup_git_sha(self, sha):
82
220
"""Lookup a Git sha in the database.
85
223
:return: (type, type_data) with type_data:
86
224
revision: revid, tree sha
226
def format(type, row):
227
return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
88
228
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
89
229
if row is not None:
90
return ("commit", row)
230
return format("commit", row)
91
231
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
232
if row is not None:
233
return format("blob", row)
94
234
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
95
235
if row is not None:
236
return format("tree", row)
97
237
raise KeyError(sha)
240
"""List the revision ids known."""
100
241
for row in self.db.execute("select revid from commits").fetchall():
242
yield row[0].encode("utf-8")
245
"""List the SHA1s."""
246
for table in ("blobs", "commits", "trees"):
247
for row in self.db.execute("select sha1 from %s" % table).fetchall():
248
yield row[0].encode("utf-8")
254
class TdbGitShaMap(GitShaMap):
255
"""SHA Map that uses a TDB database.
259
"git <sha1>" -> "<type> <type-data1> <type-data2>"
260
"commit revid" -> "<sha1> <tree-id>"
261
"tree fileid revid" -> "<sha1>"
262
"blob fileid revid" -> "<sha1>"
265
def __init__(self, path=None):
271
if not mapdbs().has_key(path):
272
mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT,
273
os.O_RDWR|os.O_CREAT)
274
self.db = mapdbs()[path]
275
if not "version" in self.db:
276
self.db["version"] = str(TDB_MAP_VERSION)
278
if int(self.db["version"]) != TDB_MAP_VERSION:
279
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
280
self.db["version"], TDB_MAP_VERSION)
282
self.db["version"] = str(TDB_MAP_VERSION)
285
def from_repository(cls, repository):
287
transport = getattr(repository, "_transport", None)
288
if transport is not None:
289
return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
290
except bzrlib.errors.NotLocalUrl:
292
from bzrlib.config import config_dir
293
return cls(os.path.join(config_dir(), "remote-git.tdb"))
295
def lookup_commit(self, revid):
296
return sha_to_hex(self.db["commit\0" + revid][:20])
301
def add_entry(self, sha, type, type_data):
302
"""Add a new entry to the database.
304
self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
306
self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
308
self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
310
def lookup_tree(self, fileid, revid):
311
return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
313
def lookup_blob(self, fileid, revid):
314
return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
316
def lookup_git_sha(self, sha):
317
"""Lookup a Git sha in the database.
319
:param sha: Git object sha
320
:return: (type, type_data) with type_data:
321
revision: revid, tree sha
323
data = self.db["git\0" + hex_to_sha(sha)].split("\0")
324
return (data[0], (data[1], data[2]))
327
"""List the revision ids known."""
328
for key in self.db.iterkeys():
329
if key.startswith("commit\0"):
333
"""List the SHA1s."""
334
for key in self.db.iterkeys():
335
if key.startswith("git\0"):
336
yield sha_to_hex(key[4:])