fsck: simplify fsck_commit_buffer() by using commit_list_count()
fsck_commit_buffer() checks that the number of items in the parents list of a commit matches the number of parent lines in its buffer or -- if a graft is used -- the number of parents in that graft. Simplify the code by using commit_list_count() instead of counting by hand. Also use different variables for the number of lines and the number of list items, making it easier to compare them. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 10, 2014 at 11:48 UTC
9d02150cf4d833935161ef265e4dc03807caa800
1 file changed
+6
-16
fsck.c
+6
-16
@@ -281,7 +281,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
281
{
282
unsigned char tree_sha1[20], sha1[20];
283
struct commit_graft *graft;
284
- int parents = 0;
284
+ unsigned parent_count, parent_line_count = 0;
285
int err;
286
287
if (!skip_prefix(buffer, "tree ", &buffer))
@@ -293,27 +293,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
293
if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
294
return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
295
buffer += 41;
296
- parents++;
296
+ parent_line_count++;
297
}
298
graft = lookup_commit_graft(commit->object.sha1);
299
+ parent_count = commit_list_count(commit->parents);
300
if (graft) {
300
- struct commit_list *p = commit->parents;
301
- parents = 0;
302
- while (p) {
303
- p = p->next;
304
- parents++;
305
- }
306
- if (graft->nr_parent == -1 && !parents)
301
+ if (graft->nr_parent == -1 && !parent_count)
302
; /* shallow commit */
308
- else if (graft->nr_parent != parents)
303
+ else if (graft->nr_parent != parent_count)
304
return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
305
} else {
311
- struct commit_list *p = commit->parents;
312
- while (p && parents) {
313
- p = p->next;
314
- parents--;
315
- }
316
- if (p || parents)
306
+ if (parent_count != parent_line_count)
307
return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
308
}
309
if (!skip_prefix(buffer, "author ", &buffer))