/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
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
20
import threading
0.200.292 by Jelmer Vernooij
Fix formatting.
21
0.200.228 by Jelmer Vernooij
Split out map.
22
import bzrlib
0.200.292 by Jelmer Vernooij
Fix formatting.
23
from bzrlib.errors import (
24
    NoSuchRevision,
25
    )
0.200.230 by Jelmer Vernooij
Implement sha cache.
26
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
27
0.200.228 by Jelmer Vernooij
Split out map.
28
def check_pysqlite_version(sqlite3):
29
    """Check that sqlite library is compatible.
30
31
    """
32
    if (sqlite3.sqlite_version_info[0] < 3 or 
33
            (sqlite3.sqlite_version_info[0] == 3 and 
34
             sqlite3.sqlite_version_info[1] < 3)):
35
        warning('Needs at least sqlite 3.3.x')
36
        raise bzrlib.errors.BzrError("incompatible sqlite library")
37
38
try:
39
    try:
40
        import sqlite3
41
        check_pysqlite_version(sqlite3)
42
    except (ImportError, bzrlib.errors.BzrError), e: 
43
        from pysqlite2 import dbapi2 as sqlite3
44
        check_pysqlite_version(sqlite3)
45
except:
46
    warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
47
            'module')
48
    raise bzrlib.errors.BzrError("missing sqlite library")
49
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
50
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
51
_mapdbs = threading.local()
52
def mapdbs():
53
    """Get a cache for this thread's db connections."""
54
    try:
55
        return _mapdbs.cache
56
    except AttributeError:
57
        _mapdbs.cache = {}
58
        return _mapdbs.cache
59
60
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
61
class GitShaMap(object):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
62
    """Git<->Bzr revision id mapping database."""
63
64
    def add_entry(self, sha, type, type_data):
65
        """Add a new entry to the database.
66
        """
67
        raise NotImplementedError(self.add_entry)
68
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
69
    def add_entries(self, entries):
70
        """Add multiple new entries to the database.
71
        """
72
        for e in entries:
73
            self.add_entry(*e)
74
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
75
    def lookup_tree(self, fileid, revid):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
76
        """Lookup the SHA of a git tree."""
77
        raise NotImplementedError(self.lookup_tree)
78
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
79
    def lookup_blob(self, fileid, revid):
80
        raise NotImplementedError(self.lookup_blob)
81
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
82
    def lookup_git_sha(self, sha):
83
        """Lookup a Git sha in the database.
84
85
        :param sha: Git object sha
86
        :return: (type, type_data) with type_data:
87
            revision: revid, tree sha
88
        """
89
        raise NotImplementedError(self.lookup_git_sha)
90
91
    def revids(self):
92
        """List the revision ids known."""
93
        raise NotImplementedError(self.revids)
94
95
    def commit(self):
96
        """Commit any pending changes."""
97
98
99
class DictGitShaMap(GitShaMap):
100
101
    def __init__(self):
102
        self.dict = {}
103
104
    def add_entry(self, sha, type, type_data):
105
        self.dict[sha] = (type, type_data)
106
107
    def lookup_git_sha(self, sha):
108
        return self.dict[sha]
109
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
110
    def lookup_tree(self, fileid, revid):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
111
        for k, v in self.dict.iteritems():
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
112
            if v == ("tree", (fileid, revid)):
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
113
                return k
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
114
        raise KeyError((fileid, revid))
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
115
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
116
    def lookup_blob(self, fileid, revid):
117
        for k, v in self.dict.iteritems():
118
            if v == ("blob", (fileid, revid)):
119
                return k
120
        raise KeyError((fileid, revid))
121
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
122
    def revids(self):
123
        for key, (type, type_data) in self.dict.iteritems():
124
            if type == "commit":
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
125
                yield type_data[0]
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
126
127
128
class SqliteGitShaMap(GitShaMap):
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
129
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
130
    def __init__(self, path=None):
131
        self.path = path
132
        if path is None:
0.200.262 by Jelmer Vernooij
Add tests for GitShaMap.
133
            self.db = sqlite3.connect(":memory:")
134
        else:
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
135
            if not mapdbs().has_key(path):
136
                mapdbs()[path] = sqlite3.connect(path)
137
            self.db = mapdbs()[path]    
0.200.230 by Jelmer Vernooij
Implement sha cache.
138
        self.db.executescript("""
139
        create table if not exists commits(sha1 text, revid text, tree_sha text);
140
        create index if not exists commit_sha1 on commits(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
141
        create unique index if not exists commit_revid on commits(revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
142
        create table if not exists blobs(sha1 text, fileid text, revid text);
143
        create index if not exists blobs_sha1 on blobs(sha1);
0.200.284 by Jelmer Vernooij
Add extra indexes.
144
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
145
        create table if not exists trees(sha1 text, fileid text, revid text);
0.200.230 by Jelmer Vernooij
Implement sha cache.
146
        create index if not exists trees_sha1 on trees(sha1);
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
147
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
0.200.230 by Jelmer Vernooij
Implement sha cache.
148
""")
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
149
0.200.365 by Jelmer Vernooij
Share sha map cache connections inside threads.
150
    @classmethod
151
    def from_repository(cls, repository):
152
        return cls(os.path.join(repository._transport.local_abspath("."), "git.db"))
153
0.200.231 by Jelmer Vernooij
Partially fix pull.
154
    def _parent_lookup(self, revid):
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
155
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
156
        if row is not None:
157
            return row[0].encode("utf-8")
158
        raise KeyError
0.200.231 by Jelmer Vernooij
Partially fix pull.
159
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
160
    def commit(self):
161
        self.db.commit()
162
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
163
    def add_entry(self, sha, type, type_data):
164
        """Add a new entry to the database.
165
        """
0.200.231 by Jelmer Vernooij
Partially fix pull.
166
        assert isinstance(type_data, tuple)
167
        assert isinstance(sha, str), "type was %r" % sha
0.200.230 by Jelmer Vernooij
Implement sha cache.
168
        if type == "commit":
169
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
170
        elif type in ("blob", "tree"):
171
            self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
0.200.230 by Jelmer Vernooij
Implement sha cache.
172
        else:
173
            raise AssertionError("Unknown type %s" % type)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
174
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
175
    def lookup_tree(self, fileid, revid):
176
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
177
        if row is None:
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
178
            raise KeyError((fileid, revid))
0.200.358 by Jelmer Vernooij
Use plain strings for sha1 strings, not unicode.
179
        return row[0].encode("utf-8")
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
180
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
181
    def lookup_blob(self, fileid, revid):
0.230.3 by Jelmer Vernooij
Fix blob lookup.
182
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
183
        if row is None:
184
            raise KeyError((fileid, revid))
0.200.358 by Jelmer Vernooij
Use plain strings for sha1 strings, not unicode.
185
        return row[0].encode("utf-8")
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
186
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
187
    def lookup_git_sha(self, sha):
188
        """Lookup a Git sha in the database.
189
190
        :param sha: Git object sha
191
        :return: (type, type_data) with type_data:
192
            revision: revid, tree sha
193
        """
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
194
        def format(type, row):
195
            return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
0.200.230 by Jelmer Vernooij
Implement sha cache.
196
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
197
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
198
            return format("commit", row)
0.200.230 by Jelmer Vernooij
Implement sha cache.
199
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
200
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
201
            return format("blob", row)
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
202
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
0.200.230 by Jelmer Vernooij
Implement sha cache.
203
        if row is not None:
0.200.353 by Jelmer Vernooij
fileids/revids are plain strings, not unicode
204
            return format("tree", row)
0.200.230 by Jelmer Vernooij
Implement sha cache.
205
        raise KeyError(sha)
206
207
    def revids(self):
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
208
        """List the revision ids known."""
0.200.230 by Jelmer Vernooij
Implement sha cache.
209
        for row in self.db.execute("select revid from commits").fetchall():
0.200.347 by Jelmer Vernooij
Simplify converter a bit.
210
            yield row[0].encode("utf-8")