normalize: Add "./" if a path starts with a "-"

This commit is contained in:
Fabian Homborg 2022-03-26 10:45:22 +01:00
parent bb3700997c
commit b961afed49
3 changed files with 17 additions and 1 deletions

View file

@ -239,6 +239,8 @@ Examples
Unlike ``realpath`` or ``path resolve``, it does not make the paths absolute. It also does not resolve any symlinks. As such it can operate on non-existent paths.
Leading "./" components are usually removed. But when a path starts with ``-``, ``path normalize`` will add it instead to avoid confusion with options.
It returns 0 if any normalization was done, i.e. any given path wasn't in canonical form.
Examples
@ -257,6 +259,9 @@ Examples
>_ path normalize ./my/subdirs/../sub2
my/sub2
>_ path normalize -- -/foo
./-/foo
"resolve" subcommand
--------------------

View file

@ -482,7 +482,11 @@ static int path_dirname(parser_t &parser, io_streams_t &streams, int argc, const
// Not a constref because this must have the same type as wdirname.
// cppcheck-suppress passedByValue
static wcstring normalize_helper(wcstring path) {
return normalize_path(path, false);
wcstring np = normalize_path(path, false);
if (!np.empty() && np[0] == L'-') {
np = L"./" + np;
}
return np;
}
static bool filter_path(options_t opts, const wcstring &path) {

View file

@ -105,6 +105,13 @@ path normalize /bin//bash
# The "//" is squashed, but /bin isn't resolved even if your system links it to /usr/bin.
# CHECK: /bin/bash
# Paths with "-" get a "./":
path normalize -- -/foo -foo/foo
# CHECK: ./-/foo
# CHECK: ./-foo/foo
path normalize -- ../-foo
# CHECK: ../-foo
# We need to remove the rest of the path because we have no idea what its value looks like.
path resolve bin//sh | string match -r -- 'bin/bash$'
# The "//" is squashed, and the symlink is resolved.