commit-graph: extract count_distinct_commits()

The write_commit_graph() method is too complex, so we are extracting helper functions one by one. Extract count_distinct_commits(), which sorts the oids list, then iterates through to find duplicates. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 12, 2019 at 06:29 UTC 014e3440f524a620fd8aba92f1efeac737d9c23d
1 file changed +22 -13
commit-graph.c
+22 -13
@@ -963,6 +963,27 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
963 stop_progress(&ctx->progress);
964 }
965
966 +static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
967 +{
968 + uint32_t i, count_distinct = 1;
969 +
970 + if (ctx->report_progress)
971 + ctx->progress = start_delayed_progress(
972 + _("Counting distinct commits in commit graph"),
973 + ctx->oids.nr);
974 + display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
975 + QSORT(ctx->oids.list, ctx->oids.nr, commit_compare);
976 +
977 + for (i = 1; i < ctx->oids.nr; i++) {
978 + display_progress(ctx->progress, i + 1);
979 + if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
980 + count_distinct++;
981 + }
982 + stop_progress(&ctx->progress);
983 +
984 + return count_distinct;
985 +}
986 +
987 int write_commit_graph(const char *obj_dir,
988 struct string_list *pack_indexes,
989 struct string_list *commit_hex,
@@ -1024,19 +1045,7 @@ int write_commit_graph(const char *obj_dir,
1045
1046 close_reachable(ctx);
1047
1027 - if (ctx->report_progress)
1028 - ctx->progress = start_delayed_progress(
1029 - _("Counting distinct commits in commit graph"),
1030 - ctx->oids.nr);
1031 - display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1032 - QSORT(ctx->oids.list, ctx->oids.nr, commit_compare);
1033 - count_distinct = 1;
1034 - for (i = 1; i < ctx->oids.nr; i++) {
1035 - display_progress(ctx->progress, i + 1);
1036 - if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1037 - count_distinct++;
1038 - }
1039 - stop_progress(&ctx->progress);
1048 + count_distinct = count_distinct_commits(ctx);
1049
1050 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1051 error(_("the commit graph format cannot write %d commits"), count_distinct);