Skip to content

Commit dc4a18c

Browse files
committed
index: test dirty index bit
Test that any changes to the index will mark the index as dirty. Also ensure that when we initialize a new index, read the index contents from disk, or write the index contents to disk that we reset the dirty flag to zero. Further ensure that an unforced read with dirty contents (when the on-disk index has not changed) does _not_ reset the dirty flag as we have not updated the contents of our index and our unsaved contents remain intact.
1 parent 7c56c49 commit dc4a18c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/index/tests.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,59 @@ void test_index_tests__add_frombuffer(void)
331331
git_repository_free(repo);
332332
}
333333

334+
void test_index_tests__dirty_and_clean(void)
335+
{
336+
git_repository *repo;
337+
git_index *index;
338+
git_index_entry entry = {{0}};
339+
340+
/* Index is not dirty after opening */
341+
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
342+
cl_git_pass(git_repository_index(&index, repo));
343+
344+
cl_assert(git_index_entrycount(index) == 0);
345+
cl_assert(!git_index_is_dirty(index));
346+
347+
/* Index is dirty after adding an entry */
348+
entry.mode = GIT_FILEMODE_BLOB;
349+
entry.path = "test.txt";
350+
cl_git_pass(git_index_add_frombuffer(index, &entry, "Hi.\n", 4));
351+
cl_assert(git_index_entrycount(index) == 1);
352+
cl_assert(git_index_is_dirty(index));
353+
354+
/* Index is not dirty after write */
355+
cl_git_pass(git_index_write(index));
356+
cl_assert(!git_index_is_dirty(index));
357+
358+
/* Index is dirty after removing an entry */
359+
cl_git_pass(git_index_remove_bypath(index, "test.txt"));
360+
cl_assert(git_index_entrycount(index) == 0);
361+
cl_assert(git_index_is_dirty(index));
362+
363+
/* Index is not dirty after write */
364+
cl_git_pass(git_index_write(index));
365+
cl_assert(!git_index_is_dirty(index));
366+
367+
/* Index remains not dirty after read */
368+
cl_git_pass(git_index_read(index, 0));
369+
cl_assert(!git_index_is_dirty(index));
370+
371+
/* Index is dirty when we do an unforced read with dirty content */
372+
cl_git_pass(git_index_add_frombuffer(index, &entry, "Hi.\n", 4));
373+
cl_assert(git_index_entrycount(index) == 1);
374+
cl_assert(git_index_is_dirty(index));
375+
376+
cl_git_pass(git_index_read(index, 0));
377+
cl_assert(git_index_is_dirty(index));
378+
379+
/* Index is clean when we force a read with dirty content */
380+
cl_git_pass(git_index_read(index, 1));
381+
cl_assert(!git_index_is_dirty(index));
382+
383+
git_index_free(index);
384+
git_repository_free(repo);
385+
}
386+
334387
void test_index_tests__add_frombuffer_reset_entry(void)
335388
{
336389
git_index *index;

0 commit comments

Comments
 (0)