repo_path: don't panic on invalid UTF-8 path component

Although watchman client appears to fail at decoding non-UTF-8 path (somewhere
in serde), jj shouldn't panic if watchman could deal with that.

The outer error message "path not in the repo" would sounds odd, but I think
that's okay because 1. it's unlikely that a user input is not UTF-8, and 2.
it's technically correct that a non-UTF-8 path is not contained in the repo.
This commit is contained in:
Yuya Nishihara 2023-11-20 17:28:21 +09:00
parent a224d0f172
commit fa60026f25

View file

@ -211,7 +211,12 @@ impl RepoPathBuf {
let mut components = relative_path
.components()
.map(|c| match c {
Component::Normal(name) => Ok(name.to_str().unwrap()),
Component::Normal(name) => {
name.to_str()
.ok_or_else(|| RelativePathParseError::InvalidUtf8 {
path: relative_path.into(),
})
}
_ => Err(RelativePathParseError::InvalidComponent {
component: c.as_os_str().to_string_lossy().into(),
path: relative_path.into(),
@ -424,6 +429,8 @@ pub enum RelativePathParseError {
component: Box<str>,
path: Box<Path>,
},
#[error(r#"Not valid UTF-8 path "{path}""#)]
InvalidUtf8 { path: Box<Path> },
}
#[derive(Clone, Debug, Eq, Error, PartialEq)]