/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

Add roundtrip tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Map from Git sha's to Bazaar objects."""
18
18
 
 
19
import os
 
20
 
19
21
import bzrlib
20
 
 
21
 
from bzrlib.errors import NoSuchRevision
22
 
 
23
 
import os
 
22
from bzrlib.errors import (
 
23
    NoSuchRevision,
 
24
    )
24
25
 
25
26
 
26
27
def check_pysqlite_version(sqlite3):
47
48
 
48
49
 
49
50
class GitShaMap(object):
50
 
 
51
 
    def __init__(self, transport):
 
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 lookup_tree(self, fileid, revid):
 
59
        """Lookup the SHA of a git tree."""
 
60
        raise NotImplementedError(self.lookup_tree)
 
61
 
 
62
    def lookup_blob(self, fileid, revid):
 
63
        raise NotImplementedError(self.lookup_blob)
 
64
 
 
65
    def lookup_git_sha(self, sha):
 
66
        """Lookup a Git sha in the database.
 
67
 
 
68
        :param sha: Git object sha
 
69
        :return: (type, type_data) with type_data:
 
70
            revision: revid, tree sha
 
71
        """
 
72
        raise NotImplementedError(self.lookup_git_sha)
 
73
 
 
74
    def revids(self):
 
75
        """List the revision ids known."""
 
76
        raise NotImplementedError(self.revids)
 
77
 
 
78
    def commit(self):
 
79
        """Commit any pending changes."""
 
80
 
 
81
 
 
82
class DictGitShaMap(GitShaMap):
 
83
 
 
84
    def __init__(self):
 
85
        self.dict = {}
 
86
 
 
87
    def add_entry(self, sha, type, type_data):
 
88
        self.dict[sha] = (type, type_data)
 
89
 
 
90
    def lookup_git_sha(self, sha):
 
91
        return self.dict[sha]
 
92
 
 
93
    def lookup_tree(self, fileid, revid):
 
94
        for k, v in self.dict.iteritems():
 
95
            if v == ("tree", (fileid, revid)):
 
96
                return k
 
97
        raise KeyError((fileid, revid))
 
98
 
 
99
    def lookup_blob(self, fileid, revid):
 
100
        for k, v in self.dict.iteritems():
 
101
            if v == ("blob", (fileid, revid)):
 
102
                return k
 
103
        raise KeyError((fileid, revid))
 
104
 
 
105
    def revids(self):
 
106
        for key, (type, type_data) in self.dict.iteritems():
 
107
            if type == "commit":
 
108
                yield type_data[0]
 
109
 
 
110
 
 
111
class SqliteGitShaMap(GitShaMap):
 
112
 
 
113
    def __init__(self, transport=None):
52
114
        self.transport = transport
53
 
        self.db = sqlite3.connect(
54
 
            os.path.join(self.transport.local_abspath("."), "git.db"))
 
115
        if transport is None:
 
116
            self.db = sqlite3.connect(":memory:")
 
117
        else:
 
118
            self.db = sqlite3.connect(
 
119
                os.path.join(self.transport.local_abspath("."), "git.db"))
55
120
        self.db.executescript("""
56
121
        create table if not exists commits(sha1 text, revid text, tree_sha text);
57
122
        create index if not exists commit_sha1 on commits(sha1);
 
123
        create unique index if not exists commit_revid on commits(revid);
58
124
        create table if not exists blobs(sha1 text, fileid text, revid text);
59
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);
60
127
        create table if not exists trees(sha1 text, fileid text, revid text);
61
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);
62
130
""")
63
131
 
64
132
    def _parent_lookup(self, revid):
65
133
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
66
134
 
 
135
    def commit(self):
 
136
        self.db.commit()
 
137
 
67
138
    def add_entry(self, sha, type, type_data):
68
139
        """Add a new entry to the database.
69
140
        """
71
142
        assert isinstance(sha, str), "type was %r" % sha
72
143
        if type == "commit":
73
144
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
74
 
        elif type == "blob":
75
 
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
76
 
        elif type == "tree":
77
 
            self.db.execute("replace into trees (sha1, fileid, revid) 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]))
78
147
        else:
79
148
            raise AssertionError("Unknown type %s" % type)
80
149
 
 
150
    def lookup_tree(self, fileid, revid):
 
151
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
 
152
        if row is None:
 
153
            raise KeyError((fileid, revid))
 
154
        return row[0]
 
155
 
 
156
    def lookup_blob(self, fileid, revid):
 
157
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
 
158
        if row is None:
 
159
            raise KeyError((fileid, revid))
 
160
        return row[0]
 
161
 
81
162
    def lookup_git_sha(self, sha):
82
163
        """Lookup a Git sha in the database.
83
164
 
97
178
        raise KeyError(sha)
98
179
 
99
180
    def revids(self):
 
181
        """List the revision ids known."""
100
182
        for row in self.db.execute("select revid from commits").fetchall():
101
 
            yield row[0]
 
183
            yield row[0].encode("utf-8")