summaryrefslogtreecommitdiff
path: root/tests/index/tests.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2018-06-22 19:17:08 +0900
committerEdward Thomson <ethomson@edwardthomson.com>2018-06-29 14:54:29 +0100
commitbfa1f02292d1b637928fe18663a5f2d0aa993400 (patch)
tree0d08a6eb80acf9b6114d2f817cd9e8cd711daf10 /tests/index/tests.c
parent787768c2d70dfcd1c9ebc1854b5d0f67d2e6d4d9 (diff)
downloadlibgit2-bfa1f02292d1b637928fe18663a5f2d0aa993400.tar.gz
settings: optional unsaved index safety
Add the `GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY` option, which will cause commands that reload the on-disk index to fail if the current `git_index` has changed that have not been saved. This will prevent users from - for example - adding a file to the index then calling a function like `git_checkout` and having that file be silently removed from the index since it was re-read from disk. Now calls that would re-read the index will fail if the index is "dirty", meaning changes have been made to it but have not been written. Users can either `git_index_read` to discard those changes explicitly, or `git_index_write` to write them.
Diffstat (limited to 'tests/index/tests.c')
-rw-r--r--tests/index/tests.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/tests/index/tests.c b/tests/index/tests.c
index d7a1cc50b..3605ac9c3 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -72,6 +72,11 @@ void test_index_tests__initialize(void)
{
}
+void test_index_tests__cleanup(void)
+{
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 0));
+}
+
void test_index_tests__empty_index(void)
{
git_index *index;
@@ -384,7 +389,7 @@ void test_index_tests__dirty_and_clean(void)
git_repository_free(repo);
}
-void test_index_tests__dirty_fails_with_error(void)
+void test_index_tests__dirty_fails_optionally(void)
{
git_repository *repo;
git_index *index;
@@ -400,6 +405,15 @@ void test_index_tests__dirty_fails_with_error(void)
cl_git_pass(git_index_add_frombuffer(index, &entry, "Hi.\n", 4));
cl_assert(git_index_is_dirty(index));
+ cl_git_pass(git_checkout_head(repo, NULL));
+
+ /* Index is dirty (again) 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_is_dirty(index));
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 1));
cl_git_fail_with(GIT_EINDEXDIRTY, git_checkout_head(repo, NULL));
git_index_free(index);