Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/pathutil.py @ 21568:8dd17b19e722 stable
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.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Thu, 08 May 2014 19:03:00 +0900 |
parents | f962870712da |
children | e53f6b72a0e4 c02a05cc6f5e |
comparison
equal
deleted
inserted
replaced
21567:5900bc09e684 | 21568:8dd17b19e722 |
---|---|
140 if dirname == name: | 140 if dirname == name: |
141 break | 141 break |
142 name = dirname | 142 name = dirname |
143 | 143 |
144 raise util.Abort(_("%s not under root '%s'") % (myname, root)) | 144 raise util.Abort(_("%s not under root '%s'") % (myname, root)) |
145 | |
146 def normasprefix(path): | |
147 '''normalize the specified path as path prefix | |
148 | |
149 Returned vaule can be used safely for "p.startswith(prefix)", | |
150 "p[len(prefix):]", and so on. | |
151 | |
152 For efficiency, this expects "path" argument to be already | |
153 normalized by "os.path.normpath", "os.path.realpath", and so on. | |
154 | |
155 See also issue3033 for detail about need of this function. | |
156 | |
157 >>> normasprefix('/foo/bar').replace(os.sep, '/') | |
158 '/foo/bar/' | |
159 >>> normasprefix('/').replace(os.sep, '/') | |
160 '/' | |
161 ''' | |
162 d, p = os.path.splitdrive(path) | |
163 if len(p) != len(os.sep): | |
164 return path + os.sep | |
165 else: | |
166 return path |