forked from mirrors/jj
cli: simplify relative_path() by leveraging ancestors() iterator
Just code cleanup. There should be no behavior change.
This commit is contained in:
parent
f3a9b06cc8
commit
58977f8cbf
1 changed files with 19 additions and 18 deletions
37
src/ui.rs
37
src/ui.rs
|
@ -15,7 +15,7 @@
|
|||
use std::io::{Stderr, Stdout, Write};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::{fmt, io};
|
||||
use std::{fmt, io, iter};
|
||||
|
||||
use atty::Stream;
|
||||
use jujutsu_lib::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin};
|
||||
|
@ -209,25 +209,26 @@ pub enum FilePathParseError {
|
|||
InputNotInRepo(String),
|
||||
}
|
||||
|
||||
pub fn relative_path(mut from: &Path, to: &Path) -> PathBuf {
|
||||
let mut result = PathBuf::from("");
|
||||
loop {
|
||||
if let Ok(suffix) = to.strip_prefix(from) {
|
||||
result = result.join(suffix);
|
||||
break;
|
||||
}
|
||||
if let Some(parent) = from.parent() {
|
||||
result = result.join("..");
|
||||
from = parent;
|
||||
} else {
|
||||
result = to.to_path_buf();
|
||||
break;
|
||||
/// Turns the given `to` path into relative path starting from the `from` path.
|
||||
///
|
||||
/// Both `from` and `to` paths are supposed to be absolute and normalized in the
|
||||
/// same manner.
|
||||
pub fn relative_path(from: &Path, to: &Path) -> PathBuf {
|
||||
// Find common prefix.
|
||||
for (i, base) in from.ancestors().enumerate() {
|
||||
if let Ok(suffix) = to.strip_prefix(base) {
|
||||
if i == 0 && suffix.as_os_str().is_empty() {
|
||||
return ".".into();
|
||||
} else {
|
||||
let mut result = PathBuf::from_iter(iter::repeat("..").take(i));
|
||||
result.push(suffix);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if result.as_os_str().is_empty() {
|
||||
result = PathBuf::from(".");
|
||||
}
|
||||
result
|
||||
|
||||
// No common prefix found. Return the original (absolute) path.
|
||||
to.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue