11use std:: convert:: TryInto ;
22
33use git_hash:: { oid, ObjectId } ;
4- use git_odb:: { Find , FindExt } ;
4+ use git_odb:: { Find , FindExt , Write } ;
55use git_ref:: {
66 transaction:: { LogChange , PreviousValue , RefLog } ,
77 FullName ,
@@ -41,10 +41,9 @@ impl crate::Repository {
4141 /// `try_find_object()` operations from succeeding while alive.
4242 /// To bypass this limit, clone this `sync::Handle` instance.
4343 pub fn try_find_object ( & self , id : impl Into < ObjectId > ) -> Result < Option < Object < ' _ > > , object:: find:: OdbError > {
44- let state = self ;
4544 let id = id. into ( ) ;
4645
47- let mut buf = state . free_buf ( ) ;
46+ let mut buf = self . free_buf ( ) ;
4847 match self . objects . try_find ( & id, & mut buf) ? {
4948 Some ( obj) => {
5049 let kind = obj. kind ;
@@ -56,16 +55,33 @@ impl crate::Repository {
5655
5756 /// Write the given object into the object database and return its object id.
5857 pub fn write_object ( & self , object : impl git_object:: WriteTo ) -> Result < Id < ' _ > , object:: write:: Error > {
59- use git_odb:: Write ;
60-
61- let state = self ;
62- state
63- . objects
58+ self . objects
6459 . write ( object)
6560 . map ( |oid| oid. attach ( self ) )
6661 . map_err ( Into :: into)
6762 }
6863
64+ /// Write a blob from the given `bytes`.
65+ pub fn write_blob ( & self , bytes : impl AsRef < [ u8 ] > ) -> Result < Id < ' _ > , object:: write:: Error > {
66+ self . objects
67+ . write_buf ( git_object:: Kind :: Blob , bytes. as_ref ( ) )
68+ . map ( |oid| oid. attach ( self ) )
69+ }
70+
71+ /// Write a blob from the given `Read` implementation.
72+ pub fn write_blob_stream (
73+ & self ,
74+ mut bytes : impl std:: io:: Read + std:: io:: Seek ,
75+ ) -> Result < Id < ' _ > , object:: write:: Error > {
76+ let current = bytes. stream_position ( ) ?;
77+ let len = bytes. seek ( std:: io:: SeekFrom :: End ( 0 ) ) ? - current;
78+ bytes. seek ( std:: io:: SeekFrom :: Start ( current) ) ?;
79+
80+ self . objects
81+ . write_stream ( git_object:: Kind :: Blob , len, bytes)
82+ . map ( |oid| oid. attach ( self ) )
83+ }
84+
6985 /// Create a tag reference named `name` (without `refs/tags/` prefix) pointing to a newly created tag object
7086 /// which in turn points to `target` and return the newly created reference.
7187 ///
0 commit comments