Commit c67fd4c9d5e1ff715df28b884d7f7f9f20fad1ec

Russell Belfer 2014-02-07T11:20:36

Some vector utility tweaks This is just laying some groundwork for internal index changes that I'm working on.

diff --git a/src/checkout.c b/src/checkout.c
index 141fc13..f9bc5e9 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -653,10 +653,13 @@ static int checkout_conflictdata_cmp(const void *a, const void *b)
 	return diff;
 }
 
-int checkout_conflictdata_empty(const git_vector *conflicts, size_t idx)
+int checkout_conflictdata_empty(
+	const git_vector *conflicts, size_t idx, void *payload)
 {
 	checkout_conflictdata *conflict;
 
+	GIT_UNUSED(payload);
+
 	if ((conflict = git_vector_get(conflicts, idx)) == NULL)
 		return -1;
 
@@ -954,7 +957,8 @@ static int checkout_conflicts_coalesce_renames(
 			ancestor_conflict->one_to_two = 1;
 	}
 
-	git_vector_remove_matching(&data->conflicts, checkout_conflictdata_empty);
+	git_vector_remove_matching(
+		&data->conflicts, checkout_conflictdata_empty, NULL);
 
 done:
 	return error;
diff --git a/src/index.c b/src/index.c
index b0b5eae..08c4b79 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1228,10 +1228,13 @@ int git_index_conflict_remove(git_index *index, const char *path)
 	return 0;
 }
 
-static int index_conflicts_match(const git_vector *v, size_t idx)
+static int index_conflicts_match(const git_vector *v, size_t idx, void *p)
 {
+	git_index *index = p;
 	git_index_entry *entry = git_vector_get(v, idx);
 
+	GIT_UNUSED(index);
+
 	if (GIT_IDXENTRY_STAGE(entry) > 0) {
 		index_entry_free(entry);
 		return 1;
@@ -1243,7 +1246,7 @@ static int index_conflicts_match(const git_vector *v, size_t idx)
 void git_index_conflict_cleanup(git_index *index)
 {
 	assert(index);
-	git_vector_remove_matching(&index->entries, index_conflicts_match);
+	git_vector_remove_matching(&index->entries, index_conflicts_match, index);
 }
 
 int git_index_has_conflicts(const git_index *index)
diff --git a/src/merge.c b/src/merge.c
index 10c19b5..9c4a07b 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -995,10 +995,12 @@ static void merge_diff_list_coalesce_renames(
 	}
 }
 
-static int merge_diff_empty(const git_vector *conflicts, size_t idx)
+static int merge_diff_empty(const git_vector *conflicts, size_t idx, void *p)
 {
 	git_merge_diff *conflict = conflicts->contents[idx];
 
+	GIT_UNUSED(p);
+
 	return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) &&
 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) &&
 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry));
@@ -1079,7 +1081,7 @@ int git_merge_diff_list__find_renames(
 	merge_diff_list_coalesce_renames(diff_list, similarity_ours, similarity_theirs, opts);
 
 	/* And remove any entries that were merged and are now empty. */
-	git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty);
+	git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty, NULL);
 
 done:
 	if (cache != NULL) {
diff --git a/src/vector.c b/src/vector.c
index 37ea07f..9f7eed5 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -276,14 +276,16 @@ void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *))
 }
 
 void git_vector_remove_matching(
-	git_vector *v, int (*match)(const git_vector *v, size_t idx))
+	git_vector *v,
+	int (*match)(const git_vector *v, size_t idx, void *payload),
+	void *payload)
 {
 	size_t i, j;
 
 	for (i = 0, j = 0; j < v->length; ++j) {
 		v->contents[i] = v->contents[j];
 
-		if (!match(v, i))
+		if (!match(v, i, payload))
 			i++;
 	}
 
@@ -339,3 +341,18 @@ int git_vector_set(void **old, git_vector *v, size_t position, void *value)
 
 	return 0;
 }
+
+int git_vector_verify_sorted(const git_vector *v)
+{
+	size_t i;
+
+	if (!git_vector_is_sorted(v))
+		return -1;
+
+	for (i = 1; i < v->length; ++i) {
+		if (v->_cmp(v->contents[i - 1], v->contents[i]) > 0)
+			return -1;
+	}
+
+	return 0;
+}
diff --git a/src/vector.h b/src/vector.h
index 682b6ad..aac46c4 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -85,8 +85,11 @@ int git_vector_insert_sorted(git_vector *v, void *element,
 int git_vector_remove(git_vector *v, size_t idx);
 void git_vector_pop(git_vector *v);
 void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *));
+
 void git_vector_remove_matching(
-	git_vector *v, int (*match)(const git_vector *v, size_t idx));
+	git_vector *v,
+	int (*match)(const git_vector *v, size_t idx, void *payload),
+	void *payload);
 
 int git_vector_resize_to(git_vector *v, size_t new_length);
 int git_vector_set(void **old, git_vector *v, size_t position, void *value);
@@ -108,4 +111,7 @@ GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
 	}
 }
 
+/* Just use this in tests, not for realz. returns -1 if not sorted */
+int git_vector_verify_sorted(const git_vector *v);
+
 #endif
diff --git a/tests/core/vector.c b/tests/core/vector.c
index db52c00..66f90b8 100644
--- a/tests/core/vector.c
+++ b/tests/core/vector.c
@@ -190,8 +190,9 @@ void test_core_vector__5(void)
 	git_vector_free(&x);
 }
 
-static int remove_ones(const git_vector *v, size_t idx)
+static int remove_ones(const git_vector *v, size_t idx, void *p)
 {
+	GIT_UNUSED(p);
 	return (git_vector_get(v, idx) == (void *)0x001);
 }
 
@@ -206,7 +207,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 1);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 0);
 
 	git_vector_insert(&x, (void*) 0x001);
@@ -214,7 +215,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 3);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 0);
 
 	git_vector_insert(&x, (void*) 0x002);
@@ -223,7 +224,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -238,7 +239,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -253,7 +254,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x001);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 2);
 
 	git_vector_foreach(&x, i, compare) {
@@ -268,7 +269,7 @@ void test_core_vector__remove_matching(void)
 	git_vector_insert(&x, (void*) 0x003);
 
 	cl_assert(x.length == 4);
-	git_vector_remove_matching(&x, remove_ones);
+	git_vector_remove_matching(&x, remove_ones, NULL);
 	cl_assert(x.length == 4);
 
 	git_vector_free(&x);
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 6e28af1..fa5c0bb 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -544,36 +544,22 @@ 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);
+	cl_git_pass(git_vector_verify_sorted(&index->entries));
 
 	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_vector_verify_sorted(&index->entries));
 
 	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_vector_verify_sorted(&index->entries));
 
 	git_index_free(index);
 }