builtin-fsck: reports missing parent commits
[git/spearce.git] / fsck.c
blob98fb41af685b0a82339e23c30c8318e4d205d08e
1 #include "cache.h"
2 #include "object.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "fsck.h"
10 static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
12 struct tree_desc desc;
13 struct name_entry entry;
14 int res = 0;
16 if (parse_tree(tree))
17 return -1;
19 init_tree_desc(&desc, tree->buffer, tree->size);
20 while (tree_entry(&desc, &entry)) {
21 int result;
23 if (S_ISGITLINK(entry.mode))
24 continue;
25 if (S_ISDIR(entry.mode))
26 result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
27 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
28 result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
29 else {
30 result = error("in tree %s: entry %s has bad mode %.6o\n",
31 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
33 if (result < 0)
34 return result;
35 if (!res)
36 res = result;
38 return res;
41 static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
43 struct commit_list *parents;
44 int res;
45 int result;
47 if (parse_commit(commit))
48 return -1;
50 result = walk((struct object *)commit->tree, OBJ_TREE, data);
51 if (result < 0)
52 return result;
53 res = result;
55 parents = commit->parents;
56 while (parents) {
57 result = walk((struct object *)parents->item, OBJ_COMMIT, data);
58 if (result < 0)
59 return result;
60 if (!res)
61 res = result;
62 parents = parents->next;
64 return res;
67 static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
69 if (parse_tag(tag))
70 return -1;
71 return walk(tag->tagged, OBJ_ANY, data);
74 int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
76 if (!obj)
77 return -1;
78 switch (obj->type) {
79 case OBJ_BLOB:
80 return 0;
81 case OBJ_TREE:
82 return fsck_walk_tree((struct tree *)obj, walk, data);
83 case OBJ_COMMIT:
84 return fsck_walk_commit((struct commit *)obj, walk, data);
85 case OBJ_TAG:
86 return fsck_walk_tag((struct tag *)obj, walk, data);
87 default:
88 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
89 return -1;