Expand symlinks before computing relative path for display paths

Fixes #447
pull/454/head
Wilfred Hughes 2022-12-22 22:46:32 +07:00
parent d18acce856
commit 3b37b9a12c
2 changed files with 12 additions and 0 deletions

@ -11,6 +11,11 @@ support them.
`--list-languages` now shows filenames associated with languages
(e.g. `Cargo.lock` is TOML) in addition to extensions.
### Display
Symlinks are now expanded before calculating relative paths, resulting
in relative paths being shown in more cases.
### Build
Difftastic is now built with Ubuntu 20.04 on GitHub, so prebuilt

@ -242,8 +242,15 @@ pub enum FileArgument {
DevNull,
}
fn try_canonicalize(path: &Path) -> PathBuf {
path.canonicalize().unwrap_or_else(|_| path.into())
}
fn relative_to_current(path: &Path) -> PathBuf {
if let Ok(current_path) = std::env::current_dir() {
let path = try_canonicalize(path);
let current_path = try_canonicalize(&current_path);
if let Ok(rel_path) = path.strip_prefix(current_path) {
return rel_path.into();
}