Skip to content

Commit 4516338

Browse files
Martin Koeglergitster
authored andcommitted
builtin-fsck: reports missing parent commits
parse_commit ignores parent commits with certain errors (eg. a non commit object is already loaded under the sha1 of the parent). To make fsck reports such errors, it has to compare the nummer of parent commits returned by parse commit with the number of parent commits in the object or in the graft/shallow file. Signed-off-by: Martin Koegler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7914053 commit 4516338

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

builtin-fsck.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ static int fsck_commit(struct commit *commit)
397397
{
398398
char *buffer = commit->buffer;
399399
unsigned char tree_sha1[20], sha1[20];
400+
struct commit_graft *graft;
401+
int parents = 0;
400402

401403
if (verbose)
402404
fprintf(stderr, "Checking commit %s\n",
@@ -411,6 +413,28 @@ static int fsck_commit(struct commit *commit)
411413
if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
412414
return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
413415
buffer += 48;
416+
parents++;
417+
}
418+
graft = lookup_commit_graft(commit->object.sha1);
419+
if (graft) {
420+
struct commit_list *p = commit->parents;
421+
parents = 0;
422+
while (p) {
423+
p = p->next;
424+
parents++;
425+
}
426+
if (graft->nr_parent == -1 && !parents)
427+
; /* shallow commit */
428+
else if (graft->nr_parent != parents)
429+
return objerror(&commit->object, "graft objects missing");
430+
} else {
431+
struct commit_list *p = commit->parents;
432+
while (p && parents) {
433+
p = p->next;
434+
parents--;
435+
}
436+
if (p || parents)
437+
return objerror(&commit->object, "parent objects missing");
414438
}
415439
if (memcmp(buffer, "author ", 7))
416440
return objerror(&commit->object, "invalid format - expected 'author' line");

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void prepare_commit_graft(void)
193193
commit_graft_prepared = 1;
194194
}
195195

196-
static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
196+
struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
197197
{
198198
int pos;
199199
prepare_commit_graft();

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct commit_graft {
101101
struct commit_graft *read_graft_line(char *buf, int len);
102102
int register_commit_graft(struct commit_graft *, int);
103103
int read_graft_file(const char *graft_file);
104+
struct commit_graft *lookup_commit_graft(const unsigned char *sha1);
104105

105106
extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2, int cleanup);
106107

0 commit comments

Comments
 (0)