/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

Eliminate InventorySHAMap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
class InventorySHAMap(object):
79
79
    """Maps inventory file ids to Git SHAs."""
80
80
 
 
81
 
 
82
class GitShaMap(object):
 
83
    """Git<->Bzr revision id mapping database."""
 
84
 
 
85
    def _add_entry(self, sha, type, type_data):
 
86
        """Add a new entry to the database.
 
87
        """
 
88
        raise NotImplementedError(self._add_entry)
 
89
 
 
90
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha, 
 
91
                    entries):
 
92
        """Add multiple new entries to the database.
 
93
        """
 
94
        for (fileid, kind, hexsha, revision) in entries:
 
95
            self._add_entry(hexsha, kind, (fileid, revision))
 
96
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
 
97
 
 
98
    def lookup_git_sha(self, sha):
 
99
        """Lookup a Git sha in the database.
 
100
        :param sha: Git object sha
 
101
        :return: (type, type_data) with type_data:
 
102
            revision: revid, tree sha
 
103
        """
 
104
        raise NotImplementedError(self.lookup_git_sha)
 
105
 
81
106
    def lookup_blob_id(self, file_id, revision):
82
107
        """Retrieve a Git blob SHA by file id.
83
108
 
86
111
        """
87
112
        raise NotImplementedError(self.lookup_blob_id)
88
113
 
89
 
    def lookup_tree_id(self, file_id):
 
114
    def lookup_tree_id(self, file_id, revision):
90
115
        """Retrieve a Git tree SHA by file id.
91
116
        """
92
117
        raise NotImplementedError(self.lookup_tree_id)
93
118
 
94
 
 
95
 
class GitShaMap(object):
96
 
    """Git<->Bzr revision id mapping database."""
97
 
 
98
 
    def _add_entry(self, sha, type, type_data):
99
 
        """Add a new entry to the database.
100
 
        """
101
 
        raise NotImplementedError(self._add_entry)
102
 
 
103
 
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha, 
104
 
                    entries):
105
 
        """Add multiple new entries to the database.
106
 
        """
107
 
        for (fileid, kind, hexsha, revision) in entries:
108
 
            self._add_entry(hexsha, kind, (fileid, revision))
109
 
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
110
 
 
111
 
    def get_inventory_sha_map(self, revid):
112
 
        """Return the inventory SHA map for a revision.
113
 
 
114
 
        :param revid: Revision to fetch the map for
115
 
        :return: A `InventorySHAMap`
116
 
        """
117
 
        raise NotImplementedError(self.get_inventory_sha_map)
118
 
 
119
 
    def lookup_git_sha(self, sha):
120
 
        """Lookup a Git sha in the database.
121
 
        :param sha: Git object sha
122
 
        :return: (type, type_data) with type_data:
123
 
            revision: revid, tree sha
124
 
        """
125
 
        raise NotImplementedError(self.lookup_git_sha)
126
 
 
127
119
    def revids(self):
128
120
        """List the revision ids known."""
129
121
        raise NotImplementedError(self.revids)
160
152
        if type in ("blob", "tree"):
161
153
            self._by_fileid.setdefault(type_data[1], {})[type_data[0]] = sha
162
154
 
163
 
    def get_inventory_sha_map(self, revid):
164
 
 
165
 
        class DictInventorySHAMap(InventorySHAMap):
166
 
 
167
 
            def __init__(self, base, revid):
168
 
                self._base = base
169
 
                self.revid = revid
170
 
 
171
 
            def lookup_blob_id(self, fileid, revision):
172
 
                return self._base._by_fileid[revision][fileid]
173
 
 
174
 
            def lookup_tree_id(self, fileid):
175
 
                return self._base._by_fileid[self.revid][fileid]
176
 
 
177
 
        return DictInventorySHAMap(self, revid)
 
155
    def lookup_blob_id(self, fileid, revision):
 
156
        return self._by_fileid[revision][fileid]
178
157
 
179
158
    def lookup_git_sha(self, sha):
180
159
        return self._by_sha[sha]
181
160
 
 
161
    def lookup_tree_id(self, fileid, revision):
 
162
        return self._base._by_fileid[revision][fileid]
 
163
 
182
164
    def revids(self):
183
165
        for key, (type, type_data) in self._by_sha.iteritems():
184
166
            if type == "commit":
278
260
        else:
279
261
            raise AssertionError("Unknown type %s" % type)
280
262
 
281
 
    def get_inventory_sha_map(self, revid):
282
 
        class SqliteInventorySHAMap(InventorySHAMap):
283
 
 
284
 
            def __init__(self, db, revid):
285
 
                self.db = db
286
 
                self.revid = revid
287
 
 
288
 
            def lookup_blob_id(self, fileid, revision):
289
 
                row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
290
 
                if row is not None:
291
 
                    return row[0]
292
 
                raise KeyError(fileid)
293
 
 
294
 
            def lookup_tree_id(self, fileid):
295
 
                row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
296
 
                if row is not None:
297
 
                    return row[0]
298
 
                raise KeyError(fileid)
299
 
 
300
 
        return SqliteInventorySHAMap(self.db, revid)
 
263
    def lookup_blob_id(self, fileid, revision):
 
264
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
 
265
        if row is not None:
 
266
            return row[0]
 
267
        raise KeyError(fileid)
 
268
 
 
269
    def lookup_tree_id(self, fileid, revision):
 
270
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
 
271
        if row is not None:
 
272
            return row[0]
 
273
        raise KeyError(fileid)
301
274
 
302
275
    def lookup_git_sha(self, sha):
303
276
        """Lookup a Git sha in the database.
406
379
        elif type == "blob":
407
380
            self.db["\0".join(("blob", type_data[0], type_data[1]))] = sha
408
381
 
409
 
    def get_inventory_sha_map(self, revid):
410
 
 
411
 
        class TdbInventorySHAMap(InventorySHAMap):
412
 
 
413
 
            def __init__(self, db, revid):
414
 
                self.db = db
415
 
                self.revid = revid
416
 
 
417
 
            def lookup_blob_id(self, fileid, revision):
418
 
                return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
 
382
    def lookup_blob_id(self, fileid, revision):
 
383
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
419
384
                
420
 
        return TdbInventorySHAMap(self.db, revid)
421
 
 
422
385
    def lookup_git_sha(self, sha):
423
386
        """Lookup a Git sha in the database.
424
387