use super::CURRENT;
use super::lexical::{expand_home, express_relative_to_root, lexical_join};
pub(super) fn home_path_inside_root(path: &str) -> Option<String> {
if !path.starts_with("~/") || path.contains(['*', '?', '[']) {
return None;
}
let (cwd, root) = CURRENT.with(|c| {
let ctx = c.borrow();
(ctx.cwd.clone(), ctx.root.clone())
});
let (cwd, root) = (cwd.filter(|c| c.starts_with('/'))?, root.filter(|r| r.starts_with('/'))?);
if express_relative_to_root(&lexical_join("/", &cwd), &root).starts_with('/')
|| crate::engine::resolve::regions::protection_covers(&root)
{
return None;
}
let relative = express_relative_to_root(&lexical_join("/", &expand_home(path)?), &root);
(!relative.starts_with('/')).then_some(relative)
}