Skip to content

Commit

Permalink
simplify looking up entries by path (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 20, 2022
1 parent 523418f commit 15a18e4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
18 changes: 18 additions & 0 deletions git-repository/src/object/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::path::Path>,
) -> Result<Option<git_object::tree::Entry>, 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()),
)
}
}

///
Expand Down
18 changes: 7 additions & 11 deletions git-repository/src/revision/spec/parse/delegate/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 15a18e4

Please sign in to comment.