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."""
21
from bzrlib.errors import NoSuchRevision
26
def check_pysqlite_version(sqlite3):
27
"""Check that sqlite library is compatible.
30
if (sqlite3.sqlite_version_info[0] < 3 or
31
(sqlite3.sqlite_version_info[0] == 3 and
32
sqlite3.sqlite_version_info[1] < 3)):
33
warning('Needs at least sqlite 3.3.x')
34
raise bzrlib.errors.BzrError("incompatible sqlite library")
39
check_pysqlite_version(sqlite3)
40
except (ImportError, bzrlib.errors.BzrError), e:
41
from pysqlite2 import dbapi2 as sqlite3
42
check_pysqlite_version(sqlite3)
44
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
46
raise bzrlib.errors.BzrError("missing sqlite library")
49
class GitShaMap(object):
50
"""Git<->Bzr revision id mapping database."""
52
def add_entry(self, sha, type, type_data):
53
"""Add a new entry to the database.
55
raise NotImplementedError(self.add_entry)
57
def lookup_tree(self, path, revid):
58
"""Lookup the SHA of a git tree."""
59
raise NotImplementedError(self.lookup_tree)
61
def lookup_git_sha(self, sha):
62
"""Lookup a Git sha in the database.
64
:param sha: Git object sha
65
:return: (type, type_data) with type_data:
66
revision: revid, tree sha
68
raise NotImplementedError(self.lookup_git_sha)
71
"""List the revision ids known."""
72
raise NotImplementedError(self.revids)
75
"""Commit any pending changes."""
78
class DictGitShaMap(GitShaMap):
83
def add_entry(self, sha, type, type_data):
84
self.dict[sha] = (type, type_data)
86
def lookup_git_sha(self, sha):
89
def lookup_tree(self, path, revid):
90
for k, v in self.dict.iteritems():
91
if v == ("tree", (path, revid)):
93
raise KeyError((path, revid))
96
for key, (type, type_data) in self.dict.iteritems():
101
class SqliteGitShaMap(GitShaMap):
103
def __init__(self, transport=None):
104
self.transport = transport
105
if transport is None:
106
self.db = sqlite3.connect(":memory:")
108
self.db = sqlite3.connect(
109
os.path.join(self.transport.local_abspath("."), "git.db"))
110
self.db.executescript("""
111
create table if not exists commits(sha1 text, revid text, tree_sha text);
112
create index if not exists commit_sha1 on commits(sha1);
113
create unique index if not exists commit_revid on commits(revid);
114
create table if not exists blobs(sha1 text, fileid text, revid text);
115
create index if not exists blobs_sha1 on blobs(sha1);
116
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
117
create table if not exists trees(sha1 text, path text, revid text);
118
create index if not exists trees_sha1 on trees(sha1);
119
create unique index if not exists trees_path_revid on trees(path, revid);
122
def _parent_lookup(self, revid):
123
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
128
def add_entry(self, sha, type, type_data):
129
"""Add a new entry to the database.
131
assert isinstance(type_data, tuple)
132
assert isinstance(sha, str), "type was %r" % sha
134
self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
136
self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
138
self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
140
raise AssertionError("Unknown type %s" % type)
142
def lookup_tree(self, path, revid):
143
row = self.db.execute("select sha1 from trees where path = ? and revid = ?", (path,revid)).fetchone()
145
raise KeyError((path, revid))
148
def lookup_git_sha(self, sha):
149
"""Lookup a Git sha in the database.
151
:param sha: Git object sha
152
:return: (type, type_data) with type_data:
153
revision: revid, tree sha
155
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
157
return ("commit", row)
158
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
161
row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
167
"""List the revision ids known."""
168
for row in self.db.execute("select revid from commits").fetchall():