commit-graph: use commit_list_count()

Let commit_list_count() count the number of parents instead of duplicating it. Also store the result in an unsigned int, as that's what the function returns, and the count is never negative. Signed-off-by: René Scharfe <l.s.r@web.de> Acked-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 15, 2019 at 19:07 UTC 689a146c914d861a5bc8b88643802a1ede013dce
1 file changed +6 -11
commit-graph.c
+6 -11
@@ -1276,7 +1276,6 @@ static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1276 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1277 {
1278 uint32_t i;
1279 - struct commit_list *parent;
1279
1280 ctx->num_extra_edges = 0;
1281 if (ctx->report_progress)
@@ -1284,7 +1283,8 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1283 _("Finding extra edges in commit graph"),
1284 ctx->oids.nr);
1285 for (i = 0; i < ctx->oids.nr; i++) {
1287 - int num_parents = 0;
1286 + unsigned int num_parents;
1287 +
1288 display_progress(ctx->progress, i + 1);
1289 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1290 continue;
@@ -1298,10 +1298,7 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1298
1299 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1300
1301 - for (parent = ctx->commits.list[ctx->commits.nr]->parents;
1302 - parent; parent = parent->next)
1303 - num_parents++;
1304 -
1301 + num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1302 if (num_parents > 2)
1303 ctx->num_extra_edges += num_parents - 1;
1304
@@ -1613,8 +1610,7 @@ static int commit_compare(const void *_a, const void *_b)
1610
1611 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1612 {
1616 - uint32_t i, num_parents;
1617 - struct commit_list *parent;
1613 + uint32_t i;
1614
1615 if (ctx->report_progress)
1616 ctx->progress = start_delayed_progress(
@@ -1632,10 +1628,9 @@ static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1628 die(_("unexpected duplicate commit id %s"),
1629 oid_to_hex(&ctx->commits.list[i]->object.oid));
1630 } else {
1635 - num_parents = 0;
1636 - for (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)
1637 - num_parents++;
1631 + unsigned int num_parents;
1632
1633 + num_parents = commit_list_count(ctx->commits.list[i]->parents);
1634 if (num_parents > 2)
1635 ctx->num_extra_edges += num_parents - 1;
1636 }