Commit dc4a18c7455e1696b2056a02f934e1f7ac80d940

Edward Thomson 2017-11-12T08:23:13

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.

diff --git a/tests/index/tests.c b/tests/index/tests.c
index b993fb2..c2e48c9 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -331,6 +331,59 @@ void test_index_tests__add_frombuffer(void)
 	git_repository_free(repo);
 }
 
+void test_index_tests__dirty_and_clean(void)
+{
+	git_repository *repo;
+	git_index *index;
+	git_index_entry entry = {{0}};
+
+	/* Index is not dirty after opening */
+	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
+	cl_git_pass(git_repository_index(&index, repo));
+
+	cl_assert(git_index_entrycount(index) == 0);
+	cl_assert(!git_index_is_dirty(index));
+
+	/* Index is dirty after adding an entry */
+	entry.mode = GIT_FILEMODE_BLOB;
+	entry.path = "test.txt";
+	cl_git_pass(git_index_add_frombuffer(index, &entry, "Hi.\n", 4));
+	cl_assert(git_index_entrycount(index) == 1);
+	cl_assert(git_index_is_dirty(index));
+
+	/* Index is not dirty after write */
+	cl_git_pass(git_index_write(index));
+	cl_assert(!git_index_is_dirty(index));
+
+	/* Index is dirty after removing an entry */
+	cl_git_pass(git_index_remove_bypath(index, "test.txt"));
+	cl_assert(git_index_entrycount(index) == 0);
+	cl_assert(git_index_is_dirty(index));
+
+	/* Index is not dirty after write */
+	cl_git_pass(git_index_write(index));
+	cl_assert(!git_index_is_dirty(index));
+
+	/* Index remains not dirty after read */
+	cl_git_pass(git_index_read(index, 0));
+	cl_assert(!git_index_is_dirty(index));
+
+	/* Index is dirty when we do an unforced read with dirty content */
+	cl_git_pass(git_index_add_frombuffer(index, &entry, "Hi.\n", 4));
+	cl_assert(git_index_entrycount(index) == 1);
+	cl_assert(git_index_is_dirty(index));
+
+	cl_git_pass(git_index_read(index, 0));
+	cl_assert(git_index_is_dirty(index));
+
+	/* Index is clean when we force a read with dirty content */
+	cl_git_pass(git_index_read(index, 1));
+	cl_assert(!git_index_is_dirty(index));
+
+	git_index_free(index);
+	git_repository_free(repo);
+}
+
 void test_index_tests__add_frombuffer_reset_entry(void)
 {
 	git_index *index;