/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 cache.py

Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
        """Lookup a Git sha in the database.
102
102
        :param sha: Git object sha
103
103
        :return: (type, type_data) with type_data:
104
 
            revision: revid, tree sha
 
104
            commit: revid, tree_sha, verifiers
 
105
            blob: fileid, revid
 
106
            tree: fileid, revid
105
107
        """
106
108
        raise NotImplementedError(self.lookup_git_sha)
107
109
 
220
222
    """Base class for objects that can update a bzr-git cache."""
221
223
 
222
224
    def add_object(self, obj, ie, path):
 
225
        """Add an object.
 
226
 
 
227
        :param obj: Object type ("commit", "blob" or "tree")
 
228
        :param ie: Inventory entry (for blob/tree) or testament_sha in case
 
229
            of commit
 
230
        :param path: Path of the object (optional)
 
231
        """
223
232
        raise NotImplementedError(self.add_object)
224
233
 
225
234
    def finish(self):
257
266
    def add_object(self, obj, ie, path):
258
267
        if obj.type_name == "commit":
259
268
            self._commit = obj
260
 
            assert ie is None
261
 
            type_data = (self.revid, self._commit.tree)
 
269
            assert type(ie) is dict
 
270
            type_data = (self.revid, self._commit.tree, ie)
262
271
            self.cache.idmap._by_revid[self.revid] = obj.id
263
272
        elif obj.type_name in ("blob", "tree"):
264
273
            if ie is not None:
267
276
                else:
268
277
                    revision = self.revid
269
278
                type_data = (ie.file_id, revision)
270
 
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] =\
271
 
                    obj.id
 
279
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
272
280
        else:
273
281
            raise AssertionError
274
282
        self.cache.idmap._by_sha[obj.id] = (obj.type_name, type_data)
321
329
    def add_object(self, obj, ie, path):
322
330
        if obj.type_name == "commit":
323
331
            self._commit = obj
324
 
            assert ie is None
 
332
            self._testament3_sha1 = ie["testament3-sha1"]
 
333
            assert type(ie) is dict
325
334
        elif obj.type_name == "tree":
326
335
            if ie is not None:
327
336
                self._trees.append((obj.id, ie.file_id, self.revid))
341
350
            "replace into blobs (sha1, fileid, revid) values (?, ?, ?)",
342
351
            self._blobs)
343
352
        self.db.execute(
344
 
            "replace into commits (sha1, revid, tree_sha) values (?, ?, ?)",
345
 
            (self._commit.id, self.revid, self._commit.tree))
 
353
            "replace into commits (sha1, revid, tree_sha, testament3_sha1) values (?, ?, ?, ?)",
 
354
            (self._commit.id, self.revid, self._commit.tree, self._testament3_sha1))
346
355
        return self._commit
347
356
 
348
357
 
397
406
        create unique index if not exists trees_sha1 on trees(sha1);
398
407
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
399
408
""")
 
409
        try:
 
410
            self.db.executescript(
 
411
                "ALTER TABLE commits ADD testament3_sha1 TEXT;")
 
412
        except sqlite3.OperationalError:
 
413
            pass # Column already exists.
400
414
 
401
415
    def __repr__(self):
402
416
        return "%s(%r)" % (self.__class__.__name__, self.path)
403
 
    
 
417
 
404
418
    def lookup_commit(self, revid):
405
419
        cursor = self.db.execute("select sha1 from commits where revid = ?", 
406
420
            (revid,))
429
443
 
430
444
        :param sha: Git object sha
431
445
        :return: (type, type_data) with type_data:
432
 
            revision: revid, tree sha
 
446
            commit: revid, tree sha, verifiers
 
447
            tree: fileid, revid
 
448
            blob: fileid, revid
433
449
        """
434
 
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
 
450
        row = self.db.execute("select revid, tree_sha, testament3_sha1 from commits where sha1 = ?", (sha,)).fetchone()
435
451
        if row is not None:
436
 
            return ("commit", row)
 
452
            return ("commit", (row[0], row[1], {"testament3-sha1": row[2]}))
437
453
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
438
454
        if row is not None:
439
455
            return ("blob", row)
468
484
        sha = obj.sha().digest()
469
485
        if obj.type_name == "commit":
470
486
            self.db["commit\0" + self.revid] = "\0".join((sha, obj.tree))
471
 
            type_data = (self.revid, obj.tree)
 
487
            assert type(ie) is dict, "was %r" % ie
 
488
            type_data = (self.revid, obj.tree, ie["testament3-sha1"])
472
489
            self._commit = obj
473
 
            assert ie is None
474
490
        elif obj.type_name == "blob":
475
491
            if ie is None:
476
492
                return
564
580
 
565
581
    def lookup_blob_id(self, fileid, revision):
566
582
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
567
 
                
 
583
 
568
584
    def lookup_git_sha(self, sha):
569
585
        """Lookup a Git sha in the database.
570
586
 
571
587
        :param sha: Git object sha
572
588
        :return: (type, type_data) with type_data:
573
 
            revision: revid, tree sha
 
589
            commit: revid, tree sha
 
590
            blob: fileid, revid
 
591
            tree: fileid, revid
574
592
        """
575
593
        if len(sha) == 40:
576
594
            sha = hex_to_sha(sha)
577
595
        data = self.db["git\0" + sha].split("\0")
578
 
        return (data[0], (data[1], data[2]))
 
596
        if data[0] == "commit":
 
597
            if len(data) == 3:
 
598
                return (data[0], (data[1], data[2], {}))
 
599
            else:
 
600
                return (data[0], (data[1], data[2], {"testament3-sha1": data[3]}))
 
601
        else:
 
602
            return (data[0], tuple(data[1:]))
579
603
 
580
604
    def missing_revisions(self, revids):
581
605
        ret = set()
643
667
    def add_object(self, obj, ie, path):
644
668
        if obj.type_name == "commit":
645
669
            self._commit = obj
646
 
            assert ie is None
 
670
            assert type(ie) is dict
647
671
            self.cache.idmap._add_git_sha(obj.id, "commit",
648
 
                (self.revid, obj.tree))
 
672
                (self.revid, obj.tree, ie))
649
673
            self.cache.idmap._add_node(("commit", self.revid, "X"),
650
674
                " ".join((obj.id, obj.tree)))
651
675
            self._cache_objs.add((obj, path))
811
835
    def _add_git_sha(self, hexsha, type, type_data):
812
836
        if hexsha is not None:
813
837
            self._name.update(hexsha)
814
 
            self._add_node(("git", hexsha, "X"),
815
 
                " ".join((type, type_data[0], type_data[1])))
 
838
            if type == "commit":
 
839
                td = (type_data[0], type_data[1], type_data[2]["testament3-sha1"])
 
840
            else:
 
841
                td = type_data
 
842
            self._add_node(("git", hexsha, "X"), " ".join((type,) + td))
816
843
        else:
817
844
            # This object is not represented in Git - perhaps an empty
818
845
            # directory?
824
851
    def lookup_git_sha(self, sha):
825
852
        if len(sha) == 20:
826
853
            sha = sha_to_hex(sha)
827
 
        data = self._get_entry(("git", sha, "X")).split(" ", 2)
828
 
        return (data[0], (data[1], data[2]))
 
854
        data = self._get_entry(("git", sha, "X")).split(" ", 3)
 
855
        if data[0] == "commit":
 
856
            return ("commit", (data[1], data[2], {"testament3-sha1": data[3]}))
 
857
        else:
 
858
            return (data[0], tuple(data[1:]))
829
859
 
830
860
    def revids(self):
831
861
        """List the revision ids known."""