diff --git a/mercurial/branching/rev_cache.py b/mercurial/branching/rev_cache.py --- a/mercurial/branching/rev_cache.py +++ b/mercurial/branching/rev_cache.py @@ -397,11 +397,15 @@ class revbranchcache: start = 0 if current_size != start: threshold = current_size * REWRITE_RATIO - if (max(end, current_size) - start) < threshold: - # end affected, let overwrite the bad value - overwritten = min(end, current_size) - start + overwritten = min(end, current_size) - start + if (max(end, current_size) - start) >= threshold: + start = 0 + dbg = b"resetting content of cache/%s\n" % _rbcrevs + repo.ui.debug(dbg) + elif overwritten > 0: + # end affected, let us overwrite the bad value dbg = b"overwriting %d bytes from %d in cache/%s" - dbg %= (overwritten, start, _rbcrevs) + dbg %= (current_size - start, start, _rbcrevs) if end < current_size: extra = b" leaving (%d trailing bytes)" extra %= current_size - end @@ -409,9 +413,12 @@ class revbranchcache: dbg += b'\n' repo.ui.debug(dbg) else: - start = 0 - dbg = b"resetting content of cache/%s\n" % _rbcrevs + # extra untouched data at the end, lets warn about them + assert start == end # since don't write anything + dbg = b"cache/%s contains %d unknown trailing bytes\n" + dbg %= (_rbcrevs, current_size - start) repo.ui.debug(dbg) + if start > 0: f.seek(start) f.write(self._rbcrevs.slice(start, end)) diff --git a/tests/test-branches.t b/tests/test-branches.t --- a/tests/test-branches.t +++ b/tests/test-branches.t @@ -835,7 +835,7 @@ recovery from invalid cache revs file wi $ echo >> .hg/cache/rbc-revs-v2 $ rm -f .hg/cache/branch* && hg head a -T '{rev}\n' --debug 5 - overwriting 0 bytes from 160 in cache/rbc-revs-v2 leaving (2 trailing bytes) + cache/rbc-revs-v2 contains 2 unknown trailing bytes $ f --size .hg/cache/rbc-revs* .hg/cache/rbc-revs-v2: size=162