1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Map from Git sha's to Bazaar objects."""
19
from dulwich.objects import (
27
from bzrlib.errors import (
32
def check_pysqlite_version(sqlite3):
33
"""Check that sqlite library is compatible.
36
if (sqlite3.sqlite_version_info[0] < 3 or
37
(sqlite3.sqlite_version_info[0] == 3 and
38
sqlite3.sqlite_version_info[1] < 3)):
39
warning('Needs at least sqlite 3.3.x')
40
raise bzrlib.errors.BzrError("incompatible sqlite library")
45
check_pysqlite_version(sqlite3)
46
except (ImportError, bzrlib.errors.BzrError), e:
47
from pysqlite2 import dbapi2 as sqlite3
48
check_pysqlite_version(sqlite3)
50
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
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:
65
class GitShaMap(object):
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]
150
self.db.executescript("""
151
create table if not exists commits(sha1 text, revid text, tree_sha text);
152
create index if not exists commit_sha1 on commits(sha1);
153
create unique index if not exists commit_revid on commits(revid);
154
create table if not exists blobs(sha1 text, fileid text, revid text);
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);
157
create table if not exists trees(sha1 text, fileid text, revid text);
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);
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)
195
def add_entry(self, sha, type, type_data):
196
"""Add a new entry to the database.
198
assert isinstance(type_data, tuple)
199
assert isinstance(sha, str), "type was %r" % sha
201
self.db.execute("replace into commits (sha1, revid, tree_sha) 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]))
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")
219
def lookup_git_sha(self, sha):
220
"""Lookup a Git sha in the database.
222
:param sha: Git object sha
223
:return: (type, type_data) with type_data:
224
revision: revid, tree sha
226
def format(type, row):
227
return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
228
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
230
return format("commit", row)
231
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
233
return format("blob", row)
234
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
236
return format("tree", row)
240
"""List the revision ids known."""
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:])