@@ -364,7 +364,7 @@ impl<'repo> io::Read for OdbReader<'repo> {
364364 if res < 0 {
365365 Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Read error" ) )
366366 } else {
367- Ok ( len )
367+ Ok ( res as _ )
368368 }
369369 }
370370 }
@@ -726,4 +726,45 @@ mod tests {
726726 t ! ( repo. reset( commit1. as_object( ) , ResetType :: Hard , None ) ) ;
727727 assert ! ( foo_file. exists( ) ) ;
728728 }
729+
730+ #[ test]
731+ fn stream_read ( ) {
732+ // Test for read impl of OdbReader.
733+ const FOO_TEXT : & [ u8 ] = b"this is a test" ;
734+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
735+ let p = repo. path ( ) . parent ( ) . unwrap ( ) . join ( "foo" ) ;
736+ std:: fs:: write ( & p, FOO_TEXT ) . unwrap ( ) ;
737+ let mut index = repo. index ( ) . unwrap ( ) ;
738+ index. add_path ( std:: path:: Path :: new ( "foo" ) ) . unwrap ( ) ;
739+ let tree_id = index. write_tree ( ) . unwrap ( ) ;
740+ let tree = repo. find_tree ( tree_id) . unwrap ( ) ;
741+ let sig = repo. signature ( ) . unwrap ( ) ;
742+ let head_id = repo. refname_to_id ( "HEAD" ) . unwrap ( ) ;
743+ let parent = repo. find_commit ( head_id) . unwrap ( ) ;
744+ let _commit = repo
745+ . commit ( Some ( "HEAD" ) , & sig, & sig, "commit" , & tree, & [ & parent] )
746+ . unwrap ( ) ;
747+
748+ // Try reading from a commit object.
749+ let odb = repo. odb ( ) . unwrap ( ) ;
750+ let oid = repo. refname_to_id ( "HEAD" ) . unwrap ( ) ;
751+ let ( mut reader, size, ty) = odb. reader ( oid) . unwrap ( ) ;
752+ assert ! ( ty == ObjectType :: Commit ) ;
753+ let mut x = [ 0 ; 10000 ] ;
754+ let r = reader. read ( & mut x) . unwrap ( ) ;
755+ assert ! ( r == size) ;
756+
757+ // Try reading from a blob. This assumes it is a loose object (packed
758+ // objects can't read).
759+ let commit = repo. find_commit ( oid) . unwrap ( ) ;
760+ let tree = commit. tree ( ) . unwrap ( ) ;
761+ let entry = tree. get_name ( "foo" ) . unwrap ( ) ;
762+ let ( mut reader, size, ty) = odb. reader ( entry. id ( ) ) . unwrap ( ) ;
763+ assert_eq ! ( size, FOO_TEXT . len( ) ) ;
764+ assert ! ( ty == ObjectType :: Blob ) ;
765+ let mut x = [ 0 ; 10000 ] ;
766+ let r = reader. read ( & mut x) . unwrap ( ) ;
767+ assert_eq ! ( r, 14 ) ;
768+ assert_eq ! ( & x[ ..FOO_TEXT . len( ) ] , FOO_TEXT ) ;
769+ }
729770}
0 commit comments