mercurial/branching/rev_cache.py
changeset 51903 9f7cf869e9f4
parent 51902 1eb2317c1762
child 51904 7032da075572
--- a/mercurial/branching/rev_cache.py	Tue Sep 24 00:01:30 2024 +0200
+++ b/mercurial/branching/rev_cache.py	Mon Sep 23 23:52:45 2024 +0200
@@ -170,6 +170,7 @@
             self._names = []
         self._rbcnamescount = len(self._names)  # number of names read at
         # _rbcsnameslen
+        self._force_overwrite = False
 
     def _clear(self):
         self._rbcsnameslen = 0
@@ -178,6 +179,12 @@
         self._rbcrevslen = len(self._repo.changelog)
         self._rbcrevs = rbcrevs(bytearray(self._rbcrevslen * _rbcrecsize))
         util.clearcachedproperty(self, b'_namesreverse')
+        self._force_overwrite = True
+
+    def invalidate(self, rev=0):
+        self._rbcrevslen = rev
+        self._rbcrevs.truncate(rev)
+        self._force_overwrite = True
 
     @util.propertycache
     def _namesreverse(self):
@@ -292,7 +299,7 @@
 
             # write the new revs
             start = self._rbcrevslen * _rbcrecsize
-            if start != len(self._rbcrevs):
+            if self._force_overwrite or start != len(self._rbcrevs):
                 step = b''
                 if wlock is None:
                     wlock = repo.wlock(wait=False)
@@ -335,6 +342,8 @@
     def _writerevs(self, repo, start):
         """write the new revs to revbranchcache"""
         revs = min(len(repo.changelog), len(self._rbcrevs) // _rbcrecsize)
+        if self._force_overwrite:
+            start = 0
         with repo.cachevfs.open(_rbcrevs, b'ab') as f:
             current_size = f.tell()
             if current_size < start:
@@ -348,3 +357,4 @@
             end = revs * _rbcrecsize
             f.write(self._rbcrevs.slice(start, end))
         self._rbcrevslen = revs
+        self._force_overwrite = False