/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

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
 
56
56
 
57
57
def get_remote_cache_transport():
 
58
    """Retrieve the transport to use when accessing (unwritable) remote 
 
59
    repositories.
 
60
    """
58
61
    return get_transport(get_cache_dir())
59
62
 
60
63
 
98
101
        """Lookup a Git sha in the database.
99
102
        :param sha: Git object sha
100
103
        :return: (type, type_data) with type_data:
101
 
            revision: revid, tree sha
 
104
            commit: revid, tree_sha, verifiers
 
105
            blob: fileid, revid
 
106
            tree: fileid, revid
102
107
        """
103
108
        raise NotImplementedError(self.lookup_git_sha)
104
109
 
217
222
    """Base class for objects that can update a bzr-git cache."""
218
223
 
219
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
        """
220
232
        raise NotImplementedError(self.add_object)
221
233
 
222
234
    def finish(self):
254
266
    def add_object(self, obj, ie, path):
255
267
        if obj.type_name == "commit":
256
268
            self._commit = obj
257
 
            assert ie is None
258
 
            type_data = (self.revid, self._commit.tree)
 
269
            assert type(ie) is dict
 
270
            type_data = (self.revid, self._commit.tree, ie)
259
271
            self.cache.idmap._by_revid[self.revid] = obj.id
260
272
        elif obj.type_name in ("blob", "tree"):
261
273
            if ie is not None:
264
276
                else:
265
277
                    revision = self.revid
266
278
                type_data = (ie.file_id, revision)
267
 
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] =\
268
 
                    obj.id
 
279
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
269
280
        else:
270
281
            raise AssertionError
271
282
        self.cache.idmap._by_sha[obj.id] = (obj.type_name, type_data)
318
329
    def add_object(self, obj, ie, path):
319
330
        if obj.type_name == "commit":
320
331
            self._commit = obj
321
 
            assert ie is None
 
332
            self._testament3_sha1 = ie["testament3-sha1"]
 
333
            assert type(ie) is dict
322
334
        elif obj.type_name == "tree":
323
335
            if ie is not None:
324
336
                self._trees.append((obj.id, ie.file_id, self.revid))
338
350
            "replace into blobs (sha1, fileid, revid) values (?, ?, ?)",
339
351
            self._blobs)
340
352
        self.db.execute(
341
 
            "replace into commits (sha1, revid, tree_sha) values (?, ?, ?)",
342
 
            (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))
343
355
        return self._commit
344
356
 
345
357
 
394
406
        create unique index if not exists trees_sha1 on trees(sha1);
395
407
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
396
408
""")
 
409
        try:
 
410
            self.db.executescript(
 
411
                "ALTER TABLE commits ADD testament3_sha1 TEXT;")
 
412
        except sqlite3.OperationalError:
 
413
            pass # Column already exists.
397
414
 
398
415
    def __repr__(self):
399
416
        return "%s(%r)" % (self.__class__.__name__, self.path)
400
 
    
 
417
 
401
418
    def lookup_commit(self, revid):
402
419
        cursor = self.db.execute("select sha1 from commits where revid = ?", 
403
420
            (revid,))
426
443
 
427
444
        :param sha: Git object sha
428
445
        :return: (type, type_data) with type_data:
429
 
            revision: revid, tree sha
 
446
            commit: revid, tree sha, verifiers
 
447
            tree: fileid, revid
 
448
            blob: fileid, revid
430
449
        """
431
 
        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()
432
451
        if row is not None:
433
 
            return ("commit", row)
 
452
            return ("commit", (row[0], row[1], {"testament3-sha1": row[2]}))
434
453
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
435
454
        if row is not None:
436
455
            return ("blob", row)
465
484
        sha = obj.sha().digest()
466
485
        if obj.type_name == "commit":
467
486
            self.db["commit\0" + self.revid] = "\0".join((sha, obj.tree))
468
 
            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"])
469
489
            self._commit = obj
470
 
            assert ie is None
471
490
        elif obj.type_name == "blob":
472
491
            if ie is None:
473
492
                return
561
580
 
562
581
    def lookup_blob_id(self, fileid, revision):
563
582
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
564
 
                
 
583
 
565
584
    def lookup_git_sha(self, sha):
566
585
        """Lookup a Git sha in the database.
567
586
 
568
587
        :param sha: Git object sha
569
588
        :return: (type, type_data) with type_data:
570
 
            revision: revid, tree sha
 
589
            commit: revid, tree sha
 
590
            blob: fileid, revid
 
591
            tree: fileid, revid
571
592
        """
572
593
        if len(sha) == 40:
573
594
            sha = hex_to_sha(sha)
574
595
        data = self.db["git\0" + sha].split("\0")
575
 
        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:]))
576
603
 
577
604
    def missing_revisions(self, revids):
578
605
        ret = set()
640
667
    def add_object(self, obj, ie, path):
641
668
        if obj.type_name == "commit":
642
669
            self._commit = obj
643
 
            assert ie is None
 
670
            assert type(ie) is dict
644
671
            self.cache.idmap._add_git_sha(obj.id, "commit",
645
 
                (self.revid, obj.tree))
 
672
                (self.revid, obj.tree, ie))
646
673
            self.cache.idmap._add_node(("commit", self.revid, "X"),
647
674
                " ".join((obj.id, obj.tree)))
648
675
            self._cache_objs.add((obj, path))
808
835
    def _add_git_sha(self, hexsha, type, type_data):
809
836
        if hexsha is not None:
810
837
            self._name.update(hexsha)
811
 
            self._add_node(("git", hexsha, "X"),
812
 
                " ".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))
813
843
        else:
814
844
            # This object is not represented in Git - perhaps an empty
815
845
            # directory?
821
851
    def lookup_git_sha(self, sha):
822
852
        if len(sha) == 20:
823
853
            sha = sha_to_hex(sha)
824
 
        data = self._get_entry(("git", sha, "X")).split(" ", 2)
825
 
        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:]))
826
859
 
827
860
    def revids(self):
828
861
        """List the revision ids known."""