17
17
"""Map from Git sha's to Bazaar objects."""
19
from dulwich.objects import (
21
from bzrlib.errors import NoSuchRevision
35
from xdg.BaseDirectory import xdg_cache_home
37
from bzrlib.config import config_dir
38
ret = os.path.join(config_dir(), "git")
40
ret = os.path.join(xdg_cache_home, "bazaar", "git")
41
if not os.path.isdir(ret):
26
46
def check_pysqlite_version(sqlite3):
27
47
"""Check that sqlite library is compatible.
30
if (sqlite3.sqlite_version_info[0] < 3 or
31
(sqlite3.sqlite_version_info[0] == 3 and
50
if (sqlite3.sqlite_version_info[0] < 3 or
51
(sqlite3.sqlite_version_info[0] == 3 and
32
52
sqlite3.sqlite_version_info[1] < 3)):
33
warning('Needs at least sqlite 3.3.x')
53
trace.warning('Needs at least sqlite 3.3.x')
34
54
raise bzrlib.errors.BzrError("incompatible sqlite library")
39
59
check_pysqlite_version(sqlite3)
40
except (ImportError, bzrlib.errors.BzrError), e:
60
except (ImportError, bzrlib.errors.BzrError), e:
41
61
from pysqlite2 import dbapi2 as sqlite3
42
62
check_pysqlite_version(sqlite3)
44
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
64
trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
46
66
raise bzrlib.errors.BzrError("missing sqlite library")
69
_mapdbs = threading.local()
71
"""Get a cache for this thread's db connections."""
74
except AttributeError:
79
class InventorySHAMap(object):
80
"""Maps inventory file ids to Git SHAs."""
82
def lookup_blob(self, file_id, revision_hint=None):
83
"""Retrieve a Git blob SHA by file id.
85
:param file_id: File id of the file/symlink
86
:param revision_hint: Optional revision in which the file was last
89
raise NotImplementedError(self.lookup_blob)
91
def lookup_tree(self, file_id):
92
"""Retrieve a Git tree SHA by file id.
94
raise NotImplementedError(self.lookup_tree)
49
97
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"))
98
"""Git<->Bzr revision id mapping database."""
100
def add_entry(self, sha, type, type_data):
101
"""Add a new entry to the database.
103
raise NotImplementedError(self.add_entry)
105
def add_entries(self, entries):
106
"""Add multiple new entries to the database.
111
def get_inventory_sha_map(self, revid):
112
"""Return the inventory SHA map for a revision.
114
:param revid: Revision to fetch the map for
115
:return: A `InventorySHAMap`
117
raise NotImplementedError(self.get_inventory_sha_map)
119
def lookup_git_sha(self, sha):
120
"""Lookup a Git sha in the database.
122
:param sha: Git object sha
123
:return: (type, type_data) with type_data:
124
revision: revid, tree sha
126
raise NotImplementedError(self.lookup_git_sha)
129
"""List the revision ids known."""
130
raise NotImplementedError(self.revids)
132
def missing_revisions(self, revids):
133
"""Return set of all the revisions that are not present."""
134
present_revids = set(self.revids())
135
if not isinstance(revids, set):
137
return revids - present_revids
140
"""List the SHA1s."""
141
raise NotImplementedError(self.sha1s)
143
def start_write_group(self):
144
"""Start writing changes."""
146
def commit_write_group(self):
147
"""Commit any pending changes."""
149
def abort_write_group(self):
150
"""Abort any pending changes."""
153
class DictGitShaMap(GitShaMap):
159
def add_entry(self, sha, type, type_data):
160
self._by_sha[sha] = (type, type_data)
161
if type in ("blob", "tree"):
162
self._by_fileid.setdefault(type_data[1], {})[type_data[0]] = sha
164
def get_inventory_sha_map(self, revid):
165
class DictInventorySHAMap(InventorySHAMap):
167
def __init__(self, base, revid):
171
def lookup_blob(self, fileid, revision_hint=None):
172
if revision_hint is not None:
173
revid = revision_hint
176
return self._base._by_fileid[revid][fileid]
178
def lookup_tree(self, fileid):
179
return self._base._by_fileid[self.revid][fileid]
181
return DictInventorySHAMap(self, revid)
183
def lookup_git_sha(self, sha):
184
return self._by_sha[sha]
187
for key, (type, type_data) in self._by_sha.iteritems():
192
return self._by_sha.iterkeys()
195
class SqliteGitShaMap(GitShaMap):
197
def __init__(self, path=None):
200
self.db = sqlite3.connect(":memory:")
202
if not mapdbs().has_key(path):
203
mapdbs()[path] = sqlite3.connect(path)
204
self.db = mapdbs()[path]
205
self.db.text_factory = str
55
206
self.db.executescript("""
56
create table if not exists commits(sha1 text, revid text, tree_sha text);
207
create table if not exists commits(
208
sha1 text not null check(length(sha1) == 40),
210
tree_sha text not null check(length(tree_sha) == 40)
57
212
create index if not exists commit_sha1 on commits(sha1);
58
create table if not exists blobs(sha1 text, fileid text, revid text);
213
create unique index if not exists commit_revid on commits(revid);
214
create table if not exists blobs(
215
sha1 text not null check(length(sha1) == 40),
216
fileid text not null,
59
219
create index if not exists blobs_sha1 on blobs(sha1);
60
create table if not exists trees(sha1 text, fileid text, revid text);
61
create index if not exists trees_sha1 on trees(sha1);
220
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
221
create table if not exists trees(
222
sha1 text unique not null check(length(sha1) == 40),
223
fileid text not null,
226
create unique index if not exists trees_sha1 on trees(sha1);
227
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")
231
return "%s(%r)" % (self.__class__.__name__, self.path)
234
def remove_for_repository(cls, repository):
235
repository._transport.delete('git.db')
238
def exists_for_repository(cls, repository):
240
transport = getattr(repository, "_transport", None)
241
if transport is not None:
242
return transport.has("git.db")
243
except bzrlib.errors.NotLocalUrl:
247
def from_repository(cls, repository):
249
transport = getattr(repository, "_transport", None)
250
if transport is not None:
251
return cls(os.path.join(transport.local_abspath("."), "git.db"))
252
except bzrlib.errors.NotLocalUrl:
254
return cls(os.path.join(get_cache_dir(), "remote.db"))
256
def lookup_commit(self, revid):
257
row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
262
def commit_write_group(self):
265
def add_entries(self, entries):
268
for sha, type, type_data in entries:
269
assert isinstance(type_data[0], str)
270
assert isinstance(type_data[1], str)
271
entry = (sha, type_data[0], type_data[1])
279
self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
281
self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
67
284
def add_entry(self, sha, type, type_data):
68
285
"""Add a new entry to the database.
70
287
assert isinstance(type_data, tuple)
71
290
assert isinstance(sha, str), "type was %r" % sha
72
291
if type == "commit":
73
292
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]))
293
elif type in ("blob", "tree"):
294
self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
79
296
raise AssertionError("Unknown type %s" % type)
298
def get_inventory_sha_map(self, revid):
299
class SqliteInventorySHAMap(InventorySHAMap):
301
def __init__(self, db, revid):
305
def lookup_blob(self, fileid, revision_hint=None):
306
if revision_hint is not None:
307
revid = revision_hint
310
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
313
raise KeyError(fileid)
315
def lookup_tree(self, fileid):
316
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
319
raise KeyError(fileid)
321
return SqliteInventorySHAMap(self.db, revid)
81
323
def lookup_git_sha(self, sha):
82
324
"""Lookup a Git sha in the database.
85
327
:return: (type, type_data) with type_data:
86
328
revision: revid, tree sha
330
def format(type, row):
331
return (type, (row[0], row[1]))
88
332
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
89
333
if row is not None:
90
return ("commit", row)
334
return format("commit", row)
91
335
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
336
if row is not None:
337
return format("blob", row)
94
338
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
95
339
if row is not None:
340
return format("tree", row)
97
341
raise KeyError(sha)
100
for row in self.db.execute("select revid from commits").fetchall():
344
"""List the revision ids known."""
345
return (row for (row,) in self.db.execute("select revid from commits"))
348
"""List the SHA1s."""
349
for table in ("blobs", "commits", "trees"):
351
for (row,) in self.db.execute("select sha1 from %s" % table):
356
TDB_HASH_SIZE = 50000
359
class TdbGitShaMap(GitShaMap):
360
"""SHA Map that uses a TDB database.
364
"git <sha1>" -> "<type> <type-data1> <type-data2>"
365
"commit revid" -> "<sha1> <tree-id>"
366
"tree fileid revid" -> "<sha1>"
367
"blob fileid revid" -> "<sha1>"
370
def __init__(self, path=None):
376
if not mapdbs().has_key(path):
377
mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
378
os.O_RDWR|os.O_CREAT)
379
self.db = mapdbs()[path]
381
if int(self.db["version"]) not in (2, 3):
382
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
383
self.db["version"], TDB_MAP_VERSION)
387
self.db["version"] = str(TDB_MAP_VERSION)
390
return "%s(%r)" % (self.__class__.__name__, self.path)
393
def exists_for_repository(cls, repository):
395
transport = getattr(repository, "_transport", None)
396
if transport is not None:
397
return transport.has("git.tdb")
398
except bzrlib.errors.NotLocalUrl:
402
def remove_for_repository(cls, repository):
403
repository._transport.delete('git.tdb')
406
def from_repository(cls, repository):
408
transport = getattr(repository, "_transport", None)
409
if transport is not None:
410
return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
411
except bzrlib.errors.NotLocalUrl:
413
return cls(os.path.join(get_cache_dir(), "remote.tdb"))
415
def lookup_commit(self, revid):
416
return sha_to_hex(self.db["commit\0" + revid][:20])
418
def add_entry(self, hexsha, type, type_data):
419
"""Add a new entry to the database.
424
sha = hex_to_sha(hexsha)
425
self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
427
self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
429
self.db["\0".join(("blob", type_data[0], type_data[1]))] = sha
431
def get_inventory_sha_map(self, revid):
433
class TdbInventorySHAMap(InventorySHAMap):
435
def __init__(self, db, revid):
439
def lookup_blob(self, fileid, revision_hint=None):
440
if revision_hint is not None:
441
revid = revision_hint
444
return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
446
return TdbInventorySHAMap(self.db, revid)
448
def lookup_git_sha(self, sha):
449
"""Lookup a Git sha in the database.
451
:param sha: Git object sha
452
:return: (type, type_data) with type_data:
453
revision: revid, tree sha
456
sha = hex_to_sha(sha)
457
data = self.db["git\0" + sha].split("\0")
458
return (data[0], (data[1], data[2]))
460
def missing_revisions(self, revids):
463
if self.db.get("commit\0" + revid) is None:
468
"""List the revision ids known."""
469
for key in self.db.iterkeys():
470
if key.startswith("commit\0"):
474
"""List the SHA1s."""
475
for key in self.db.iterkeys():
476
if key.startswith("git\0"):
477
yield sha_to_hex(key[4:])
480
def migrate(source, target):
481
"""Migrate from one cache map to another."""
482
pb = ui.ui_factory.nested_progress_bar()
484
target.start_write_group()
486
for i, sha in enumerate(source.sha1s()):
487
pb.update("migrating sha map", i)
488
(kind, info) = source.lookup_git_sha(sha)
489
target.add_entry(sha, kind, info)
491
target.abort_write_group()
494
target.commit_write_group()
499
def from_repository(repository):
502
shamap = TdbGitShaMap.from_repository(repository)
503
upgrade_from = [SqliteGitShaMap]
505
shamap = SqliteGitShaMap.from_repository(repository)
506
for cls in upgrade_from:
507
if not cls.exists_for_repository(repository):
509
old_shamap = cls.from_repository(repository)
510
trace.info('Importing SHA map from %r into %r',
512
migrate(old_shamap, shamap)
513
cls.remove_for_repository(repository)