Some vector utility tweaks This is just laying some groundwork for internal index changes that I'm working on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
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);
}