{"thread":{"id":"37145","subject":"[PATCH] use commit_list_count() to count the members of commit_lists","startedAt":"2014-07-16T23:52:09Z","lastAt":"2014-07-16T23:52:09Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"246211","messageId":"53C71029.8060709@web.de","threadId":"37145","inReplyTo":null,"subject":"[PATCH] use commit_list_count() to count the members of commit_lists","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-07-16T23:52:09Z","receivedAt":"2014-07-16T23:52:09Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Call commit_list_count() instead of open-coding it repeatedly.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n\n---\n builtin/blame.c        |  5 +----\n builtin/for-each-ref.c | 16 ++--------------\n commit.c               |  7 +------\n line-log.c             | 13 +------------\n pretty.c               |  7 +------\n 5 files changed, 6 insertions(+), 42 deletions(-)\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex c59e702..339a8d0 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -1371,11 +1371,8 @@ static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit\n \n static int num_scapegoats(struct rev_info *revs, struct commit *commit)\n {\n-\tint cnt;\n \tstruct commit_list *l = first_scapegoat(revs, commit);\n-\tfor (cnt = 0; l; l = l->next)\n-\t\tcnt++;\n-\treturn cnt;\n+\treturn commit_list_count(l);\n }\n \n /* Distribute collected unsorted blames to the respected sorted lists\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 4135980..47bd624 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -283,18 +283,6 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob\n \t}\n }\n \n-static int num_parents(struct commit *commit)\n-{\n-\tstruct commit_list *parents;\n-\tint i;\n-\n-\tfor (i = 0, parents = commit->parents;\n-\t     parents;\n-\t     parents = parents->next)\n-\t\ti++;\n-\treturn i;\n-}\n-\n /* See grab_values */\n static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)\n {\n@@ -315,12 +303,12 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object\n \t\t}\n \t\tif (!strcmp(name, \"numparent\")) {\n \t\t\tchar *s = xmalloc(40);\n-\t\t\tv->ul = num_parents(commit);\n+\t\t\tv->ul = commit_list_count(commit->parents);\n \t\t\tsprintf(s, \"%lu\", v->ul);\n \t\t\tv->s = s;\n \t\t}\n \t\telse if (!strcmp(name, \"parent\")) {\n-\t\t\tint num = num_parents(commit);\n+\t\t\tint num = commit_list_count(commit->parents);\n \t\t\tint i;\n \t\t\tstruct commit_list *parents;\n \t\t\tchar *s = xmalloc(41 * num + 1);\ndiff --git a/commit.c b/commit.c\nindex f43970d..e9c40f7 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -987,12 +987,7 @@ struct commit_list *get_merge_bases_many(struct commit *one,\n \t}\n \n \t/* There are more than one */\n-\tcnt = 0;\n-\tlist = result;\n-\twhile (list) {\n-\t\tlist = list->next;\n-\t\tcnt++;\n-\t}\n+\tcnt = commit_list_count(result);\n \trslt = xcalloc(cnt, sizeof(*rslt));\n \tfor (list = result, i = 0; list; list = list->next)\n \t\trslt[i++] = list->item;\ndiff --git a/line-log.c b/line-log.c\nindex afcc98d..1008e72 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -766,17 +766,6 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list\n \t}\n }\n \n-static int count_parents(struct commit *commit)\n-{\n-\tstruct commit_list *parents = commit->parents;\n-\tint count = 0;\n-\twhile (parents) {\n-\t\tcount++;\n-\t\tparents = parents->next;\n-\t}\n-\treturn count;\n-}\n-\n static void move_diff_queue(struct diff_queue_struct *dst,\n \t\t\t    struct diff_queue_struct *src)\n {\n@@ -1150,7 +1139,7 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm\n \tstruct commit **parents;\n \tstruct commit_list *p;\n \tint i;\n-\tint nparents = count_parents(commit);\n+\tint nparents = commit_list_count(commit->parents);\n \n \tdiffqueues = xmalloc(nparents * sizeof(*diffqueues));\n \tcand = xmalloc(nparents * sizeof(*cand));\ndiff --git a/pretty.c b/pretty.c\nindex eb676d6..3a1da6f 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1554,12 +1554,7 @@ static void pp_header(struct pretty_print_context *pp,\n \t\t}\n \n \t\tif (!parents_shown) {\n-\t\t\tstruct commit_list *parent;\n-\t\t\tint num;\n-\t\t\tfor (parent = commit->parents, num = 0;\n-\t\t\t     parent;\n-\t\t\t     parent = parent->next, num++)\n-\t\t\t\t;\n+\t\t\tunsigned num = commit_list_count(commit->parents);\n \t\t\t/* with enough slop */\n \t\t\tstrbuf_grow(sb, num * 50 + 20);\n \t\t\tadd_merge_info(pp, sb, commit);\n-- \n2.0.2\n"}]}