bookmarks: explicitly convert to 'node' during initialization
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 07 Jun 2017 22:26:43 +0100
changeset 32735 d7522f983f37
parent 32734 b5613bda454e
child 32736 173f1bdc322d
bookmarks: explicitly convert to 'node' during initialization We know the content of the file is supposed to be full hex. So we can do the translation ourselves and directly check if the node is known. As nice side effect we now have proper error handling for invalid node value. Before: ! wall 0.021580 comb 0.020000 user 0.020000 sys 0.000000 (best of 134) After: ! wall 0.009342 comb 0.010000 user 0.010000 sys 0.000000 (best of 302)
mercurial/bookmarks.py
tests/test-bookmarks.t
--- a/mercurial/bookmarks.py	Wed Jun 07 19:21:02 2017 +0100
+++ b/mercurial/bookmarks.py	Wed Jun 07 22:26:43 2017 +0100
@@ -50,7 +50,8 @@
     def __init__(self, repo):
         dict.__init__(self)
         self._repo = repo
-        lookup = repo.changelog.lookup
+        nm = repo.changelog.nodemap
+        tonode = bin # force local lookup
         try:
             bkfile = _getbkfile(repo)
             for line in bkfile:
@@ -62,11 +63,16 @@
                                  % line)
                     continue
                 sha, refspec = line.split(' ', 1)
-                refspec = encoding.tolocal(refspec)
                 try:
-                    self[refspec] = lookup(sha)
-                except LookupError:
-                    pass
+                    node = tonode(sha)
+                    if node in nm:
+                        refspec = encoding.tolocal(refspec)
+                        self[refspec] = node
+                except (TypeError, ValueError):
+                    # - bin(...) can raise TypeError
+                    # - node in nm can raise ValueError for non-20-bytes entry
+                    repo.ui.warn(_('malformed line in .hg/bookmarks: %r\n')
+                                 % line)
         except IOError as inst:
             if inst.errno != errno.ENOENT:
                 raise
--- a/tests/test-bookmarks.t	Wed Jun 07 19:21:02 2017 +0100
+++ b/tests/test-bookmarks.t	Wed Jun 07 22:26:43 2017 +0100
@@ -700,7 +700,7 @@
 
 test missing revisions
 
-  $ echo "925d80f479bc z" > .hg/bookmarks
+  $ echo "925d80f479b925d80f479bc925d80f479bccabab z" > .hg/bookmarks
   $ hg book
   no bookmarks set