{"thread":{"id":"51854","subject":"[PATCH] commit-graph: use commit_list_count()","startedAt":"2019-09-15T17:07:55Z","lastAt":"2019-09-16T02:05:08Z","messageCount":2,"participants":["René Scharfe","Derrick Stolee"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"382397","messageId":"1d28fb89-6ed6-b70f-762c-b65cffeb6705@web.de","threadId":"51854","inReplyTo":null,"subject":"[PATCH] commit-graph: use commit_list_count()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2019-09-15T17:07:44Z","receivedAt":"2019-09-15T17:07:55Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Let commit_list_count() count the number of parents instead of\nduplicating it.  Also store the result in an unsigned int, as that's\nwhat the function returns, and the count is never negative.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n commit-graph.c | 17 ++++++-----------\n 1 file changed, 6 insertions(+), 11 deletions(-)\n\ndiff --git a/commit-graph.c b/commit-graph.c\nindex 9b02d2c426..5b0d6b5adc 100644\n--- a/commit-graph.c\n+++ b/commit-graph.c\n@@ -1279,7 +1279,6 @@ static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)\n static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n {\n \tuint32_t i;\n-\tstruct commit_list *parent;\n\n \tctx->num_extra_edges = 0;\n \tif (ctx->report_progress)\n@@ -1287,7 +1286,8 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n \t\t\t_(\"Finding extra edges in commit graph\"),\n \t\t\tctx->oids.nr);\n \tfor (i = 0; i < ctx->oids.nr; i++) {\n-\t\tint num_parents = 0;\n+\t\tunsigned int num_parents;\n+\n \t\tdisplay_progress(ctx->progress, i + 1);\n \t\tif (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))\n \t\t\tcontinue;\n@@ -1301,10 +1301,7 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n\n \t\tparse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);\n\n-\t\tfor (parent = ctx->commits.list[ctx->commits.nr]->parents;\n-\t\t     parent; parent = parent->next)\n-\t\t\tnum_parents++;\n-\n+\t\tnum_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);\n \t\tif (num_parents > 2)\n \t\t\tctx->num_extra_edges += num_parents - 1;\n\n@@ -1616,8 +1613,7 @@ static int commit_compare(const void *_a, const void *_b)\n\n static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)\n {\n-\tuint32_t i, num_parents;\n-\tstruct commit_list *parent;\n+\tuint32_t i;\n\n \tif (ctx->report_progress)\n \t\tctx->progress = start_delayed_progress(\n@@ -1635,10 +1631,9 @@ static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)\n \t\t\tdie(_(\"unexpected duplicate commit id %s\"),\n \t\t\t    oid_to_hex(&ctx->commits.list[i]->object.oid));\n \t\t} else {\n-\t\t\tnum_parents = 0;\n-\t\t\tfor (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)\n-\t\t\t\tnum_parents++;\n+\t\t\tunsigned int num_parents;\n\n+\t\t\tnum_parents = commit_list_count(ctx->commits.list[i]->parents);\n \t\t\tif (num_parents > 2)\n \t\t\t\tctx->num_extra_edges += num_parents - 1;\n \t\t}\n--\n2.23.0\n"},{"id":"382409","messageId":"eb0aeb20-4fa0-7898-c3c8-139526667f5d@gmail.com","threadId":"51854","inReplyTo":"1d28fb89-6ed6-b70f-762c-b65cffeb6705@web.de","subject":"Re: [PATCH] commit-graph: use commit_list_count()","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2019-09-16T02:05:03Z","receivedAt":"2019-09-16T02:05:08Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/15/2019 1:07 PM, René Scharfe wrote:\n> Let commit_list_count() count the number of parents instead of\n> duplicating it.  Also store the result in an unsigned int, as that's\n> what the function returns, and the count is never negative.\n\nI was unfamiliar with this method, but it obviously removes some\nredundant code. We would have many more problems before the signed-ness\nof the int was important, but good to use the type matching the\nmethod.\n\nThanks,\n-Stolee\n\n> \n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  commit-graph.c | 17 ++++++-----------\n>  1 file changed, 6 insertions(+), 11 deletions(-)\n> \n> diff --git a/commit-graph.c b/commit-graph.c\n> index 9b02d2c426..5b0d6b5adc 100644\n> --- a/commit-graph.c\n> +++ b/commit-graph.c\n> @@ -1279,7 +1279,6 @@ static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)\n>  static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n>  {\n>  \tuint32_t i;\n> -\tstruct commit_list *parent;\n> \n>  \tctx->num_extra_edges = 0;\n>  \tif (ctx->report_progress)\n> @@ -1287,7 +1286,8 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n>  \t\t\t_(\"Finding extra edges in commit graph\"),\n>  \t\t\tctx->oids.nr);\n>  \tfor (i = 0; i < ctx->oids.nr; i++) {\n> -\t\tint num_parents = 0;\n> +\t\tunsigned int num_parents;\n> +\n>  \t\tdisplay_progress(ctx->progress, i + 1);\n>  \t\tif (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))\n>  \t\t\tcontinue;\n> @@ -1301,10 +1301,7 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)\n> \n>  \t\tparse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);\n> \n> -\t\tfor (parent = ctx->commits.list[ctx->commits.nr]->parents;\n> -\t\t     parent; parent = parent->next)\n> -\t\t\tnum_parents++;\n> -\n> +\t\tnum_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);\n>  \t\tif (num_parents > 2)\n>  \t\t\tctx->num_extra_edges += num_parents - 1;\n> \n> @@ -1616,8 +1613,7 @@ static int commit_compare(const void *_a, const void *_b)\n> \n>  static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)\n>  {\n> -\tuint32_t i, num_parents;\n> -\tstruct commit_list *parent;\n> +\tuint32_t i;\n> \n>  \tif (ctx->report_progress)\n>  \t\tctx->progress = start_delayed_progress(\n> @@ -1635,10 +1631,9 @@ static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)\n>  \t\t\tdie(_(\"unexpected duplicate commit id %s\"),\n>  \t\t\t    oid_to_hex(&ctx->commits.list[i]->object.oid));\n>  \t\t} else {\n> -\t\t\tnum_parents = 0;\n> -\t\t\tfor (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)\n> -\t\t\t\tnum_parents++;\n> +\t\t\tunsigned int num_parents;\n> \n> +\t\t\tnum_parents = commit_list_count(ctx->commits.list[i]->parents);\n>  \t\t\tif (num_parents > 2)\n>  \t\t\t\tctx->num_extra_edges += num_parents - 1;\n>  \t\t}\n> --\n> 2.23.0\n> \n\n"}]}