/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 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
47
47
 
48
48
 
49
49
class GitShaMap(object):
50
 
 
51
 
    def __init__(self, transport):
 
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_tree(self, path, revid):
 
58
        """Lookup the SHA of a git tree."""
 
59
        raise NotImplementedError(self.lookup_tree)
 
60
 
 
61
    def lookup_git_sha(self, sha):
 
62
        """Lookup a Git sha in the database.
 
63
 
 
64
        :param sha: Git object sha
 
65
        :return: (type, type_data) with type_data:
 
66
            revision: revid, tree sha
 
67
        """
 
68
        raise NotImplementedError(self.lookup_git_sha)
 
69
 
 
70
    def revids(self):
 
71
        """List the revision ids known."""
 
72
        raise NotImplementedError(self.revids)
 
73
 
 
74
    def commit(self):
 
75
        """Commit any pending changes."""
 
76
 
 
77
 
 
78
class DictGitShaMap(GitShaMap):
 
79
 
 
80
    def __init__(self):
 
81
        self.dict = {}
 
82
 
 
83
    def add_entry(self, sha, type, type_data):
 
84
        self.dict[sha] = (type, type_data)
 
85
 
 
86
    def lookup_git_sha(self, sha):
 
87
        return self.dict[sha]
 
88
 
 
89
    def lookup_tree(self, path, revid):
 
90
        for k, v in self.dict.iteritems():
 
91
            if v == ("tree", (path, revid)):
 
92
                return k
 
93
        raise KeyError((path, revid))
 
94
 
 
95
    def revids(self):
 
96
        for key, (type, type_data) in self.dict.iteritems():
 
97
            if type == "commit":
 
98
                yield type_data[0]
 
99
 
 
100
 
 
101
class SqliteGitShaMap(GitShaMap):
 
102
 
 
103
    def __init__(self, transport=None):
52
104
        self.transport = transport
53
 
        self.db = sqlite3.connect(
54
 
            os.path.join(self.transport.local_abspath("."), "git.db"))
 
105
        if transport is None:
 
106
            self.db = sqlite3.connect(":memory:")
 
107
        else:
 
108
            self.db = sqlite3.connect(
 
109
                os.path.join(self.transport.local_abspath("."), "git.db"))
55
110
        self.db.executescript("""
56
111
        create table if not exists commits(sha1 text, revid text, tree_sha text);
57
112
        create index if not exists commit_sha1 on commits(sha1);
 
113
        create unique index if not exists commit_revid on commits(revid);
58
114
        create table if not exists blobs(sha1 text, fileid text, revid text);
59
115
        create index if not exists blobs_sha1 on blobs(sha1);
60
 
        create table if not exists trees(sha1 text, fileid text, revid text);
 
116
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
 
117
        create table if not exists trees(sha1 text, path text, revid text);
61
118
        create index if not exists trees_sha1 on trees(sha1);
 
119
        create unique index if not exists trees_path_revid on trees(path, revid);
62
120
""")
63
121
 
64
122
    def _parent_lookup(self, revid):
65
123
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
66
124
 
 
125
    def commit(self):
 
126
        self.db.commit()
 
127
 
67
128
    def add_entry(self, sha, type, type_data):
68
129
        """Add a new entry to the database.
69
130
        """
74
135
        elif type == "blob":
75
136
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
76
137
        elif type == "tree":
77
 
            self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
138
            self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
78
139
        else:
79
140
            raise AssertionError("Unknown type %s" % type)
80
141
 
 
142
    def lookup_tree(self, path, revid):
 
143
        row = self.db.execute("select sha1 from trees where path = ? and revid = ?", (path,revid)).fetchone()
 
144
        if row is None:
 
145
            raise KeyError((path, revid))
 
146
        return row[0]
 
147
 
81
148
    def lookup_git_sha(self, sha):
82
149
        """Lookup a Git sha in the database.
83
150
 
91
158
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
159
        if row is not None:
93
160
            return ("blob", row)
94
 
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
161
        row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
95
162
        if row is not None:
96
163
            return ("tree", row)
97
164
        raise KeyError(sha)
98
165
 
99
166
    def revids(self):
 
167
        """List the revision ids known."""
100
168
        for row in self.db.execute("select revid from commits").fetchall():
101
169
            yield row[0]