Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tree::get_name_bytes to handle non-UTF-8 entry names #841

Merged
merged 2 commits into from
May 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ impl<'repo> Tree<'repo> {

/// Lookup a tree entry by its filename
pub fn get_name(&self, filename: &str) -> Option<TreeEntry<'_>> {
self.get_name_bytes(filename.as_bytes())
}

/// 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));
Expand Down Expand Up @@ -510,6 +517,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