Skip to content

Commit dd60136

Browse files
committed
index: Make _read() cope with index file creation
1 parent 5aeef5a commit dd60136

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/index.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ int git_index_read(git_index *index)
461461
return create_index_error(-1,
462462
"Failed to read index: The index is in-memory only");
463463

464-
if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
464+
index->on_disk = git_path_exists(index->index_file_path);
465+
466+
if (!index->on_disk) {
465467
git_index_clear(index);
466-
index->on_disk = 0;
467468
return 0;
468469
}
469470

tests-clar/index/tests.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,53 @@ void test_index_tests__elocked(void)
484484
git_index_free(index);
485485
git_repository_free(repo);
486486
}
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

Comments
 (0)