Skip to content

Commit

Permalink
Add Tree::get_name_bytes to handle non-UTF-8 entry names
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtriplett committed May 10, 2022
1 parent 31d3ff0 commit 23a5340
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ impl<'repo> Tree<'repo> {
}
}

/// Lookup a tree entry by its filename, specified as bytes.
///
/// This allows for non-UTF-8 filenames.
pub fn get_name_bytes(&self, filename: &[u8]) -> Option<TreeEntry<'_>> {
let filename = CString::new(filename).unwrap();
unsafe {
let ptr = call!(raw::git_tree_entry_byname(&*self.raw(), filename));
if ptr.is_null() {
None
} else {
Some(entry_from_raw_const(ptr))
}
}
}

/// Retrieve a tree entry contained in a tree or in any of its subtrees,
/// given its relative path.
pub fn get_path(&self, path: &Path) -> Result<TreeEntry<'static>, Error> {
Expand Down Expand Up @@ -510,6 +525,7 @@ mod tests {
let e1 = tree.get(0).unwrap();
assert!(e1 == tree.get_id(e1.id()).unwrap());
assert!(e1 == tree.get_name("foo").unwrap());
assert!(e1 == tree.get_name_bytes(b"foo").unwrap());
assert!(e1 == tree.get_path(Path::new("foo")).unwrap());
assert_eq!(e1.name(), Some("foo"));
e1.to_object(&repo).unwrap();
Expand Down

0 comments on commit 23a5340

Please sign in to comment.