changeset 12758:2d754eae430c

tags: do not fail if tags.cache is corrupted (issue2444) This file is not critical for hg, so we can safely swallow the ValueError
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Tue, 19 Oct 2010 20:20:10 +0900
parents 62c8f7691bc3
children 9c5794223340
files mercurial/tags.py tests/test-tags.t
diffstat 2 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/tags.py	Sun Oct 10 09:50:25 2010 -0500
+++ b/mercurial/tags.py	Tue Oct 19 20:20:10 2010 +0900
@@ -176,16 +176,23 @@
     cacheheads = []                     # list of headnode
     cachefnode = {}                     # map headnode to filenode
     if cachefile:
-        for line in cachelines:
-            if line == "\n":
-                break
-            line = line.rstrip().split()
-            cacherevs.append(int(line[0]))
-            headnode = bin(line[1])
-            cacheheads.append(headnode)
-            if len(line) == 3:
-                fnode = bin(line[2])
-                cachefnode[headnode] = fnode
+        try:
+            for line in cachelines:
+                if line == "\n":
+                    break
+                line = line.rstrip().split()
+                cacherevs.append(int(line[0]))
+                headnode = bin(line[1])
+                cacheheads.append(headnode)
+                if len(line) == 3:
+                    fnode = bin(line[2])
+                    cachefnode[headnode] = fnode
+        except (ValueError, TypeError):
+            # corruption of tags.cache, just recompute it
+            ui.warn(_('.hg/tags.cache is corrupt, rebuilding it\n'))
+            cacheheads = []
+            cacherevs = []
+            cachefnode = {}
 
     tipnode = repo.changelog.tip()
     tiprev = len(repo.changelog) - 1
--- a/tests/test-tags.t	Sun Oct 10 09:50:25 2010 -0500
+++ b/tests/test-tags.t	Tue Oct 19 20:20:10 2010 +0900
@@ -34,6 +34,17 @@
   $ cacheexists
   tag cache exists
 
+Try corrupting the cache
+
+  $ echo 'a b\n' > .hg/tags.cache
+  $ hg identify
+  .hg/tags.cache is corrupt, rebuilding it
+  acb14030fe0a tip
+  $ cacheexists
+  tag cache exists
+  $ hg identify
+  acb14030fe0a tip
+
 Create local tag with long name:
 
   $ T=`hg identify --debug --id`