@@ -9,12 +9,11 @@ use std::path::{Path, PathBuf};
99use std:: time:: { SystemTime , UNIX_EPOCH } ;
1010
1111use byteorder:: { BigEndian , ReadBytesExt , WriteBytesExt } ;
12- use sha1:: { Digest , Sha1 } ;
1312
1413use crate :: errors:: GitError ;
1514use crate :: hash:: { ObjectHash , get_hash_kind} ;
1615use crate :: internal:: pack:: wrapper:: Wrapper ;
17- use crate :: utils:: { self , Hashalgorithm } ;
16+ use crate :: utils:: { self , HashAlgorithm } ;
1817
1918#[ derive( PartialEq , Eq , Debug , Clone ) ]
2019pub struct Time {
@@ -281,15 +280,16 @@ impl Index {
281280 . insert ( ( entry. name . clone ( ) , entry. flags . stage ) , entry) ;
282281
283282 // 1-8 nul bytes as necessary to pad the entry to a multiple of eight bytes
284- // while keeping the name NUL-terminated. // so at least 1 byte nul
283+ // while keeping the name NUL-terminated.
285284 let hash_len = get_hash_kind ( ) . size ( ) ;
286- let padding = ( 8 - ( ( hash_len + 2 + name_len) % 8 ) ) % 8 ; // 2 for flags
285+ let entry_len = hash_len + 2 + name_len;
286+ let padding = 1 + ( ( 8 - ( ( entry_len + 1 ) % 8 ) ) % 8 ) ; // at least 1 byte nul
287287 utils:: read_bytes ( file, padding) ?;
288288 }
289289
290290 // Extensions
291291 while file. bytes_read ( ) + get_hash_kind ( ) . size ( ) < total_size as usize {
292- // The remaining 20 bytes must be checksum
292+ // The remaining bytes must be the pack checksum (size = get_hash_kind().size())
293293 let sign = utils:: read_bytes ( file, 4 ) ?;
294294 println ! (
295295 "{:?}" ,
@@ -321,7 +321,7 @@ impl Index {
321321
322322 pub fn to_file ( & self , path : impl AsRef < Path > ) -> Result < ( ) , GitError > {
323323 let mut file = File :: create ( path) ?;
324- let mut hash = Sha1 :: new ( ) ;
324+ let mut hash = HashAlgorithm :: new ( ) ;
325325
326326 let mut header = Vec :: new ( ) ;
327327 header. write_all ( b"DIRC" ) ?;
@@ -342,21 +342,23 @@ impl Index {
342342 entry_bytes. write_u32 :: < BigEndian > ( entry. uid ) ?;
343343 entry_bytes. write_u32 :: < BigEndian > ( entry. gid ) ?;
344344 entry_bytes. write_u32 :: < BigEndian > ( entry. size ) ?;
345- entry_bytes. write_all ( & entry. hash . as_ref ( ) ) ?;
345+ entry_bytes. write_all ( entry. hash . as_ref ( ) ) ?;
346346 entry_bytes. write_u16 :: < BigEndian > ( ( & entry. flags ) . try_into ( ) . unwrap ( ) ) ?;
347347 entry_bytes. write_all ( entry. name . as_bytes ( ) ) ?;
348- let padding = 8 - ( ( 22 + entry. name . len ( ) ) % 8 ) ;
348+ let hash_len = get_hash_kind ( ) . size ( ) ;
349+ let entry_len = hash_len + 2 + entry. name . len ( ) ;
350+ let padding = 1 + ( ( 8 - ( ( entry_len + 1 ) % 8 ) ) % 8 ) ; // at least 1 byte nul
349351 entry_bytes. write_all ( & vec ! [ 0 ; padding] ) ?;
350-
351352 file. write_all ( & entry_bytes) ?;
352353 hash. update ( & entry_bytes) ;
353354 }
354355
355356 // Extensions
356357
357358 // check sum
358- let file_hash: [ u8 ; 20 ] = hash. finalize ( ) . into ( ) ;
359- file. write_all ( & file_hash) ?;
359+ let file_hash =
360+ ObjectHash :: from_bytes ( & hash. finalize ( ) ) . map_err ( GitError :: InvalidIndexFile ) ?;
361+ file. write_all ( file_hash. as_ref ( ) ) ?;
360362 Ok ( ( ) )
361363 }
362364
@@ -384,7 +386,7 @@ impl Index {
384386
385387 // re-calculate SHA1/SHA256
386388 let mut file = File :: open ( & abs_path) ?;
387- let mut hasher = Hashalgorithm :: new ( ) ;
389+ let mut hasher = HashAlgorithm :: new ( ) ;
388390 io:: copy ( & mut file, & mut hasher) ?;
389391 let new_hash = ObjectHash :: from_bytes ( & hasher. finalize ( ) ) . unwrap ( ) ;
390392
0 commit comments