pub struct RepoPathNormalizer<'a> { /* private fields */ }Expand description
Converts user-supplied paths, relative to one working directory, into normalized
repository-relative paths (as normalize_repo_relative does for a single path).
The working directory’s position in the work tree is resolved once, so each relative path
is normalized lexically on its /-separated components with a single allocation for the
result. Absolute paths and paths that climb above the work tree root take the general
route, which also recognizes alternate spellings of the work tree (such as macOS
/private/tmp for /tmp).
§Examples
use std::path::Path;
use grit_lib::config::environment::RepositoryOptions;
use grit_lib::repo::Repository;
use grit_lib::worktree::ignore::RepoPathNormalizer;
let repo = Repository::discover(None, &RepositoryOptions::empty())?;
let cwd = repo.work_tree().unwrap_or(Path::new(".")).join("src");
let normalizer = RepoPathNormalizer::new(&repo, &cwd)?;
assert_eq!(normalizer.normalize("./lib/../main.rs")?, "src/main.rs");
assert_eq!(normalizer.normalize("../docs/")?, "docs/");Implementations§
Source§impl<'a> RepoPathNormalizer<'a>
impl<'a> RepoPathNormalizer<'a>
Sourcepub fn new(repo: &'a Repository, cwd: &'a Path) -> Result<Self>
pub fn new(repo: &'a Repository, cwd: &'a Path) -> Result<Self>
Prepare to normalize paths given relative to cwd.
§Parameters
repo- repository whose work tree paths are made relative to.cwd- directory relative input paths start from (usually the process’s current directory); it may lie outside the work tree.
§Errors
Returns crate::worktree::WorktreeError::Path when repo has no work tree.
Sourcepub fn normalize(&self, path: &str) -> Result<String>
pub fn normalize(&self, path: &str) -> Result<String>
Normalize one user-supplied path.
. and .. components are resolved lexically, a trailing / (a directory query) is
kept, the work tree root itself becomes "", and paths starting with : are returned
unchanged.
§Parameters
path- path as the user typed it, relative to the normalizer’s directory or absolute.
§Returns
The repository-relative path with / separators.
§Errors
Returns crate::worktree::WorktreeError::Path if the path resolves outside the work tree.