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."""
22
from bzrlib.errors import (
27
def check_pysqlite_version(sqlite3):
28
"""Check that sqlite library is compatible.
31
if (sqlite3.sqlite_version_info[0] < 3 or
32
(sqlite3.sqlite_version_info[0] == 3 and
33
sqlite3.sqlite_version_info[1] < 3)):
34
warning('Needs at least sqlite 3.3.x')
35
raise bzrlib.errors.BzrError("incompatible sqlite library")
40
check_pysqlite_version(sqlite3)
41
except (ImportError, bzrlib.errors.BzrError), e:
42
from pysqlite2 import dbapi2 as sqlite3
43
check_pysqlite_version(sqlite3)
45
warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
47
raise bzrlib.errors.BzrError("missing sqlite library")
50
class GitShaMap(object):
51
"""Git<->Bzr revision id mapping database."""
53
def add_entry(self, sha, type, type_data):
54
"""Add a new entry to the database.
56
raise NotImplementedError(self.add_entry)
58
def lookup_tree(self, fileid, revid):
59
"""Lookup the SHA of a git tree."""
60
raise NotImplementedError(self.lookup_tree)
62
def lookup_blob(self, fileid, revid):
63
raise NotImplementedError(self.lookup_blob)
65
def lookup_git_sha(self, sha):
66
"""Lookup a Git sha in the database.
68
:param sha: Git object sha
69
:return: (type, type_data) with type_data:
70
revision: revid, tree sha
72
raise NotImplementedError(self.lookup_git_sha)
75
"""List the revision ids known."""
76
raise NotImplementedError(self.revids)
79
"""Commit any pending changes."""
82
class DictGitShaMap(GitShaMap):
87
def add_entry(self, sha, type, type_data):
88
self.dict[sha] = (type, type_data)
90
def lookup_git_sha(self, sha):
93
def lookup_tree(self, fileid, revid):
94
for k, v in self.dict.iteritems():
95
if v == ("tree", (fileid, revid)):
97
raise KeyError((fileid, revid))
99
def lookup_blob(self, fileid, revid):
100
for k, v in self.dict.iteritems():
101
if v == ("blob", (fileid, revid)):
103
raise KeyError((fileid, revid))
106
for key, (type, type_data) in self.dict.iteritems():
111
class SqliteGitShaMap(GitShaMap):
113
def __init__(self, transport=None):
114
self.transport = transport
115
if transport is None:
116
self.db = sqlite3.connect(":memory:")
118
self.db = sqlite3.connect(
119
os.path.join(self.transport.local_abspath("."), "git.db"))
120
self.db.executescript("""
121
create table if not exists commits(sha1 text, revid text, tree_sha text);
122
create index if not exists commit_sha1 on commits(sha1);
123
create unique index if not exists commit_revid on commits(revid);
124
create table if not exists blobs(sha1 text, fileid text, revid text);
125
create index if not exists blobs_sha1 on blobs(sha1);
126
create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
127
create table if not exists trees(sha1 text, fileid text, revid text);
128
create index if not exists trees_sha1 on trees(sha1);
129
create unique index if not exists trees_fileid_revid on trees(fileid, revid);
132
def _parent_lookup(self, revid):
133
return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
138
def add_entry(self, sha, type, type_data):
139
"""Add a new entry to the database.
141
assert isinstance(type_data, tuple)
142
assert isinstance(sha, str), "type was %r" % sha
144
self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
145
elif type in ("blob", "tree"):
146
self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
148
raise AssertionError("Unknown type %s" % type)
150
def lookup_tree(self, fileid, revid):
151
row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
153
raise KeyError((fileid, revid))
156
def lookup_blob(self, fileid, revid):
157
row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
159
raise KeyError((fileid, revid))
162
def lookup_git_sha(self, sha):
163
"""Lookup a Git sha in the database.
165
:param sha: Git object sha
166
:return: (type, type_data) with type_data:
167
revision: revid, tree sha
169
def format(type, row):
170
return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
171
row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
173
return format("commit", row)
174
row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
176
return format("blob", row)
177
row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
179
return format("tree", row)
183
"""List the revision ids known."""
184
for row in self.db.execute("select revid from commits").fetchall():
185
yield row[0].encode("utf-8")