Skip to content

Commit f8ac2f7

Browse files
committed
implement find_prefix for index
1 parent c93d29b commit f8ac2f7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/index.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,26 @@ impl Index {
596596
Ok(Binding::from_raw(&raw as *const _))
597597
}
598598
}
599+
600+
/// Find the first position of any entries matching a prefix.
601+
///
602+
/// To find the first position of a path inside a given folder, suffix the prefix with a '/'.
603+
pub fn find_prefix(&self, path: &Path) -> Result<Option<usize>, Error> {
604+
unsafe {
605+
let mut at_pos: size_t = 0;
606+
let entry_path = path.into_c_string()?;
607+
let result = call!(raw::git_index_find_prefix(
608+
&mut at_pos,
609+
self.raw,
610+
entry_path
611+
));
612+
if result == 0 {
613+
Ok(Some(at_pos))
614+
} else {
615+
Ok(None)
616+
}
617+
}
618+
}
599619
}
600620

601621
impl Binding for Index {
@@ -857,6 +877,21 @@ mod tests {
857877
assert_eq!(e.path.len(), 6);
858878
}
859879

880+
#[test]
881+
fn add_then_find() {
882+
let mut index = Index::new().unwrap();
883+
let mut e = entry();
884+
e.path = b"foo/bar".to_vec();
885+
index.add(&e).unwrap();
886+
assert_eq!(index.get(0).unwrap().path, b"foo/bar");
887+
assert_eq!(
888+
index.get_path(Path::new("foo/bar"), 0).unwrap().path,
889+
b"foo/bar"
890+
);
891+
assert_eq!(index.find_prefix(Path::new("foo/")).unwrap(), Some(0));
892+
assert_eq!(index.find_prefix(Path::new("empty/")).unwrap(), None);
893+
}
894+
860895
#[test]
861896
fn add_frombuffer_then_read() {
862897
let (_td, repo) = crate::test::repo_init();

0 commit comments

Comments
 (0)