{"thread":{"id":"37095","subject":"[PATCH] fsck: simplify fsck_commit_buffer() by using commit_list_count()","startedAt":"2014-07-10T09:48:26Z","lastAt":"2014-07-10T09:48:26Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"245688","messageId":"53BE616A.4000709@web.de","threadId":"37095","inReplyTo":null,"subject":"[PATCH] fsck: simplify fsck_commit_buffer() by using commit_list_count()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-07-10T09:48:26Z","receivedAt":"2014-07-10T09:48:26Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"fsck_commit_buffer() checks that the number of items in the parents\nlist of a commit matches the number of parent lines in its buffer or --\nif a graft is used -- the number of parents in that graft.  Simplify\nthe code by using commit_list_count() instead of counting by hand.\nAlso use different variables for the number of lines and the number of\nlist items, making it easier to compare them.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n fsck.c | 22 ++++++----------------\n 1 file changed, 6 insertions(+), 16 deletions(-)\n\ndiff --git a/fsck.c b/fsck.c\nindex a4e8593..56156ff 100644\n--- a/fsck.c\n+++ b/fsck.c\n@@ -281,7 +281,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,\n {\n \tunsigned char tree_sha1[20], sha1[20];\n \tstruct commit_graft *graft;\n-\tint parents = 0;\n+\tunsigned parent_count, parent_line_count = 0;\n \tint err;\n \n \tif (!skip_prefix(buffer, \"tree \", &buffer))\n@@ -293,27 +293,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,\n \t\tif (get_sha1_hex(buffer, sha1) || buffer[40] != '\\n')\n \t\t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid 'parent' line format - bad sha1\");\n \t\tbuffer += 41;\n-\t\tparents++;\n+\t\tparent_line_count++;\n \t}\n \tgraft = lookup_commit_graft(commit->object.sha1);\n+\tparent_count = commit_list_count(commit->parents);\n \tif (graft) {\n-\t\tstruct commit_list *p = commit->parents;\n-\t\tparents = 0;\n-\t\twhile (p) {\n-\t\t\tp = p->next;\n-\t\t\tparents++;\n-\t\t}\n-\t\tif (graft->nr_parent == -1 && !parents)\n+\t\tif (graft->nr_parent == -1 && !parent_count)\n \t\t\t; /* shallow commit */\n-\t\telse if (graft->nr_parent != parents)\n+\t\telse if (graft->nr_parent != parent_count)\n \t\t\treturn error_func(&commit->object, FSCK_ERROR, \"graft objects missing\");\n \t} else {\n-\t\tstruct commit_list *p = commit->parents;\n-\t\twhile (p && parents) {\n-\t\t\tp = p->next;\n-\t\t\tparents--;\n-\t\t}\n-\t\tif (p || parents)\n+\t\tif (parent_count != parent_line_count)\n \t\t\treturn error_func(&commit->object, FSCK_ERROR, \"parent objects missing\");\n \t}\n \tif (!skip_prefix(buffer, \"author \", &buffer))\n-- \n2.0.0\n"}]}