commit-reach: use `size_t` to track indices in `remove_redundant()`

The function `remove_redundant()` gets as input an array of commits as well as the size of that array and then drops redundant commits from that array. It then returns either `-1` in case an error occurred, or the new number of items in the array. The function receives and returns these sizes with a signed integer, which causes several warnings with -Wsign-compare. Fix this issue by consistently using `size_t` to track array indices and splitting up the returned value into a returned error code and a separate out pointer for the new computed size. Note that `get_merge_bases_many()` and related functions still track array sizes as a signed integer. This will be fixed in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 27, 2024 at 11:46 UTC 45843d8f4eb2bbfc73cc361ba9d612d088dc8a4f
1 file changed +30 -23
commit-reach.c
+30 -23
@@ -212,12 +212,13 @@ int get_octopus_merge_bases(struct commit_list *in, struct commit_list **result)
212 }
213
214 static int remove_redundant_no_gen(struct repository *r,
215 - struct commit **array, int cnt)
215 + struct commit **array,
216 + size_t cnt, size_t *dedup_cnt)
217 {
218 struct commit **work;
219 unsigned char *redundant;
219 - int *filled_index;
220 - int i, j, filled;
220 + size_t *filled_index;
221 + size_t i, j, filled;
222
223 CALLOC_ARRAY(work, cnt);
224 redundant = xcalloc(cnt, 1);
@@ -267,20 +268,22 @@ static int remove_redundant_no_gen(struct repository *r,
268 for (i = filled = 0; i < cnt; i++)
269 if (!redundant[i])
270 array[filled++] = work[i];
271 + *dedup_cnt = filled;
272 free(work);
273 free(redundant);
274 free(filled_index);
273 - return filled;
275 + return 0;
276 }
277
278 static int remove_redundant_with_gen(struct repository *r,
277 - struct commit **array, int cnt)
279 + struct commit **array, size_t cnt,
280 + size_t *dedup_cnt)
281 {
279 - int i, count_non_stale = 0, count_still_independent = cnt;
282 + size_t i, count_non_stale = 0, count_still_independent = cnt;
283 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
284 struct commit **walk_start, **sorted;
285 size_t walk_start_nr = 0, walk_start_alloc = cnt;
283 - int min_gen_pos = 0;
286 + size_t min_gen_pos = 0;
287
288 /*
289 * Sort the input by generation number, ascending. This allows
@@ -326,12 +329,12 @@ static int remove_redundant_with_gen(struct repository *r,
329 * terminate early. Otherwise, we will do the same amount of work
330 * as before.
331 */
329 - for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
332 + for (i = walk_start_nr; i && count_still_independent > 1; i--) {
333 /* push the STALE bits up to min generation */
334 struct commit_list *stack = NULL;
335
333 - commit_list_insert(walk_start[i], &stack);
334 - walk_start[i]->object.flags |= STALE;
336 + commit_list_insert(walk_start[i - 1], &stack);
337 + walk_start[i - 1]->object.flags |= STALE;
338
339 while (stack) {
340 struct commit_list *parents;
@@ -388,10 +391,12 @@ static int remove_redundant_with_gen(struct repository *r,
391 clear_commit_marks_many(walk_start_nr, walk_start, STALE);
392 free(walk_start);
393
391 - return count_non_stale;
394 + *dedup_cnt = count_non_stale;
395 + return 0;
396 }
397
394 -static int remove_redundant(struct repository *r, struct commit **array, int cnt)
398 +static int remove_redundant(struct repository *r, struct commit **array,
399 + size_t cnt, size_t *dedup_cnt)
400 {
401 /*
402 * Some commit in the array may be an ancestor of
@@ -401,19 +406,17 @@ static int remove_redundant(struct repository *r, struct commit **array, int cnt
406 * that number.
407 */
408 if (generation_numbers_enabled(r)) {
404 - int i;
405 -
409 /*
410 * If we have a single commit with finite generation
411 * number, then the _with_gen algorithm is preferred.
412 */
410 - for (i = 0; i < cnt; i++) {
413 + for (size_t i = 0; i < cnt; i++) {
414 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
412 - return remove_redundant_with_gen(r, array, cnt);
415 + return remove_redundant_with_gen(r, array, cnt, dedup_cnt);
416 }
417 }
418
416 - return remove_redundant_no_gen(r, array, cnt);
419 + return remove_redundant_no_gen(r, array, cnt, dedup_cnt);
420 }
421
422 static int get_merge_bases_many_0(struct repository *r,
@@ -425,7 +428,8 @@ static int get_merge_bases_many_0(struct repository *r,
428 {
429 struct commit_list *list;
430 struct commit **rslt;
428 - int cnt, i;
431 + size_t cnt, i;
432 + int ret;
433
434 if (merge_bases_many(r, one, n, twos, result) < 0)
435 return -1;
@@ -452,8 +456,8 @@ static int get_merge_bases_many_0(struct repository *r,
456 clear_commit_marks(one, all_flags);
457 clear_commit_marks_many(n, twos, all_flags);
458
455 - cnt = remove_redundant(r, rslt, cnt);
456 - if (cnt < 0) {
459 + ret = remove_redundant(r, rslt, cnt, &cnt);
460 + if (ret < 0) {
461 free(rslt);
462 return -1;
463 }
@@ -582,7 +586,8 @@ struct commit_list *reduce_heads(struct commit_list *heads)
586 struct commit_list *p;
587 struct commit_list *result = NULL, **tail = &result;
588 struct commit **array;
585 - int num_head, i;
589 + size_t num_head, i;
590 + int ret;
591
592 if (!heads)
593 return NULL;
@@ -603,11 +608,13 @@ struct commit_list *reduce_heads(struct commit_list *heads)
608 p->item->object.flags &= ~STALE;
609 }
610 }
606 - num_head = remove_redundant(the_repository, array, num_head);
607 - if (num_head < 0) {
611 +
612 + ret = remove_redundant(the_repository, array, num_head, &num_head);
613 + if (ret < 0) {
614 free(array);
615 return NULL;
616 }
617 +
618 for (i = 0; i < num_head; i++)
619 tail = &commit_list_insert(array[i], tail)->next;
620 free(array);