gix_path

Function normalize

Source
pub fn normalize<'a>(
    path: Cow<'a, Path>,
    current_dir: &Path,
) -> Option<Cow<'a, Path>>
Expand description

Resolve relative components virtually without accessing the file system, e.g. turn a/./b/c/.././.. into a, without keeping intermediate .. and /a/../b/.. becomes /. If the input path was relative and ends up being the current_dir, . is returned instead of the full path to current_dir. Note that single . components as well as duplicate separators are left untouched.

This is particularly useful when manipulating paths that are based on user input, and not resolving intermediate symlinks keeps the path similar to what the user provided. If that’s not desirable, use [realpath()][crate::realpath() instead.

Note that we might access the current_dir if we run out of path components to pop off, which is expected to be absolute as typical return value of std::env::current_dir() or gix_fs::current_dir(…) when core.precomposeUnicode is known. As a current_dir like /c can be exhausted by paths like ../../r, None will be returned to indicate the inability to produce a logically consistent path.