/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to shamap.py

Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Map from Git sha's to Bazaar objects."""
 
18
 
 
19
import os
 
20
 
 
21
import bzrlib
 
22
from bzrlib.errors import (
 
23
    NoSuchRevision,
 
24
    )
 
25
 
 
26
 
 
27
def check_pysqlite_version(sqlite3):
 
28
    """Check that sqlite library is compatible.
 
29
 
 
30
    """
 
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")
 
36
 
 
37
try:
 
38
    try:
 
39
        import sqlite3
 
40
        check_pysqlite_version(sqlite3)
 
41
    except (ImportError, bzrlib.errors.BzrError), e: 
 
42
        from pysqlite2 import dbapi2 as sqlite3
 
43
        check_pysqlite_version(sqlite3)
 
44
except:
 
45
    warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
 
46
            'module')
 
47
    raise bzrlib.errors.BzrError("missing sqlite library")
 
48
 
 
49
 
 
50
class GitShaMap(object):
 
51
    """Git<->Bzr revision id mapping database."""
 
52
 
 
53
    def add_entry(self, sha, type, type_data):
 
54
        """Add a new entry to the database.
 
55
        """
 
56
        raise NotImplementedError(self.add_entry)
 
57
 
 
58
    def add_entries(self, entries):
 
59
        """Add multiple new entries to the database.
 
60
        """
 
61
        for e in entries:
 
62
            self.add_entry(*e)
 
63
 
 
64
    def lookup_tree(self, fileid, revid):
 
65
        """Lookup the SHA of a git tree."""
 
66
        raise NotImplementedError(self.lookup_tree)
 
67
 
 
68
    def lookup_blob(self, fileid, revid):
 
69
        raise NotImplementedError(self.lookup_blob)
 
70
 
 
71
    def lookup_git_sha(self, sha):
 
72
        """Lookup a Git sha in the database.
 
73
 
 
74
        :param sha: Git object sha
 
75
        :return: (type, type_data) with type_data:
 
76
            revision: revid, tree sha
 
77
        """
 
78
        raise NotImplementedError(self.lookup_git_sha)
 
79
 
 
80
    def revids(self):
 
81
        """List the revision ids known."""
 
82
        raise NotImplementedError(self.revids)
 
83
 
 
84
    def commit(self):
 
85
        """Commit any pending changes."""
 
86
 
 
87
 
 
88
class DictGitShaMap(GitShaMap):
 
89
 
 
90
    def __init__(self):
 
91
        self.dict = {}
 
92
 
 
93
    def add_entry(self, sha, type, type_data):
 
94
        self.dict[sha] = (type, type_data)
 
95
 
 
96
    def lookup_git_sha(self, sha):
 
97
        return self.dict[sha]
 
98
 
 
99
    def lookup_tree(self, fileid, revid):
 
100
        for k, v in self.dict.iteritems():
 
101
            if v == ("tree", (fileid, revid)):
 
102
                return k
 
103
        raise KeyError((fileid, revid))
 
104
 
 
105
    def lookup_blob(self, fileid, revid):
 
106
        for k, v in self.dict.iteritems():
 
107
            if v == ("blob", (fileid, revid)):
 
108
                return k
 
109
        raise KeyError((fileid, revid))
 
110
 
 
111
    def revids(self):
 
112
        for key, (type, type_data) in self.dict.iteritems():
 
113
            if type == "commit":
 
114
                yield type_data[0]
 
115
 
 
116
 
 
117
class SqliteGitShaMap(GitShaMap):
 
118
 
 
119
    def __init__(self, transport=None):
 
120
        self.transport = transport
 
121
        if transport is None:
 
122
            self.db = sqlite3.connect(":memory:")
 
123
        else:
 
124
            self.db = sqlite3.connect(
 
125
                os.path.join(self.transport.local_abspath("."), "git.db"))
 
126
        self.db.executescript("""
 
127
        create table if not exists commits(sha1 text, revid text, tree_sha text);
 
128
        create index if not exists commit_sha1 on commits(sha1);
 
129
        create unique index if not exists commit_revid on commits(revid);
 
130
        create table if not exists blobs(sha1 text, fileid text, revid text);
 
131
        create index if not exists blobs_sha1 on blobs(sha1);
 
132
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
 
133
        create table if not exists trees(sha1 text, fileid text, revid text);
 
134
        create index if not exists trees_sha1 on trees(sha1);
 
135
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
 
136
""")
 
137
 
 
138
    def _parent_lookup(self, revid):
 
139
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
 
140
 
 
141
    def commit(self):
 
142
        self.db.commit()
 
143
 
 
144
    def add_entry(self, sha, type, type_data):
 
145
        """Add a new entry to the database.
 
146
        """
 
147
        assert isinstance(type_data, tuple)
 
148
        assert isinstance(sha, str), "type was %r" % sha
 
149
        if type == "commit":
 
150
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
151
        elif type in ("blob", "tree"):
 
152
            self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
 
153
        else:
 
154
            raise AssertionError("Unknown type %s" % type)
 
155
 
 
156
    def lookup_tree(self, fileid, revid):
 
157
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
 
158
        if row is None:
 
159
            raise KeyError((fileid, revid))
 
160
        return row[0].encode("utf-8")
 
161
 
 
162
    def lookup_blob(self, fileid, revid):
 
163
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
 
164
        if row is None:
 
165
            raise KeyError((fileid, revid))
 
166
        return row[0].encode("utf-8")
 
167
 
 
168
    def lookup_git_sha(self, sha):
 
169
        """Lookup a Git sha in the database.
 
170
 
 
171
        :param sha: Git object sha
 
172
        :return: (type, type_data) with type_data:
 
173
            revision: revid, tree sha
 
174
        """
 
175
        def format(type, row):
 
176
            return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
 
177
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
 
178
        if row is not None:
 
179
            return format("commit", row)
 
180
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
 
181
        if row is not None:
 
182
            return format("blob", row)
 
183
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
184
        if row is not None:
 
185
            return format("tree", row)
 
186
        raise KeyError(sha)
 
187
 
 
188
    def revids(self):
 
189
        """List the revision ids known."""
 
190
        for row in self.db.execute("select revid from commits").fetchall():
 
191
            yield row[0].encode("utf-8")