sparse: clean up path handling, making it platform agnostic

Summary:
There is no need to strip path separators from the normalised path; normpath
will never leave any.

We also ensure that the path prefix we test ends in a path separator, to avoid
matching on sibling paths that happen to share a prefix.

Reviewed By: ryanmce

Differential Revision: D7056649

fbshipit-source-id: 10b78a78ba44fbc8d9c05fb7ffd0ffd1c1496a67
This commit is contained in:
Martijn Pieters 2018-02-27 11:13:59 -08:00 committed by Saurabh Singh
parent 73568fd07d
commit 42ac84e8cf
3 changed files with 25 additions and 15 deletions

View File

@ -9,7 +9,7 @@
"""
from mercurial import util, cmdutil, extensions, context, dirstate, commands
from mercurial import localrepo, error, hg, pathutil, registrar, patch
from mercurial import localrepo, error, hg, pathutil, registrar, patch, pycompat
from mercurial import match as matchmod
from mercurial import merge as mergemod
from mercurial.node import nullid
@ -1202,26 +1202,26 @@ def _cwdlist(repo):
"""
ctx = repo['.']
mf = ctx.manifest()
cwd = util.normpath(os.getcwd())
# Get the root of the repo so that we remove the content of
# the root from the current working directory
root = repo.root
if cwd.startswith(root):
cwd = cwd[len(root):]
else:
raise error.Abort(_("the current working directory should begin " +
"with the root %s") % root)
cwd = util.normpath(pycompat.getcwd())
cwd = os.path.relpath(cwd, root)
cwd = '' if cwd == os.curdir else cwd + pycompat.ossep
if cwd.startswith(os.pardir + pycompat.ossep):
raise error.Abort(
_("the current working directory should begin "
"with the root %s") % root)
cwd = cwd.strip("/")
sparsematch = repo.sparsematch(ctx.rev())
checkedoutentries = set()
allentries = set()
cwdlength = len(cwd) + 1
cwdlength = len(cwd)
for filepath in mf:
if filepath.startswith(cwd):
tail = filepath[cwdlength:] if cwdlength > 1 else filepath
entryname = tail.split('/', 1)[0]
entryname = filepath[cwdlength:].partition(pycompat.ossep)[0]
allentries.add(entryname)
if sparsematch(filepath):

View File

@ -95,9 +95,6 @@ New errors are not allowed. Warnings are strongly discouraged.
hgext/fastannotate/commands.py:43:
> reldir = os.path.relpath(os.getcwd(), reporoot)
use pycompat.getcwd instead (py3)
hgext/fbsparse.py:1205:
> cwd = util.normpath(os.getcwd())
use pycompat.getcwd instead (py3)
Skipping hgext/hgsql.py it has no-che?k-code (glob)
Skipping hgext/hgsubversion/__init__.py it has no-che?k-code (glob)
Skipping hgext/hgsubversion/compathacks.py it has no-che?k-code (glob)

View File

@ -354,7 +354,20 @@ Test --cwd-list
$ hg sparse --cwd-list
- bar
foo
$ cd ..
Make sure to match whole directory names, not prefixes
$ mkdir prefix prefixpostfix
$ touch prefix/correct prefixpostfix/incorrect
$ hg sparse -I prefix prefixpostfix
$ hg addremove .
adding prefix/correct
adding prefixpostfix/incorrect
$ hg ci -m 'subdirs'
$ cd prefix
$ hg sparse --cwd-list
correct
$ cd ../..
$ cd ..