diff --git a/git-repository/src/object/tree/mod.rs b/git-repository/src/object/tree/mod.rs index d8a2bad728..cb4a6f9ecb 100644 --- a/git-repository/src/object/tree/mod.rs +++ b/git-repository/src/object/tree/mod.rs @@ -60,6 +60,24 @@ impl<'repo> Tree<'repo> { } Ok(None) } + + /// Like [`lookup_path()`][Self::lookup_path()], but takes a `Path` directly via `relative_path`, a path relative to this tree. + /// + /// # Note + /// + /// If any path component contains illformed UTF-8 and thus can't be converted to bytes on platforms which can't do so natively, + /// the returned component will be empty which makes the lookup fail. + pub fn lookup_path_by_path( + self, + relative_path: impl AsRef, + ) -> Result, find::existing::Error> { + self.lookup_path( + relative_path + .as_ref() + .components() + .map(|c| git_path::os_str_into_bstr(c.as_os_str()).unwrap_or("".into()).as_ref()), + ) + } } /// diff --git a/git-repository/src/revision/spec/parse/delegate/navigate.rs b/git-repository/src/revision/spec/parse/delegate/navigate.rs index 3bcca5ebe5..35433a6e73 100644 --- a/git-repository/src/revision/spec/parse/delegate/navigate.rs +++ b/git-repository/src/revision/spec/parse/delegate/navigate.rs @@ -124,17 +124,13 @@ impl<'repo> delegate::Navigate for Delegate<'repo> { return Ok(tree_id); } let tree = repo.find_object(tree_id)?.into_tree(); - let entry = tree - .lookup_path(git_path::from_bstr(path).components().map(|c| { - git_path::os_str_into_bstr(c.as_os_str()) - .expect("no illformed UTF-8") - .as_ref() - }))? - .ok_or_else(|| Error::PathNotFound { - path: path.into(), - object: obj.attach(repo).shorten_or_id(), - tree: tree_id.attach(repo).shorten_or_id(), - })?; + let entry = + tree.lookup_path_by_path(git_path::from_bstr(path))? + .ok_or_else(|| Error::PathNotFound { + path: path.into(), + object: obj.attach(repo).shorten_or_id(), + tree: tree_id.attach(repo).shorten_or_id(), + })?; Ok(entry.oid) }; for obj in objs.iter() {