@@ -484,3 +484,53 @@ void test_index_tests__elocked(void)
484
484
git_index_free (index );
485
485
git_repository_free (repo );
486
486
}
487
+
488
+ void test_index_tests__reload_from_disk (void )
489
+ {
490
+ git_repository * repo ;
491
+ git_index * read_index ;
492
+ git_index * write_index ;
493
+
494
+ cl_set_cleanup (& cleanup_myrepo , NULL );
495
+
496
+ cl_git_pass (git_futils_mkdir ("./myrepo" , NULL , 0777 , GIT_MKDIR_PATH ));
497
+ cl_git_mkfile ("./myrepo/a.txt" , "a\n" );
498
+ cl_git_mkfile ("./myrepo/b.txt" , "b\n" );
499
+
500
+ cl_git_pass (git_repository_init (& repo , "./myrepo" , 0 ));
501
+ cl_git_pass (git_repository_index (& write_index , repo ));
502
+ cl_assert_equal_i (false, write_index -> on_disk );
503
+
504
+ cl_git_pass (git_index_open (& read_index , write_index -> index_file_path ));
505
+ cl_assert_equal_i (false, read_index -> on_disk );
506
+
507
+ /* Stage two new files agaisnt the write_index */
508
+ cl_git_pass (git_index_add_bypath (write_index , "a.txt" ));
509
+ cl_git_pass (git_index_add_bypath (write_index , "b.txt" ));
510
+
511
+ cl_assert_equal_sz (2 , git_index_entrycount (write_index ));
512
+
513
+ /* Persist the index changes to disk */
514
+ cl_git_pass (git_index_write (write_index ));
515
+ cl_assert_equal_i (true, write_index -> on_disk );
516
+
517
+ /* Sync the changes back into the read_index */
518
+ cl_assert_equal_sz (0 , git_index_entrycount (read_index ));
519
+
520
+ cl_git_pass (git_index_read (read_index ));
521
+ cl_assert_equal_i (true, read_index -> on_disk );
522
+
523
+ cl_assert_equal_sz (2 , git_index_entrycount (read_index ));
524
+
525
+ /* Remove the index file from the filesystem */
526
+ cl_git_pass (p_unlink (write_index -> index_file_path ));
527
+
528
+ /* Sync the changes back into the read_index */
529
+ cl_git_pass (git_index_read (read_index ));
530
+ cl_assert_equal_i (false, read_index -> on_disk );
531
+ cl_assert_equal_sz (0 , git_index_entrycount (read_index ));
532
+
533
+ git_index_free (read_index );
534
+ git_index_free (write_index );
535
+ git_repository_free (repo );
536
+ }
0 commit comments