subrepo: normalize path in the specific way for problematic encodings stable
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Thu, 08 May 2014 19:03:00 +0900
branchstable
changeset 21568 8dd17b19e722
parent 21567 5900bc09e684
child 21569 c5afb07c33d3
subrepo: normalize path in the specific way for problematic encodings Before this patch, "reporelpath()" uses "rstrip(os.sep)" to trim "os.sep" at the end of "parent.root" path. But it doesn't work correctly with some problematic encodings on Windows, because some multi-byte characters in such encodings contain '\\' (0x5c) as the tail byte of them. In such cases, "reporelpath()" leaves unexpected '\\' at the beginning of the path returned to callers. "lcalrepository.root" seems not to have tail "os.sep", because it is always normalized by "os.path.realpath()" in "vfs.__init__()", but in fact it has tail "os.sep", if it is a root (of the drive): path normalization trims tail "os.sep" off "/foo/bar/", but doesn't trim one off "/". So, just avoiding "rstrip(os.sep)" in "reporelpath()" causes regression around issue3033 fixed by fccd350acf79. This patch introduces "pathutil.normasprefix" to normalize specified path in the specific way for problematic encodings without regression around issue3033.
mercurial/pathutil.py
mercurial/subrepo.py
tests/test-doctest.py
--- a/mercurial/pathutil.py	Thu May 08 19:03:00 2014 +0900
+++ b/mercurial/pathutil.py	Thu May 08 19:03:00 2014 +0900
@@ -142,3 +142,25 @@
             name = dirname
 
         raise util.Abort(_("%s not under root '%s'") % (myname, root))
+
+def normasprefix(path):
+    '''normalize the specified path as path prefix
+
+    Returned vaule can be used safely for "p.startswith(prefix)",
+    "p[len(prefix):]", and so on.
+
+    For efficiency, this expects "path" argument to be already
+    normalized by "os.path.normpath", "os.path.realpath", and so on.
+
+    See also issue3033 for detail about need of this function.
+
+    >>> normasprefix('/foo/bar').replace(os.sep, '/')
+    '/foo/bar/'
+    >>> normasprefix('/').replace(os.sep, '/')
+    '/'
+    '''
+    d, p = os.path.splitdrive(path)
+    if len(p) != len(os.sep):
+        return path + os.sep
+    else:
+        return path
--- a/mercurial/subrepo.py	Thu May 08 19:03:00 2014 +0900
+++ b/mercurial/subrepo.py	Thu May 08 19:03:00 2014 +0900
@@ -276,8 +276,7 @@
     parent = repo
     while util.safehasattr(parent, '_subparent'):
         parent = parent._subparent
-    p = parent.root.rstrip(os.sep)
-    return repo.root[len(p) + 1:]
+    return repo.root[len(pathutil.normasprefix(parent.root)):]
 
 def subrelpath(sub):
     """return path to this subrepo as seen from outermost repo"""
--- a/tests/test-doctest.py	Thu May 08 19:03:00 2014 +0900
+++ b/tests/test-doctest.py	Thu May 08 19:03:00 2014 +0900
@@ -19,6 +19,7 @@
 testmod('mercurial.hgweb.hgwebdir_mod')
 testmod('mercurial.match')
 testmod('mercurial.minirst')
+testmod('mercurial.pathutil')
 testmod('mercurial.revset')
 testmod('mercurial.store')
 testmod('mercurial.subrepo')