Skip to content

Commit 9d02150

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb979db commit 9d02150

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

fsck.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
281281
{
282282
unsigned char tree_sha1[20], sha1[20];
283283
struct commit_graft *graft;
284-
int parents = 0;
284+
unsigned parent_count, parent_line_count = 0;
285285
int err;
286286

287287
if (!skip_prefix(buffer, "tree ", &buffer))
@@ -293,27 +293,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
293293
if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
294294
return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
295295
buffer += 41;
296-
parents++;
296+
parent_line_count++;
297297
}
298298
graft = lookup_commit_graft(commit->object.sha1);
299+
parent_count = commit_list_count(commit->parents);
299300
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)
307302
; /* shallow commit */
308-
else if (graft->nr_parent != parents)
303+
else if (graft->nr_parent != parent_count)
309304
return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
310305
} 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)
317307
return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
318308
}
319309
if (!skip_prefix(buffer, "author ", &buffer))

0 commit comments

Comments
 (0)