Skip to content

Commit 7f91191

Browse files
author
Vicent Martí
committed
Merge pull request libgit2#1892 from libgit2/ntk/topic/index_read
Make git_index_read() cope with external additions and removals of the index file
2 parents 185dce6 + dd60136 commit 7f91191

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

include/git2/index.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
225225
* Update the contents of an existing index object in memory
226226
* by reading from the hard disk.
227227
*
228+
* If the file doesn't exist on the filesystem, the index
229+
* will be cleared from its current content.
230+
*
228231
* @param index an existing index object
229232
* @return 0 or an error code
230233
*/

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: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ void test_index_tests__default_test_index(void)
9999
entries = (git_index_entry **)index->entries.contents;
100100

101101
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
102-
git_index_entry *e = entries[test_entries[i].index];
102+
git_index_entry *e = entries[test_entries[i].index];
103103

104-
cl_assert_equal_s(e->path, test_entries[i].path);
105-
cl_assert(e->mtime.seconds == test_entries[i].mtime);
106-
cl_assert(e->file_size == test_entries[i].file_size);
104+
cl_assert_equal_s(e->path, test_entries[i].path);
105+
cl_assert(e->mtime.seconds == test_entries[i].mtime);
106+
cl_assert(e->file_size == test_entries[i].file_size);
107107
}
108108

109109
git_index_free(index);
@@ -131,10 +131,10 @@ void test_index_tests__find_in_existing(void)
131131
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
132132

133133
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
134-
size_t idx;
134+
size_t idx;
135135

136-
cl_assert(!git_index_find(&idx, index, test_entries[i].path));
137-
cl_assert(idx == test_entries[i].index);
136+
cl_assert(!git_index_find(&idx, index, test_entries[i].path));
137+
cl_assert(idx == test_entries[i].index);
138138
}
139139

140140
git_index_free(index);
@@ -148,7 +148,7 @@ void test_index_tests__find_in_empty(void)
148148
cl_git_pass(git_index_open(&index, "fake-index"));
149149

150150
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
151-
cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path));
151+
cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path));
152152
}
153153

154154
git_index_free(index);
@@ -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)