pub fn normalize_path(file_path: &str, repo_root: Option<&str>) -> String {
let stripped = match repo_root {
Some(root) => file_path
.strip_prefix(root)
.and_then(|s| s.strip_prefix('/'))
.unwrap_or(file_path),
None => file_path,
};
let absolute = stripped.starts_with('/');
let mut components: Vec<&str> = Vec::new();
for part in stripped.split('/') {
match part {
"" | "." => continue,
".." => {
if components.pop().is_none() {
return stripped.to_string();
}
}
c => components.push(c),
}
}
match (components.is_empty(), absolute) {
(true, true) => "/".to_string(),
(true, false) => ".".to_string(),
(false, true) => format!("/{}", components.join("/")),
(false, false) => components.join("/"),
}
}