Commit 43709ca87811efc3c237eb719611f025502f3928

Russell Belfer 2014-02-04T10:33:30

Fix typo setting sorted flag when reloading index This fixes a typo I made for setting the sorted flag on the index after a reload. That typo didn't actually cause any test failures so I'm also adding a test that explicitly checks that the index is correctly sorted after a reload when ignoring case and when not.

diff --git a/src/index.c b/src/index.c
index 1ab126c..42eb5fd 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1811,8 +1811,10 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 
 #undef seek_forward
 
-	/* Entries are stored case-sensitively on disk. */
-	git_vector_set_sorted(&index->entries, index->ignore_case);
+	/* Entries are stored case-sensitively on disk, so re-sort now if
+	 * in-memory index is supposed to be case-insensitive
+	 */
+	git_vector_set_sorted(&index->entries, !index->ignore_case);
 	git_vector_sort(&index->entries);
 
 	return 0;
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 55a2f2c..6e28af1 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -543,3 +543,37 @@ void test_index_tests__corrupted_extension(void)
 
 	cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
 }
+
+static void assert_index_is_sorted(git_index *index)
+{
+	git_vector *entries = &index->entries;
+	size_t i;
+
+	cl_assert(git_vector_is_sorted(entries));
+
+	for (i = 1; i < git_vector_length(entries); ++i) {
+		git_index_entry *prev = git_vector_get(entries, i - 1);
+		git_index_entry *curr = git_vector_get(entries, i);
+		cl_assert(index->entries._cmp(prev, curr) <= 0);
+	}
+}
+
+void test_index_tests__reload_while_ignoring_case(void)
+{
+	git_index *index;
+	unsigned int caps;
+
+	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
+	assert_index_is_sorted(index);
+
+	caps = git_index_caps(index);
+	cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEXCAP_IGNORE_CASE));
+	cl_git_pass(git_index_read(index, true));
+	assert_index_is_sorted(index);
+
+	cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
+	cl_git_pass(git_index_read(index, true));
+	assert_index_is_sorted(index);
+
+	git_index_free(index);
+}