/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 DictGitShaMap, useful for testing.

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
    """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
        ret = []
 
87
        for key, (type, type_data) in self.dict.iteritems():
 
88
            if type == "commit":
 
89
                ret.append(type_data[0])
 
90
        return ret
 
91
 
 
92
 
 
93
class SqliteGitShaMap(GitShaMap):
50
94
 
51
95
    def __init__(self, transport):
52
96
        self.transport = transport
57
101
        create index if not exists commit_sha1 on commits(sha1);
58
102
        create table if not exists blobs(sha1 text, fileid text, revid text);
59
103
        create index if not exists blobs_sha1 on blobs(sha1);
60
 
        create table if not exists trees(sha1 text, fileid text, revid text);
 
104
        create table if not exists trees(sha1 text, path text, revid text);
61
105
        create index if not exists trees_sha1 on trees(sha1);
62
106
""")
63
107
 
64
108
    def _parent_lookup(self, revid):
65
109
        return self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()[0].encode("utf-8")
66
110
 
 
111
    def commit(self):
 
112
        self.db.commit()
 
113
 
67
114
    def add_entry(self, sha, type, type_data):
68
115
        """Add a new entry to the database.
69
116
        """
74
121
        elif type == "blob":
75
122
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
76
123
        elif type == "tree":
77
 
            self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
124
            self.db.execute("replace into trees (sha1, path, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
78
125
        else:
79
126
            raise AssertionError("Unknown type %s" % type)
80
127
 
91
138
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
92
139
        if row is not None:
93
140
            return ("blob", row)
94
 
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
141
        row = self.db.execute("select path, revid from trees where sha1 = ?", (sha,)).fetchone()
95
142
        if row is not None:
96
143
            return ("tree", row)
97
144
        raise KeyError(sha)
98
145
 
99
146
    def revids(self):
 
147
        """List the revision ids known."""
100
148
        for row in self.db.execute("select revid from commits").fetchall():
101
149
            yield row[0]