changeset 13425:0fe36c347c00

bookmarks: forbid \0 \r \n : in bookmark names (BC) We restrict : to 1. make it easer to convert bookmarks to git branches, 2. use : later for a syntax to push a local bookmark to a remote bookmark of a different name. \0, \n, \r are fobbidden they are used to separate bookmarks in the bookmark file. This change breaks backward compatbility as ':' was an allowed character in previous versions.
author David Soria Parra <dsp@php.net>
date Wed, 16 Feb 2011 18:36:45 +0100
parents 08f9c587141f
children 643b8212813e
files mercurial/bookmarks.py tests/test-bookmarks.t
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bookmarks.py	Wed Feb 16 04:36:36 2011 +0100
+++ b/mercurial/bookmarks.py	Wed Feb 16 18:36:45 2011 +0100
@@ -7,9 +7,15 @@
 
 from mercurial.i18n import _
 from mercurial.node import nullid, nullrev, bin, hex, short
-from mercurial import encoding
+from mercurial import encoding, util
 import os
 
+def valid(mark):
+    for c in (':', '\0', '\n', '\r'):
+        if c in mark:
+            return False
+    return True
+
 def read(repo):
     '''Parse .hg/bookmarks file and return a dictionary
 
@@ -63,8 +69,14 @@
 
     if repo._bookmarkcurrent not in refs:
         setcurrent(repo, None)
+    for mark in refs.keys():
+        if not valid(mark):
+            raise util.Abort(_("bookmark '%s' contains illegal "
+                "character" % mark))
+
     wlock = repo.wlock()
     try:
+
         file = repo.opener('bookmarks', 'w', atomictemp=True)
         for refspec, node in refs.iteritems():
             file.write("%s %s\n" % (hex(node), encoding.fromlocal(refspec)))
@@ -97,6 +109,10 @@
         return
     if mark not in refs:
         mark = ''
+    if not valid(mark):
+        raise util.Abort(_("bookmark '%s' contains illegal "
+            "character" % mark))
+
     wlock = repo.wlock()
     try:
         file = repo.opener('bookmarks.current', 'w', atomictemp=True)
--- a/tests/test-bookmarks.t	Wed Feb 16 04:36:36 2011 +0100
+++ b/tests/test-bookmarks.t	Wed Feb 16 18:36:45 2011 +0100
@@ -212,3 +212,10 @@
   $ hg bookmark ' '
   abort: bookmark names cannot consist entirely of whitespace
   [255]
+
+invalid bookmark
+
+  $ hg bookmark 'foo:bar'
+  abort: bookmark 'foo:bar' contains illegal character
+  [255]
+