author | Martin von Zweigbergk <martinvonz@google.com> |
Mon, 21 Sep 2020 10:09:39 -0700 | |
changeset 45562 | 03726f5b6092 |
parent 44924 | 233ee525dcef |
child 45957 | 89a2afe31e82 |
permissions | -rw-r--r-- |
25964
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
1 |
from __future__ import absolute_import |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
2 |
|
44924
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
3 |
import contextlib |
25964
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
4 |
import errno |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
5 |
import os |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
6 |
import posixpath |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
7 |
import stat |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
8 |
|
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
9 |
from .i18n import _ |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
10 |
from . import ( |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
11 |
encoding, |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25964
diff
changeset
|
12 |
error, |
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
13 |
policy, |
30619
cfe66dcf45c0
py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30342
diff
changeset
|
14 |
pycompat, |
25964
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
15 |
util, |
d740df4e96cf
pathutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
16 |
) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
17 |
|
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
18 |
rustdirs = policy.importrust('dirstate', 'Dirs') |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
19 |
parsers = policy.importmod('parsers') |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
20 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
21 |
|
23598
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
22 |
def _lowerclean(s): |
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
23 |
return encoding.hfsignoreclean(s.lower()) |
c02a05cc6f5e
pathauditor: check for codepoints ignored on OS X
Augie Fackler <raf@durin42.com>
parents:
21568
diff
changeset
|
24 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
25 |
|
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
26 |
class pathauditor(object): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
27 |
'''ensure that a filesystem path contains no banned components. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
28 |
the following properties of a path are checked: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
29 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
30 |
- ends with a directory separator |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
31 |
- under top-level .hg |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
32 |
- starts at the root of a windows drive |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
33 |
- contains ".." |
27232
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
34 |
|
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
35 |
More check are also done about the file system states: |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
36 |
- traverses a symlink (e.g. a/symlink_here/b) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
37 |
- inside a nested repository (a callback can be used to approve |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
38 |
some nested repositories, e.g., subrepositories) |
27232
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
39 |
|
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
40 |
The file system checks are only done when 'realfs' is set to True (the |
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
41 |
default). They should be disable then we are auditing path for operation on |
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
42 |
stored history. |
33647
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
43 |
|
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
44 |
If 'cached' is set to True, audited paths and sub-directories are cached. |
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
45 |
Be careful to not keep the cache of unmanaged directories for long because |
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
46 |
audited paths may be replaced with symlinks. |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
47 |
''' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
48 |
|
33647
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
49 |
def __init__(self, root, callback=None, realfs=True, cached=False): |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
50 |
self.audited = set() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
51 |
self.auditeddir = set() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
52 |
self.root = root |
27232
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
53 |
self._realfs = realfs |
33647
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
54 |
self._cached = cached |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
55 |
self.callback = callback |
29893
6f447b9ec263
util: rename checkcase() to fscasesensitive() (API)
Martin von Zweigbergk <martinvonz@google.com>
parents:
28087
diff
changeset
|
56 |
if os.path.lexists(root) and not util.fscasesensitive(root): |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
57 |
self.normcase = util.normcase |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
58 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
59 |
self.normcase = lambda x: x |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
60 |
|
33435
456626e9c3d1
vfs: allow to pass more argument to audit
Boris Feld <boris.feld@octobus.net>
parents:
30619
diff
changeset
|
61 |
def __call__(self, path, mode=None): |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
62 |
'''Check the relative path. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
63 |
path may contain a pattern (e.g. foodir/**.txt)''' |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
64 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
65 |
path = util.localpath(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
66 |
normpath = self.normcase(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
67 |
if normpath in self.audited: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
68 |
return |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
69 |
# AIX ignores "/" at end of path, others raise EISDIR. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
70 |
if util.endswithsep(path): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
71 |
raise error.Abort(_(b"path ends in directory separator: %s") % path) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
72 |
parts = util.splitpath(path) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
73 |
if ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
74 |
os.path.splitdrive(path)[0] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
75 |
or _lowerclean(parts[0]) in (b'.hg', b'.hg.', b'') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
76 |
or pycompat.ospardir in parts |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
77 |
): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
78 |
raise error.Abort(_(b"path contains illegal component: %s") % path) |
23599
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
79 |
# Windows shortname aliases |
6dad422ecc5a
pathauditor: check for Windows shortname aliases
Matt Mackall <mpm@selenic.com>
parents:
23598
diff
changeset
|
80 |
for p in parts: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
81 |
if b"~" in p: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
82 |
first, last = p.split(b"~", 1) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
83 |
if last.isdigit() and first.upper() in [b"HG", b"HG8B6C"]: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
84 |
raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
85 |
_(b"path contains illegal component: %s") % path |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
86 |
) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
87 |
if b'.hg' in _lowerclean(path): |
44255
d84420232492
pathauditor: drop a redundant call to bytes.lower()
Martin von Zweigbergk <martinvonz@google.com>
parents:
43677
diff
changeset
|
88 |
lparts = [_lowerclean(p) for p in parts] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
89 |
for p in b'.hg', b'.hg.': |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
90 |
if p in lparts[1:]: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
91 |
pos = lparts.index(p) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
92 |
base = os.path.join(*parts[:pos]) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
93 |
raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
94 |
_(b"path '%s' is inside nested repo %r") |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
95 |
% (path, pycompat.bytestr(base)) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
96 |
) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
97 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
98 |
normparts = util.splitpath(normpath) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
99 |
assert len(parts) == len(normparts) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
100 |
|
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
101 |
parts.pop() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
102 |
normparts.pop() |
28087
0b7ce0b16d8a
pathauditor: change parts verification order to be root first
Durham Goode <durham@fb.com>
parents:
27235
diff
changeset
|
103 |
# It's important that we check the path parts starting from the root. |
44392
d52e3826cd4b
pathutil: resurrect comment about path auditing order
Yuya Nishihara <yuya@tcha.org>
parents:
44270
diff
changeset
|
104 |
# We don't want to add "foo/bar/baz" to auditeddir before checking if |
d52e3826cd4b
pathutil: resurrect comment about path auditing order
Yuya Nishihara <yuya@tcha.org>
parents:
44270
diff
changeset
|
105 |
# there's a "foo/.hg" directory. This also means we won't accidentally |
d52e3826cd4b
pathutil: resurrect comment about path auditing order
Yuya Nishihara <yuya@tcha.org>
parents:
44270
diff
changeset
|
106 |
# traverse a symlink into some other filesystem (which is potentially |
d52e3826cd4b
pathutil: resurrect comment about path auditing order
Yuya Nishihara <yuya@tcha.org>
parents:
44270
diff
changeset
|
107 |
# expensive to access). |
28087
0b7ce0b16d8a
pathauditor: change parts verification order to be root first
Durham Goode <durham@fb.com>
parents:
27235
diff
changeset
|
108 |
for i in range(len(parts)): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
109 |
prefix = pycompat.ossep.join(parts[: i + 1]) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
110 |
normprefix = pycompat.ossep.join(normparts[: i + 1]) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
111 |
if normprefix in self.auditeddir: |
28087
0b7ce0b16d8a
pathauditor: change parts verification order to be root first
Durham Goode <durham@fb.com>
parents:
27235
diff
changeset
|
112 |
continue |
27232
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
113 |
if self._realfs: |
79a86a95f325
pathauditor: add a way to skip file system check
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27231
diff
changeset
|
114 |
self._checkfs(prefix, path) |
44270
51c86c6167c1
pathutil: mark parent directories as audited as we go
Martin von Zweigbergk <martinvonz@google.com>
parents:
44255
diff
changeset
|
115 |
if self._cached: |
51c86c6167c1
pathutil: mark parent directories as audited as we go
Martin von Zweigbergk <martinvonz@google.com>
parents:
44255
diff
changeset
|
116 |
self.auditeddir.add(normprefix) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
117 |
|
33647
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
118 |
if self._cached: |
377e8ddaebef
pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents:
33435
diff
changeset
|
119 |
self.audited.add(normpath) |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
120 |
|
27231
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
121 |
def _checkfs(self, prefix, path): |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
122 |
"""raise exception if a file system backed check fails""" |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
123 |
curpath = os.path.join(self.root, prefix) |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
124 |
try: |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
125 |
st = os.lstat(curpath) |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
126 |
except OSError as err: |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
127 |
# EINVAL can be raised as invalid path syntax under win32. |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
128 |
# They must be ignored for patterns can be checked too. |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
129 |
if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL): |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
130 |
raise |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
131 |
else: |
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
132 |
if stat.S_ISLNK(st.st_mode): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
133 |
msg = _(b'path %r traverses symbolic link %r') % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
134 |
pycompat.bytestr(path), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
135 |
pycompat.bytestr(prefix), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
136 |
) |
27235
054cd38a2f19
pathutil: use temporary variables instead of complicated wrapping
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
27232
diff
changeset
|
137 |
raise error.Abort(msg) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
138 |
elif stat.S_ISDIR(st.st_mode) and os.path.isdir( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
139 |
os.path.join(curpath, b'.hg') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
140 |
): |
27231
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
141 |
if not self.callback or not self.callback(curpath): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
142 |
msg = _(b"path '%s' is inside nested repo %r") |
36678
d3b893ec5f08
py3: fix formatting of path-auditing errors
Yuya Nishihara <yuya@tcha.org>
parents:
36676
diff
changeset
|
143 |
raise error.Abort(msg % (path, pycompat.bytestr(prefix))) |
27231
6d29ce250a3d
pathauditor: move file system specific check in their own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26587
diff
changeset
|
144 |
|
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
145 |
def check(self, path): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
146 |
try: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
147 |
self(path) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
148 |
return True |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25964
diff
changeset
|
149 |
except (OSError, error.Abort): |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
150 |
return False |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
151 |
|
44924
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
152 |
@contextlib.contextmanager |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
153 |
def cached(self): |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
154 |
if self._cached: |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
155 |
yield |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
156 |
else: |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
157 |
try: |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
158 |
self._cached = True |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
159 |
yield |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
160 |
finally: |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
161 |
self.audited.clear() |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
162 |
self.auditeddir.clear() |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
163 |
self._cached = False |
233ee525dcef
grep: reduce the cost of pathauditor checks when grepping working copy
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
44630
diff
changeset
|
164 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
165 |
|
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
166 |
def canonpath(root, cwd, myname, auditor=None): |
34958
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
167 |
'''return the canonical path of myname, given cwd and root |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
168 |
|
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
169 |
>>> def check(root, cwd, myname): |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
170 |
... a = pathauditor(root, realfs=False) |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
171 |
... try: |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
172 |
... return canonpath(root, cwd, myname, a) |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
173 |
... except error.Abort: |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
174 |
... return 'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
175 |
>>> def unixonly(root, cwd, myname, expected='aborted'): |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
176 |
... if pycompat.iswindows: |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
177 |
... return expected |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
178 |
... return check(root, cwd, myname) |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
179 |
>>> def winonly(root, cwd, myname, expected='aborted'): |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
180 |
... if not pycompat.iswindows: |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
181 |
... return expected |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
182 |
... return check(root, cwd, myname) |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
183 |
>>> winonly(b'd:\\\\repo', b'c:\\\\dir', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
184 |
'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
185 |
>>> winonly(b'c:\\\\repo', b'c:\\\\dir', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
186 |
'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
187 |
>>> winonly(b'c:\\\\repo', b'c:\\\\', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
188 |
'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
189 |
>>> winonly(b'c:\\\\repo', b'c:\\\\', b'repo\\\\filename', |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
190 |
... b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
191 |
'filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
192 |
>>> winonly(b'c:\\\\repo', b'c:\\\\repo', b'filename', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
193 |
'filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
194 |
>>> winonly(b'c:\\\\repo', b'c:\\\\repo\\\\subdir', b'filename', |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
195 |
... b'subdir/filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
196 |
'subdir/filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
197 |
>>> unixonly(b'/repo', b'/dir', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
198 |
'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
199 |
>>> unixonly(b'/repo', b'/', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
200 |
'aborted' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
201 |
>>> unixonly(b'/repo', b'/', b'repo/filename', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
202 |
'filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
203 |
>>> unixonly(b'/repo', b'/repo', b'filename', b'filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
204 |
'filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
205 |
>>> unixonly(b'/repo', b'/repo/subdir', b'filename', b'subdir/filename') |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
206 |
'subdir/filename' |
705d0f2bb677
pathutil: add doctests for canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34956
diff
changeset
|
207 |
''' |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
208 |
if util.endswithsep(root): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
209 |
rootsep = root |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
210 |
else: |
30619
cfe66dcf45c0
py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30342
diff
changeset
|
211 |
rootsep = root + pycompat.ossep |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
212 |
name = myname |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
213 |
if not os.path.isabs(name): |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
214 |
name = os.path.join(root, cwd, name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
215 |
name = os.path.normpath(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
216 |
if auditor is None: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
217 |
auditor = pathauditor(root) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
218 |
if name != rootsep and name.startswith(rootsep): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
219 |
name = name[len(rootsep) :] |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
220 |
auditor(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
221 |
return util.pconvert(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
222 |
elif name == root: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
223 |
return b'' |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
224 |
else: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
225 |
# Determine whether `name' is in the hierarchy at or beneath `root', |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
226 |
# by iterating name=dirname(name) until that causes no change (can't |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
227 |
# check name == '/', because that doesn't work on windows). The list |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
228 |
# `rel' holds the reversed list of components making up the relative |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
229 |
# file name we want. |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
230 |
rel = [] |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
231 |
while True: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
232 |
try: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
233 |
s = util.samefile(name, root) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
234 |
except OSError: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
235 |
s = False |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
236 |
if s: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
237 |
if not rel: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
238 |
# name was actually the same as root (maybe a symlink) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
239 |
return b'' |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
240 |
rel.reverse() |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
241 |
name = os.path.join(*rel) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
242 |
auditor(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
243 |
return util.pconvert(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
244 |
dirname, basename = util.split(name) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
245 |
rel.append(basename) |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
246 |
if dirname == name: |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
247 |
break |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
248 |
name = dirname |
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
diff
changeset
|
249 |
|
25011
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
250 |
# A common mistake is to use -R, but specify a file relative to the repo |
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
251 |
# instead of cwd. Detect that case, and provide a hint to the user. |
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
252 |
hint = None |
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
253 |
try: |
25022
10bbdcd89164
canonpath: fix infinite recursion
Matt Mackall <mpm@selenic.com>
parents:
25011
diff
changeset
|
254 |
if cwd != root: |
10bbdcd89164
canonpath: fix infinite recursion
Matt Mackall <mpm@selenic.com>
parents:
25011
diff
changeset
|
255 |
canonpath(root, root, myname, auditor) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
256 |
relpath = util.pathto(root, cwd, b'') |
38593
21be76e07148
py3: use bytes.endswith() instead of bytes[n]
Yuya Nishihara <yuya@tcha.org>
parents:
36678
diff
changeset
|
257 |
if relpath.endswith(pycompat.ossep): |
34956
f445b10dc7fb
pathutil: use util.pathto() to calculate relative cwd in canonpath()
Matt Harbison <matt_harbison@yahoo.com>
parents:
34261
diff
changeset
|
258 |
relpath = relpath[:-1] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
259 |
hint = _(b"consider using '--cwd %s'") % relpath |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25964
diff
changeset
|
260 |
except error.Abort: |
25011
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
261 |
pass |
7d6a507a4c53
pathutil: hint if a path is root relative instead of cwd relative (issue4663)
Matt Harbison <matt_harbison@yahoo.com>
parents:
23602
diff
changeset
|
262 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
263 |
raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
264 |
_(b"%s not under root '%s'") % (myname, root), hint=hint |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
265 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
266 |
|
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
267 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
268 |
def normasprefix(path): |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
269 |
'''normalize the specified path as path prefix |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
270 |
|
23139
e53f6b72a0e4
spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents:
21568
diff
changeset
|
271 |
Returned value can be used safely for "p.startswith(prefix)", |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
272 |
"p[len(prefix):]", and so on. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
273 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
274 |
For efficiency, this expects "path" argument to be already |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
275 |
normalized by "os.path.normpath", "os.path.realpath", and so on. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
276 |
|
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
277 |
See also issue3033 for detail about need of this function. |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
278 |
|
34261
cd022a11ec83
py3: use bytes os.sep in doctest of pathutil.py
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
279 |
>>> normasprefix(b'/foo/bar').replace(pycompat.ossep, b'/') |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
280 |
'/foo/bar/' |
34261
cd022a11ec83
py3: use bytes os.sep in doctest of pathutil.py
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
281 |
>>> normasprefix(b'/').replace(pycompat.ossep, b'/') |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
282 |
'/' |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
283 |
''' |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
284 |
d, p = os.path.splitdrive(path) |
30619
cfe66dcf45c0
py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30342
diff
changeset
|
285 |
if len(p) != len(pycompat.ossep): |
cfe66dcf45c0
py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30342
diff
changeset
|
286 |
return path + pycompat.ossep |
21568
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
287 |
else: |
8dd17b19e722
subrepo: normalize path in the specific way for problematic encodings
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20033
diff
changeset
|
288 |
return path |
25281
660b178f49c7
pathutil: add dirname and join functions
Durham Goode <durham@fb.com>
parents:
25022
diff
changeset
|
289 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
290 |
|
43677
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
291 |
def finddirs(path): |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
292 |
pos = path.rfind(b'/') |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
293 |
while pos != -1: |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
294 |
yield path[:pos] |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
295 |
pos = path.rfind(b'/', 0, pos) |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
296 |
yield b'' |
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
297 |
|
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
298 |
|
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
299 |
class dirs(object): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
300 |
'''a multiset of directory names from a set of file paths''' |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
301 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
302 |
def __init__(self, map, skip=None): |
44630
11f284c8c5e4
pathutil: document that dirs map type implies manifest/dirstate processing
Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
parents:
44392
diff
changeset
|
303 |
''' |
11f284c8c5e4
pathutil: document that dirs map type implies manifest/dirstate processing
Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
parents:
44392
diff
changeset
|
304 |
a dict map indicates a dirstate while a list indicates a manifest |
11f284c8c5e4
pathutil: document that dirs map type implies manifest/dirstate processing
Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
parents:
44392
diff
changeset
|
305 |
''' |
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
306 |
self._dirs = {} |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
307 |
addpath = self.addpath |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
308 |
if isinstance(map, dict) and skip is not None: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
309 |
for f, s in pycompat.iteritems(map): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
310 |
if s[0] != skip: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
311 |
addpath(f) |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
312 |
elif skip is not None: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
313 |
raise error.ProgrammingError( |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
314 |
b"skip character is only supported with a dict source" |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
315 |
) |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
316 |
else: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
317 |
for f in map: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
318 |
addpath(f) |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
319 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
320 |
def addpath(self, path): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
321 |
dirs = self._dirs |
43677
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
322 |
for base in finddirs(path): |
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
323 |
if base.endswith(b'/'): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
324 |
raise ValueError( |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
325 |
"found invalid consecutive slashes in path: %r" % base |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
326 |
) |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
327 |
if base in dirs: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
328 |
dirs[base] += 1 |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
329 |
return |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
330 |
dirs[base] = 1 |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
331 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
332 |
def delpath(self, path): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
333 |
dirs = self._dirs |
43677
0b7733719d21
utils: move finddirs() to pathutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43571
diff
changeset
|
334 |
for base in finddirs(path): |
43571
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
335 |
if dirs[base] > 1: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
336 |
dirs[base] -= 1 |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
337 |
return |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
338 |
del dirs[base] |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
339 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
340 |
def __iter__(self): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
341 |
return iter(self._dirs) |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
342 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
343 |
def __contains__(self, d): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
344 |
return d in self._dirs |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
345 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
346 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
347 |
if util.safehasattr(parsers, 'dirs'): |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
348 |
dirs = parsers.dirs |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
349 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
350 |
if rustdirs is not None: |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
351 |
dirs = rustdirs |
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
352 |
|
c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43077
diff
changeset
|
353 |
|
25286
127a11f705d9
pathutil: demote two local functions to just be forwards
Augie Fackler <augie@google.com>
parents:
25285
diff
changeset
|
354 |
# forward two methods from posixpath that do what we need, but we'd |
127a11f705d9
pathutil: demote two local functions to just be forwards
Augie Fackler <augie@google.com>
parents:
25285
diff
changeset
|
355 |
# rather not let our internals know that we're thinking in posix terms |
127a11f705d9
pathutil: demote two local functions to just be forwards
Augie Fackler <augie@google.com>
parents:
25285
diff
changeset
|
356 |
# - instead we'll let them be oblivious. |
127a11f705d9
pathutil: demote two local functions to just be forwards
Augie Fackler <augie@google.com>
parents:
25285
diff
changeset
|
357 |
join = posixpath.join |
127a11f705d9
pathutil: demote two local functions to just be forwards
Augie Fackler <augie@google.com>
parents:
25285
diff
changeset
|
358 |
dirname = posixpath.dirname |