@@ -2,15 +2,50 @@ use git_features::hash;
22
33use crate :: { write, File , Version } ;
44
5+ /// The error produced by [`File::write()`].
6+ #[ derive( Debug , thiserror:: Error ) ]
7+ #[ allow( missing_docs) ]
8+ pub enum Error {
9+ #[ error( transparent) ]
10+ Io ( #[ from] std:: io:: Error ) ,
11+ #[ error( "Could not acquire lock for index file" ) ]
12+ AcquireLock ( #[ from] git_lock:: acquire:: Error ) ,
13+ #[ error( "Could not commit lock for index file" ) ]
14+ CommitLock ( #[ from] git_lock:: commit:: Error < git_lock:: File > ) ,
15+ }
16+
517impl File {
618 /// Write the index to `out` with `options`, to be readable by [`File::at()`], returning the version that was actually written
719 /// to retain all information of this index.
8- pub fn write_to ( & self , mut out : impl std:: io:: Write , options : write:: Options ) -> std:: io:: Result < Version > {
9- let mut hasher = hash:: Write :: new ( & mut out, options. hash_kind ) ;
20+ pub fn write_to (
21+ & self ,
22+ mut out : impl std:: io:: Write ,
23+ options : write:: Options ,
24+ ) -> std:: io:: Result < ( Version , git_hash:: ObjectId ) > {
25+ let mut hasher = hash:: Write :: new ( & mut out, self . state . object_hash ) ;
1026 let version = self . state . write_to ( & mut hasher, options) ?;
1127
1228 let hash = hasher. hash . digest ( ) ;
1329 out. write_all ( & hash) ?;
14- Ok ( version)
30+ Ok ( ( version, git_hash:: ObjectId :: from ( hash) ) )
31+ }
32+
33+ /// Write ourselves to the path we were read from after acquiring a lock, using `options`.
34+ ///
35+ /// Note that the hash produced will be stored which is why we need to be mutable.
36+ pub fn write ( & mut self , options : write:: Options ) -> Result < ( ) , Error > {
37+ let mut lock = std:: io:: BufWriter :: new ( git_lock:: File :: acquire_to_update_resource (
38+ & self . path ,
39+ git_lock:: acquire:: Fail :: Immediately ,
40+ None ,
41+ ) ?) ;
42+ let ( version, digest) = self . write_to ( & mut lock, options) ?;
43+ match lock. into_inner ( ) {
44+ Ok ( lock) => lock. commit ( ) ?,
45+ Err ( err) => return Err ( err. into_error ( ) . into ( ) ) ,
46+ } ;
47+ self . state . version = version;
48+ self . checksum = Some ( digest) ;
49+ Ok ( ( ) )
1550 }
1651}
0 commit comments