/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.228 by Jelmer Vernooij
Split out map.
19
import bzrlib
20
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
21
from bzrlib.errors import NoSuchRevision
22
0.200.230 by Jelmer Vernooij
Implement sha cache.
23
import os
24
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
25
0.200.228 by Jelmer Vernooij
Split out map.
26
def check_pysqlite_version(sqlite3):
27
    """Check that sqlite library is compatible.
28
29
    """
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")
35
36
try:
37
    try:
38
        import sqlite3
39
        check_pysqlite_version(sqlite3)
40
    except (ImportError, bzrlib.errors.BzrError), e: 
41
        from pysqlite2 import dbapi2 as sqlite3
42
        check_pysqlite_version(sqlite3)
43
except:
44
    warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
45
            'module')
46
    raise bzrlib.errors.BzrError("missing sqlite library")
47
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
48
49
class GitShaMap(object):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
50
    """Git<->Bzr revision id mapping database."""
51
52
    def add_entry(self, sha, type, type_data):
53
        """Add a new entry to the database.
54
        """
55
        raise NotImplementedError(self.add_entry)
56
57
    def lookup_git_sha(self, sha):
58
        """Lookup a Git sha in the database.
59
60
        :param sha: Git object sha
61
        :return: (type, type_data) with type_data:
62
            revision: revid, tree sha
63
        """
64
        raise NotImplementedError(self.lookup_git_sha)
65
66
    def revids(self):
67
        """List the revision ids known."""
68
        raise NotImplementedError(self.revids)
69
70
    def commit(self):
71
        """Commit any pending changes."""
72
73
74
class DictGitShaMap(GitShaMap):
75
76
    def __init__(self):
77
        self.dict = {}
78
79
    def add_entry(self, sha, type, type_data):
80
        self.dict[sha] = (type, type_data)
81
82
    def lookup_git_sha(self, sha):
83
        return self.dict[sha]
84
85
    def revids(self):
86
        for key, (type, type_data) in self.dict.iteritems():
87
            if type == "commit":
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
88
                yield type_data[0]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
89
90
91
class SqliteGitShaMap(GitShaMap):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
92
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
93
    def __init__(self, transport=None):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
94
        self.transport = transport
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
95
        if transport is None:
96
            self.db = sqlite3.connect(":memory:")
97
        else:
98
            self.db = sqlite3.connect(
99
                os.path.join(self.transport.local_abspath("."), "git.db"))
0.200.230 by Jelmer Vernooij
Implement sha cache.
100
        self.db.executescript("""
101
        create table if not exists commits(sha1 text, revid text, tree_sha text);
102
        create index if not exists commit_sha1 on commits(sha1);
103
        create table if not exists blobs(sha1 text, fileid text, revid text);
104
        create index if not exists blobs_sha1 on blobs(sha1);
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
105
        create table if not exists trees(sha1 text, path text, revid text);
0.200.230 by Jelmer Vernooij
Implement sha cache.
106
        create index if not exists trees_sha1 on trees(sha1);
107
""")
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
108
0.200.231 by Jelmer Vernooij
Partially fix pull.
109
    def _parent_lookup(self, revid):
110
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
111
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
112
    def commit(self):
113
        self.db.commit()
114
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
115
    def add_entry(self, sha, type, type_data):
116
        """Add a new entry to the database.
117
        """
0.200.231 by Jelmer Vernooij
Partially fix pull.
118
        assert isinstance(type_data, tuple)
119
        assert isinstance(sha, str), "type was %r" % sha
0.200.230 by Jelmer Vernooij
Implement sha cache.
120
        if type == "commit":
121
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
122
        elif type == "blob":
123
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
124
        elif type == "tree":
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
125
            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.
126
        else:
127
            raise AssertionError("Unknown type %s" % type)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
128
129
    def lookup_git_sha(self, sha):
130
        """Lookup a Git sha in the database.
131
132
        :param sha: Git object sha
133
        :return: (type, type_data) with type_data:
134
            revision: revid, tree sha
135
        """
0.200.230 by Jelmer Vernooij
Implement sha cache.
136
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
137
        if row is not None:
138
            return ("commit", row)
139
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
140
        if row is not None:
141
            return ("blob", row)
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
142
        row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
0.200.230 by Jelmer Vernooij
Implement sha cache.
143
        if row is not None:
144
            return ("tree", row)
145
        raise KeyError(sha)
146
147
    def revids(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
148
        """List the revision ids known."""
0.200.230 by Jelmer Vernooij
Implement sha cache.
149
        for row in self.db.execute("select revid from commits").fetchall():
150
            yield row[0]