cache: rebuild branch cache from scratch when inconsistencies are detected
authorMads Kiilerich <madski@unity3d.com>
Sun, 13 Mar 2016 02:06:23 +0100
changeset 28558 bcd106d456c4
parent 28557 4b83507bfc91
child 28559 30863ca01c6b
cache: rebuild branch cache from scratch when inconsistencies are detected This should recover automatically from some corruptions that for unknown reasons are seen in the wild.
mercurial/branchmap.py
tests/test-branches.t
--- a/mercurial/branchmap.py	Sun Mar 13 02:06:22 2016 +0100
+++ b/mercurial/branchmap.py	Sun Mar 13 02:06:23 2016 +0100
@@ -383,6 +383,15 @@
         self._rbcnamescount = len(self._names) # number of good names on disk
         self._namesreverse = dict((b, r) for r, b in enumerate(self._names))
 
+    def _clear(self):
+        self._rbcsnameslen = 0
+        del self._names[:]
+        self._rbcnamescount = 0
+        self._namesreverse.clear()
+        self._rbcrevslen = len(self._repo.changelog)
+        self._rbcrevs = array('c')
+        self._rbcrevs.fromstring('\0' * (self._rbcrevslen * _rbcrecsize))
+
     def branchinfo(self, rev):
         """Return branch name and close flag for rev, using and updating
         persistent cache."""
@@ -408,7 +417,11 @@
         if cachenode == '\0\0\0\0':
             pass
         elif cachenode == reponode:
-            return self._names[branchidx], close
+            if branchidx < self._rbcnamescount:
+                return self._names[branchidx], close
+            # referenced branch doesn't exist - rebuild is expensive but needed
+            self._repo.ui.debug("rebuilding corrupted revision branch cache\n")
+            self._clear()
         else:
             # rev/node map has changed, invalidate the cache from here up
             truncate = rbcrevidx + _rbcrecsize
--- a/tests/test-branches.t	Sun Mar 13 02:06:22 2016 +0100
+++ b/tests/test-branches.t	Sun Mar 13 02:06:23 2016 +0100
@@ -629,4 +629,25 @@
   $ f --size .hg/cache/rbc-revs*
   .hg/cache/rbc-revs-v1: size=112
 
+cache is rebuilt when corruption is detected
+  $ echo > .hg/cache/rbc-names-v1
+  $ hg log -r '5:&branch(.)' -T '{rev} ' --debug
+  rebuilding corrupted revision branch cache
+  8 9 10 11 12 13 truncating cache/rbc-revs-v1 to 40
+  $ f --size --hexdump .hg/cache/rbc-*
+  .hg/cache/rbc-names-v1: size=79
+  0000: 62 00 61 00 63 00 61 20 62 72 61 6e 63 68 20 6e |b.a.c.a branch n|
+  0010: 61 6d 65 20 6d 75 63 68 20 6c 6f 6e 67 65 72 20 |ame much longer |
+  0020: 74 68 61 6e 20 74 68 65 20 64 65 66 61 75 6c 74 |than the default|
+  0030: 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 75 | justification u|
+  0040: 73 65 64 20 62 79 20 62 72 61 6e 63 68 65 73    |sed by branches|
+  .hg/cache/rbc-revs-v1: size=112
+  0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0020: 00 00 00 00 00 00 00 00 d8 cb c6 1d 00 00 00 01 |................|
+  0030: 58 97 36 a2 00 00 00 02 10 ff 58 95 00 00 00 03 |X.6.......X.....|
+  0040: ee bb 94 44 00 00 00 00 5f 40 61 bb 00 00 00 00 |...D...._@a.....|
+  0050: bf be 84 1b 00 00 00 00 d3 f1 63 45 80 00 00 00 |..........cE....|
+  0060: e3 d4 9c 05 80 00 00 00 e2 3b 55 05 00 00 00 00 |.........;U.....|
+
   $ cd ..