fix: correct rename on unix platforms caused by pathdiff#8 (#1587)

* fix: correct rename on unix platforms caused by pathdiff#8

* fix: ensure all calls to pathdiff

* fix: names

* fix: file path on windows
This commit is contained in:
Myriad-Dreamin 2025-03-26 12:46:33 +08:00 committed by GitHub
parent c102ace9ab
commit e4a4fc568f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 44 additions and 28 deletions

View file

@ -1,6 +1,7 @@
//! Path utilities.
use std::path::{Component, Path};
use std::borrow::Cow;
use std::path::{Component, Path, PathBuf};
pub use path_clean::PathClean;
@ -48,6 +49,24 @@ pub fn unix_slash(root: &Path) -> String {
/// Get the path cleaned as a platform-style string.
pub use path_clean::clean;
/// Construct a relative path from a provided base directory path to the
/// provided path.
pub fn diff(fr: &Path, to: &Path) -> Option<PathBuf> {
// Because of <https://github.com/Manishearth/pathdiff/issues/8>, we have to clean the path
// before diff.
fn clean_for_diff(p: &Path) -> Cow<'_, Path> {
if p.components()
.any(|c| matches!(c, Component::ParentDir | Component::CurDir))
{
Cow::Owned(p.clean())
} else {
Cow::Borrowed(p)
}
}
pathdiff::diff_paths(clean_for_diff(fr).as_ref(), clean_for_diff(to).as_ref())
}
#[cfg(test)]
mod test {
use std::path::{Path, PathBuf};