Skip to content

Commit 23a5340

Browse files
committed
Add Tree::get_name_bytes to handle non-UTF-8 entry names
1 parent 31d3ff0 commit 23a5340

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/tree.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ impl<'repo> Tree<'repo> {
176176
}
177177
}
178178

179+
/// Lookup a tree entry by its filename, specified as bytes.
180+
///
181+
/// This allows for non-UTF-8 filenames.
182+
pub fn get_name_bytes(&self, filename: &[u8]) -> Option<TreeEntry<'_>> {
183+
let filename = CString::new(filename).unwrap();
184+
unsafe {
185+
let ptr = call!(raw::git_tree_entry_byname(&*self.raw(), filename));
186+
if ptr.is_null() {
187+
None
188+
} else {
189+
Some(entry_from_raw_const(ptr))
190+
}
191+
}
192+
}
193+
179194
/// Retrieve a tree entry contained in a tree or in any of its subtrees,
180195
/// given its relative path.
181196
pub fn get_path(&self, path: &Path) -> Result<TreeEntry<'static>, Error> {
@@ -510,6 +525,7 @@ mod tests {
510525
let e1 = tree.get(0).unwrap();
511526
assert!(e1 == tree.get_id(e1.id()).unwrap());
512527
assert!(e1 == tree.get_name("foo").unwrap());
528+
assert!(e1 == tree.get_name_bytes(b"foo").unwrap());
513529
assert!(e1 == tree.get_path(Path::new("foo")).unwrap());
514530
assert_eq!(e1.name(), Some("foo"));
515531
e1.to_object(&repo).unwrap();

0 commit comments

Comments
 (0)