oid-array: teach oid-array to handle multiple kinds of oids

While looking at how to handle input of both SHA-1 and SHA-256 oids in get_oid_with_context, I realized that the oid_array in repo_for_each_abbrev might have more than one kind of oid stored in it simultaneously. Update to oid_array_append to ensure that oids added to an oid array always have an algorithm set. Update void_hashcmp to first verify two oids use the same hash algorithm before comparing them to each other. With that oid-array should be safe to use with different kinds of oids simultaneously. Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric W. Biederman committed Oct 1, 2023 at 21:40 UTC d50cbe4a5d989c454c620cbaa0c64a06a4a2f9a2
1 file changed +10 -2
oid-array.c
+10 -2
@@ -6,12 +6,20 @@ void oid_array_append(struct oid_array *array, const struct object_id *oid)
6 {
7 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
8 oidcpy(&array->oid[array->nr++], oid);
9 + if (!oid->algo)
10 + oid_set_algo(&array->oid[array->nr - 1], the_hash_algo);
11 array->sorted = 0;
12 }
13
12 -static int void_hashcmp(const void *a, const void *b)
14 +static int void_hashcmp(const void *va, const void *vb)
15 {
14 - return oidcmp(a, b);
16 + const struct object_id *a = va, *b = vb;
17 + int ret;
18 + if (a->algo == b->algo)
19 + ret = oidcmp(a, b);
20 + else
21 + ret = a->algo > b->algo ? 1 : -1;
22 + return ret;
23 }
24
25 void oid_array_sort(struct oid_array *array)