summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2017-11-12 08:23:13 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2018-06-29 14:54:27 +0100
commitdc4a18c7455e1696b2056a02f934e1f7ac80d940 (patch)
tree07f4fe1b6231888b22b39d86b7f246d200377525
parent7c56c49be58091f4d4c5fbe96429694e80befd0b (diff)
downloadlibgit2-dc4a18c7455e1696b2056a02f934e1f7ac80d940.tar.gz
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.
-rw-r--r--tests/index/tests.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/index/tests.c b/tests/index/tests.c
index b993fb263..c2e48c9fd 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;