/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
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
0.200.292 by Jelmer Vernooij
Fix formatting.
19
import os
20
0.200.228 by Jelmer Vernooij
Split out map.
21
import bzrlib
0.200.292 by Jelmer Vernooij
Fix formatting.
22
from bzrlib.errors import (
23
    NoSuchRevision,
24
    )
0.200.230 by Jelmer Vernooij
Implement sha cache.
25
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
26
0.200.228 by Jelmer Vernooij
Split out map.
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
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
49
50
class GitShaMap(object):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
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
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
58
    def lookup_tree(self, path, revid):
59
        """Lookup the SHA of a git tree."""
60
        raise NotImplementedError(self.lookup_tree)
61
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
62
    def lookup_git_sha(self, sha):
63
        """Lookup a Git sha in the database.
64
65
        :param sha: Git object sha
66
        :return: (type, type_data) with type_data:
67
            revision: revid, tree sha
68
        """
69
        raise NotImplementedError(self.lookup_git_sha)
70
71
    def revids(self):
72
        """List the revision ids known."""
73
        raise NotImplementedError(self.revids)
74
75
    def commit(self):
76
        """Commit any pending changes."""
77
78
79
class DictGitShaMap(GitShaMap):
80
81
    def __init__(self):
82
        self.dict = {}
83
84
    def add_entry(self, sha, type, type_data):
85
        self.dict[sha] = (type, type_data)
86
87
    def lookup_git_sha(self, sha):
88
        return self.dict[sha]
89
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
90
    def lookup_tree(self, path, revid):
91
        for k, v in self.dict.iteritems():
92
            if v == ("tree", (path, revid)):
93
                return k
94
        raise KeyError((path, revid))
95
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
96
    def revids(self):
97
        for key, (type, type_data) in self.dict.iteritems():
98
            if type == "commit":
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
99
                yield type_data[0]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
100
101
102
class SqliteGitShaMap(GitShaMap):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
103
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
104
    def __init__(self, transport=None):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
105
        self.transport = transport
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
106
        if transport is None:
107
            self.db = sqlite3.connect(":memory:")
108
        else:
109
            self.db = sqlite3.connect(
110
                os.path.join(self.transport.local_abspath("."), "git.db"))
0.200.230 by Jelmer Vernooij
Implement sha cache.
111
        self.db.executescript("""
112
        create table if not exists commits(sha1 text, revid text, tree_sha text);
113
        create index if not exists commit_sha1 on commits(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
114
        create unique index if not exists commit_revid on commits(revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
115
        create table if not exists blobs(sha1 text, fileid text, revid text);
116
        create index if not exists blobs_sha1 on blobs(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
117
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
118
        create table if not exists trees(sha1 text, path text, revid text);
0.200.230 by Jelmer Vernooij
Implement sha cache.
119
        create index if not exists trees_sha1 on trees(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
120
        create unique index if not exists trees_path_revid on trees(path, revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
121
""")
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
122
0.200.231 by Jelmer Vernooij
Partially fix pull.
123
    def _parent_lookup(self, revid):
124
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
125
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
126
    def commit(self):
127
        self.db.commit()
128
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
129
    def add_entry(self, sha, type, type_data):
130
        """Add a new entry to the database.
131
        """
0.200.231 by Jelmer Vernooij
Partially fix pull.
132
        assert isinstance(type_data, tuple)
133
        assert isinstance(sha, str), "type was %r" % sha
0.200.230 by Jelmer Vernooij
Implement sha cache.
134
        if type == "commit":
135
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
136
        elif type == "blob":
137
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
138
        elif type == "tree":
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
139
            self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
0.200.230 by Jelmer Vernooij
Implement sha cache.
140
        else:
141
            raise AssertionError("Unknown type %s" % type)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
142
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
143
    def lookup_tree(self, path, revid):
144
        row = self.db.execute("select sha1 from trees where path = ? and revid = ?", (path,revid)).fetchone()
145
        if row is None:
146
            raise KeyError((path, revid))
147
        return row[0]
148
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
149
    def lookup_git_sha(self, sha):
150
        """Lookup a Git sha in the database.
151
152
        :param sha: Git object sha
153
        :return: (type, type_data) with type_data:
154
            revision: revid, tree sha
155
        """
0.200.230 by Jelmer Vernooij
Implement sha cache.
156
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
157
        if row is not None:
158
            return ("commit", row)
159
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
160
        if row is not None:
161
            return ("blob", row)
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
162
        row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
0.200.230 by Jelmer Vernooij
Implement sha cache.
163
        if row is not None:
164
            return ("tree", row)
165
        raise KeyError(sha)
166
167
    def revids(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
168
        """List the revision ids known."""
0.200.230 by Jelmer Vernooij
Implement sha cache.
169
        for row in self.db.execute("select revid from commits").fetchall():
170
            yield row[0]