@@ -87,9 +87,8 @@ impl<'repo> PackBuilder<'repo> {
8787 Ok ( ( ) )
8888 }
8989
90- /// Write the contents of the packfile to the specified path. The contents
91- /// of the buffer will become a valid packfile, even though there will be
92- /// no attached index.
90+ /// Write the new pack and corresponding index file to path.
91+ /// To set a progress callback, use `set_progress_callback` before calling this method.
9392 pub fn write ( & mut self , path : & Path , mode : u32 ) -> Result < ( ) , Error > {
9493 let path = path. into_c_string ( ) ?;
9594 let progress_cb: raw:: git_indexer_progress_cb = Some ( write_pack_progress_cb) ;
@@ -294,7 +293,10 @@ extern "C" fn progress_c(
294293
295294#[ cfg( test) ]
296295mod tests {
297- use crate :: Buf ;
296+ use crate :: { Buf , Oid } ;
297+
298+ // hash of a packfile constructed without any objects in it
299+ const EMPTY_PACKFILE_OID : & str = "029d08823bd8a8eab510ad6ac75c823cfd3ed31e" ;
298300
299301 fn pack_header ( len : u8 ) -> Vec < u8 > {
300302 [ ] . iter ( )
@@ -338,6 +340,18 @@ mod tests {
338340 assert_eq ! ( & * buf, & * empty_pack_header( ) ) ;
339341 }
340342
343+ #[ test]
344+ fn smoke_write ( ) {
345+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
346+ let mut builder = t ! ( repo. packbuilder( ) ) ;
347+ t ! ( builder. write( repo. path( ) , 0 ) ) ;
348+ #[ allow( deprecated) ]
349+ {
350+ assert ! ( builder. hash( ) . unwrap( ) == Oid :: from_str( EMPTY_PACKFILE_OID ) . unwrap( ) ) ;
351+ }
352+ assert ! ( builder. name( ) . unwrap( ) == EMPTY_PACKFILE_OID ) ;
353+ }
354+
341355 #[ test]
342356 fn smoke_foreach ( ) {
343357 let ( _td, repo) = crate :: test:: repo_init ( ) ;
@@ -391,6 +405,41 @@ mod tests {
391405 assert_eq ! ( & buf[ 0 ..12 ] , & * pack_header( 3 ) ) ;
392406 }
393407
408+ #[ test]
409+ fn insert_write ( ) {
410+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
411+ let mut builder = t ! ( repo. packbuilder( ) ) ;
412+ let ( commit, _tree) = crate :: test:: commit ( & repo) ;
413+ t ! ( builder. insert_object( commit, None ) ) ;
414+ assert_eq ! ( builder. object_count( ) , 1 ) ;
415+ t ! ( builder. write( repo. path( ) , 0 ) ) ;
416+ t ! ( repo. find_commit( commit) ) ;
417+ }
418+
419+ #[ test]
420+ fn insert_tree_write ( ) {
421+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
422+ let mut builder = t ! ( repo. packbuilder( ) ) ;
423+ let ( _commit, tree) = crate :: test:: commit ( & repo) ;
424+ // will insert the tree itself and the blob, 2 objects
425+ t ! ( builder. insert_tree( tree) ) ;
426+ assert_eq ! ( builder. object_count( ) , 2 ) ;
427+ t ! ( builder. write( repo. path( ) , 0 ) ) ;
428+ t ! ( repo. find_tree( tree) ) ;
429+ }
430+
431+ #[ test]
432+ fn insert_commit_write ( ) {
433+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
434+ let mut builder = t ! ( repo. packbuilder( ) ) ;
435+ let ( commit, _tree) = crate :: test:: commit ( & repo) ;
436+ // will insert the commit, its tree and the blob, 3 objects
437+ t ! ( builder. insert_commit( commit) ) ;
438+ assert_eq ! ( builder. object_count( ) , 3 ) ;
439+ t ! ( builder. write( repo. path( ) , 0 ) ) ;
440+ t ! ( repo. find_commit( commit) ) ;
441+ }
442+
394443 #[ test]
395444 fn progress_callback ( ) {
396445 let mut progress_called = false ;
@@ -426,6 +475,23 @@ mod tests {
426475 assert_eq ! ( progress_called, false ) ;
427476 }
428477
478+ #[ test]
479+ fn progress_callback_with_write ( ) {
480+ let mut progress_called = false ;
481+ {
482+ let ( _td, repo) = crate :: test:: repo_init ( ) ;
483+ let mut builder = t ! ( repo. packbuilder( ) ) ;
484+ let ( commit, _tree) = crate :: test:: commit ( & repo) ;
485+ t ! ( builder. set_progress_callback( |_, _, _| {
486+ progress_called = true ;
487+ true
488+ } ) ) ;
489+ t ! ( builder. insert_commit( commit) ) ;
490+ t ! ( builder. write( repo. path( ) , 0 ) ) ;
491+ }
492+ assert_eq ! ( progress_called, true ) ;
493+ }
494+
429495 #[ test]
430496 fn set_threads ( ) {
431497 let ( _td, repo) = crate :: test:: repo_init ( ) ;
0 commit comments