use std::path::{Component, Path, PathBuf};
use super::GitError;
use super::git::run_git;
use super::hunks::Hunk;
pub(super) async fn working_prefix(root: &Path) -> Result<PathBuf, GitError> {
Ok(PathBuf::from(
run_git(root, &["rev-parse", "--show-prefix"]).await?,
))
}
pub(super) fn from_prefix(path: &Path, prefix: &Path) -> PathBuf {
let prefix: Vec<Component<'_>> = prefix.components().collect();
let path: Vec<Component<'_>> = path.components().collect();
let common = prefix
.iter()
.zip(&path)
.take_while(|(left, right)| left == right)
.count();
std::iter::repeat_n(Component::ParentDir, prefix.len() - common)
.chain(path[common..].iter().copied())
.collect()
}
pub(super) fn paths_from(paths: Vec<PathBuf>, prefix: &Path) -> Vec<PathBuf> {
paths.iter().map(|path| from_prefix(path, prefix)).collect()
}
pub(super) fn hunks_from(mut hunks: Vec<Hunk>, prefix: &Path) -> Vec<Hunk> {
for hunk in &mut hunks {
hunk.file_path = from_prefix(&hunk.file_path, prefix);
}
hunks
}