Code

add generic, type aware object chain walker
authorMartin Koegler <mkoegler@auto.tuwien.ac.at>
Mon, 25 Feb 2008 21:46:04 +0000 (22:46 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Feb 2008 07:57:34 +0000 (23:57 -0800)
The requirements are:
* it may not crash on NULL pointers
* a callback function is needed, as index-pack/unpack-objects
  need to do different things
* the type information is needed to check the expected <-> real type
  and print better error messages

Signed-off-by: Martin Koegler <mkoegler@auto.tuwien.ac.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
cache.h
fsck.c [new file with mode: 0644]
fsck.h [new file with mode: 0644]

index 92341c4bbe3f65671271211c9cbac3d4ffdc3eef..33c367dfb1c92723b7e1d099398ab11e90b646ed 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -297,7 +297,7 @@ LIB_H = \
        run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
        tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
        utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h \
-       mailmap.h remote.h parse-options.h transport.h diffcore.h hash.h
+       mailmap.h remote.h parse-options.h transport.h diffcore.h hash.h fsck.h
 
 DIFF_OBJS = \
        diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -320,7 +320,7 @@ LIB_OBJS = \
        alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
        color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
        convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-       transport.o bundle.o walker.o parse-options.o ws.o archive.o
+       transport.o bundle.o walker.o parse-options.o ws.o archive.o fsck.o
 
 BUILTIN_OBJS = \
        builtin-add.o \
diff --git a/cache.h b/cache.h
index 43ba6a3ba568fb0b9e93c8aeb78782e12bfda416..d3b898708b0c30e2de979e51e31c0a95918ea348 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -189,6 +189,7 @@ enum object_type {
        /* 5 for future expansion */
        OBJ_OFS_DELTA = 6,
        OBJ_REF_DELTA = 7,
+       OBJ_ANY,
        OBJ_MAX,
 };
 
diff --git a/fsck.c b/fsck.c
new file mode 100644 (file)
index 0000000..98fb41a
--- /dev/null
+++ b/fsck.c
@@ -0,0 +1,91 @@
+#include "cache.h"
+#include "object.h"
+#include "blob.h"
+#include "tree.h"
+#include "tree-walk.h"
+#include "commit.h"
+#include "tag.h"
+#include "fsck.h"
+
+static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
+{
+       struct tree_desc desc;
+       struct name_entry entry;
+       int res = 0;
+
+       if (parse_tree(tree))
+               return -1;
+
+       init_tree_desc(&desc, tree->buffer, tree->size);
+       while (tree_entry(&desc, &entry)) {
+               int result;
+
+               if (S_ISGITLINK(entry.mode))
+                       continue;
+               if (S_ISDIR(entry.mode))
+                       result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
+               else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
+                       result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
+               else {
+                       result = error("in tree %s: entry %s has bad mode %.6o\n",
+                                       sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
+               }
+               if (result < 0)
+                       return result;
+               if (!res)
+                       res = result;
+       }
+       return res;
+}
+
+static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
+{
+       struct commit_list *parents;
+       int res;
+       int result;
+
+       if (parse_commit(commit))
+               return -1;
+
+       result = walk((struct object *)commit->tree, OBJ_TREE, data);
+       if (result < 0)
+               return result;
+       res = result;
+
+       parents = commit->parents;
+       while (parents) {
+               result = walk((struct object *)parents->item, OBJ_COMMIT, data);
+               if (result < 0)
+                       return result;
+               if (!res)
+                       res = result;
+               parents = parents->next;
+       }
+       return res;
+}
+
+static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
+{
+       if (parse_tag(tag))
+               return -1;
+       return walk(tag->tagged, OBJ_ANY, data);
+}
+
+int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
+{
+       if (!obj)
+               return -1;
+       switch (obj->type) {
+       case OBJ_BLOB:
+               return 0;
+       case OBJ_TREE:
+               return fsck_walk_tree((struct tree *)obj, walk, data);
+       case OBJ_COMMIT:
+               return fsck_walk_commit((struct commit *)obj, walk, data);
+       case OBJ_TAG:
+               return fsck_walk_tag((struct tag *)obj, walk, data);
+       default:
+               error("Unknown object type for %s", sha1_to_hex(obj->sha1));
+               return -1;
+       }
+}
diff --git a/fsck.h b/fsck.h
new file mode 100644 (file)
index 0000000..ba5514a
--- /dev/null
+++ b/fsck.h
@@ -0,0 +1,23 @@
+#ifndef GIT_FSCK_H
+#define GIT_FSCK_H
+
+/*
+ * callback function for fsck_walk
+ * type is the expected type of the object or OBJ_ANY
+ * the return value is:
+ *     0       everything OK
+ *     <0      error signaled and abort
+ *     >0      error signaled and do not abort
+ */
+typedef int (*fsck_walk_func)(struct object *obj, int type, void *data);
+
+/* descend in all linked child objects
+ * the return value is:
+ *    -1       error in processing the object
+ *    <0       return value of the callback, which lead to an abort
+ *    >0       return value of the first sigaled error >0 (in the case of no other errors)
+ *    0                everything OK
+ */
+int fsck_walk(struct object *obj, fsck_walk_func walk, void *data);
+
+#endif