shallow: fix -Wsign-compare warnings

Fix a couple of -Wsign-compare issues in "shallow.c" and mark the file as -Wsign-compare-clean. This change prepares the code for a refactoring of `repo_in_merge_bases_many()`, which will be adapted to accept the number of commits as `size_t` instead of `int`. 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 455ac07021d4feede4f5b7e39bf00dc186ce3c09
2 files changed +21 -23
shallow.c
+18 -20
@@ -1,5 +1,4 @@
1 #define USE_THE_REPOSITORY_VARIABLE
2 -#define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "hex.h"
@@ -134,7 +133,8 @@ static void free_depth_in_slab(int **ptr)
133 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
134 int shallow_flag, int not_shallow_flag)
135 {
137 - int i = 0, cur_depth = 0;
136 + size_t i = 0;
137 + int cur_depth = 0;
138 struct commit_list *result = NULL;
139 struct object_array stack = OBJECT_ARRAY_INIT;
140 struct commit *commit = NULL;
@@ -335,16 +335,16 @@ static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
335 const struct oid_array *extra,
336 unsigned flags)
337 {
338 - struct write_shallow_data data;
339 - int i;
340 - data.out = out;
341 - data.use_pack_protocol = use_pack_protocol;
342 - data.count = 0;
343 - data.flags = flags;
338 + struct write_shallow_data data = {
339 + .out = out,
340 + .use_pack_protocol = use_pack_protocol,
341 + .flags = flags,
342 + };
343 +
344 for_each_commit_graft(write_one_shallow, &data);
345 if (!extra)
346 return data.count;
347 - for (i = 0; i < extra->nr; i++) {
347 + for (size_t i = 0; i < extra->nr; i++) {
348 strbuf_addstr(out, oid_to_hex(extra->oid + i));
349 strbuf_addch(out, '\n');
350 data.count++;
@@ -466,7 +466,6 @@ struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
466 */
467 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
468 {
469 - int i;
469 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
470 memset(info, 0, sizeof(*info));
471 info->shallow = sa;
@@ -474,7 +473,7 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
473 return;
474 ALLOC_ARRAY(info->ours, sa->nr);
475 ALLOC_ARRAY(info->theirs, sa->nr);
477 - for (i = 0; i < sa->nr; i++) {
476 + for (size_t i = 0; i < sa->nr; i++) {
477 if (repo_has_object_file(the_repository, sa->oid + i)) {
478 struct commit_graft *graft;
479 graft = lookup_commit_graft(the_repository,
@@ -507,7 +506,7 @@ void clear_shallow_info(struct shallow_info *info)
506 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
507 {
508 struct object_id *oid = info->shallow->oid;
510 - int i, dst;
509 + size_t i, dst;
510 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
511 for (i = dst = 0; i < info->nr_theirs; i++) {
512 if (i != dst)
@@ -560,7 +559,7 @@ static void paint_down(struct paint_info *info, const struct object_id *oid,
559 {
560 unsigned int i, nr;
561 struct commit_list *head = NULL;
563 - int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
562 + size_t bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
563 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
564 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
565 1);
@@ -660,7 +659,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
659 struct object_id *oid = info->shallow->oid;
660 struct oid_array *ref = info->ref;
661 unsigned int i, nr;
663 - int *shallow, nr_shallow = 0;
662 + size_t *shallow, nr_shallow = 0;
663 struct paint_info pi;
664
665 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
@@ -735,7 +734,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
734
735 struct commit_array {
736 struct commit **commits;
738 - int nr, alloc;
737 + size_t nr, alloc;
738 };
739
740 static int add_ref(const char *refname UNUSED,
@@ -753,12 +752,11 @@ static int add_ref(const char *refname UNUSED,
752 return 0;
753 }
754
756 -static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
755 +static void update_refstatus(int *ref_status, size_t nr, uint32_t *bitmap)
756 {
758 - unsigned int i;
757 if (!ref_status)
758 return;
761 - for (i = 0; i < nr; i++)
759 + for (size_t i = 0; i < nr; i++)
760 if (bitmap[i / 32] & (1U << (i % 32)))
761 ref_status[i]++;
762 }
@@ -773,8 +771,8 @@ static void post_assign_shallow(struct shallow_info *info,
771 struct object_id *oid = info->shallow->oid;
772 struct commit *c;
773 uint32_t **bitmap;
776 - int dst, i, j;
777 - int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
774 + size_t dst, i, j;
775 + size_t bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
776 struct commit_array ca;
777
778 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
shallow.h
+3 -3
@@ -59,8 +59,8 @@ void prune_shallow(unsigned options);
59 */
60 struct shallow_info {
61 struct oid_array *shallow;
62 - int *ours, nr_ours;
63 - int *theirs, nr_theirs;
62 + size_t *ours, nr_ours;
63 + size_t *theirs, nr_theirs;
64 struct oid_array *ref;
65
66 /* for receive-pack */
@@ -69,7 +69,7 @@ struct shallow_info {
69 int *reachable;
70 int *shallow_ref;
71 struct commit **commits;
72 - int nr_commits;
72 + size_t nr_commits;
73 };
74
75 void prepare_shallow_info(struct shallow_info *, struct oid_array *);