File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -596,6 +596,26 @@ impl Index {
596
596
Ok ( Binding :: from_raw ( & raw as * const _) )
597
597
}
598
598
}
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
+ }
599
619
}
600
620
601
621
impl Binding for Index {
@@ -857,6 +877,21 @@ mod tests {
857
877
assert_eq ! ( e. path. len( ) , 6 ) ;
858
878
}
859
879
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
+
860
895
#[ test]
861
896
fn add_frombuffer_then_read ( ) {
862
897
let ( _td, repo) = crate :: test:: repo_init ( ) ;
You can’t perform that action at this time.
0 commit comments