git-am: implement --reject option passed to git-apply
[gitweb.git] / builtin-fsck.c
index 6eb7da88d3e8591a8c544acc61a42e00babff120..aecc8280a0aedb37437ccce446f3382ccba08829 100644 (file)
@@ -10,6 +10,7 @@
 #include "tree-walk.h"
 #include "fsck.h"
 #include "parse-options.h"
+#include "dir.h"
 
 #define REACHABLE 0x0001
 #define SEEN      0x0002
@@ -64,11 +65,11 @@ static int fsck_error_func(struct object *obj, int type, const char *err, ...)
        return (type == FSCK_WARN) ? 0 : 1;
 }
 
+static struct object_array pending;
+
 static int mark_object(struct object *obj, int type, void *data)
 {
-       struct tree *tree = NULL;
        struct object *parent = data;
-       int result;
 
        if (!obj) {
                printf("broken link from %7s %s\n",
@@ -96,6 +97,20 @@ static int mark_object(struct object *obj, int type, void *data)
                return 1;
        }
 
+       add_object_array(obj, (void *) parent, &pending);
+       return 0;
+}
+
+static void mark_object_reachable(struct object *obj)
+{
+       mark_object(obj, OBJ_ANY, 0);
+}
+
+static int traverse_one_object(struct object *obj, struct object *parent)
+{
+       int result;
+       struct tree *tree = NULL;
+
        if (obj->type == OBJ_TREE) {
                obj->parsed = 0;
                tree = (struct tree *)obj;
@@ -107,15 +122,22 @@ static int mark_object(struct object *obj, int type, void *data)
                free(tree->buffer);
                tree->buffer = NULL;
        }
-       if (result < 0)
-               result = 1;
-
        return result;
 }
 
-static void mark_object_reachable(struct object *obj)
+static int traverse_reachable(void)
 {
-       mark_object(obj, OBJ_ANY, 0);
+       int result = 0;
+       while (pending.nr) {
+               struct object_array_entry *entry;
+               struct object *obj, *parent;
+
+               entry = pending.objects + --pending.nr;
+               obj = entry->item;
+               parent = (struct object *) entry->name;
+               result |= traverse_one_object(obj, parent);
+       }
+       return !!result;
 }
 
 static int mark_used(struct object *obj, int type, void *data)
@@ -201,12 +223,16 @@ static void check_unreachable_object(struct object *obj)
                                char *buf = read_sha1_file(obj->sha1,
                                                &type, &size);
                                if (buf) {
-                                       fwrite(buf, size, 1, f);
+                                       if (fwrite(buf, size, 1, f) != 1)
+                                               die("Could not write %s: %s",
+                                                   filename, strerror(errno));
                                        free(buf);
                                }
                        } else
                                fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
-                       fclose(f);
+                       if (fclose(f))
+                               die("Could not finish %s: %s",
+                                   filename, strerror(errno));
                }
                return;
        }
@@ -233,6 +259,9 @@ static void check_connectivity(void)
 {
        int i, max;
 
+       /* Traverse the pending reachable objects */
+       traverse_reachable();
+
        /* Look up all the requirements, warn about missing objects.. */
        max = get_max_object_index();
        if (verbose)
@@ -367,25 +396,18 @@ static void fsck_dir(int i, char *path)
        while ((de = readdir(dir)) != NULL) {
                char name[100];
                unsigned char sha1[20];
-               int len = strlen(de->d_name);
 
-               switch (len) {
-               case 2:
-                       if (de->d_name[1] != '.')
-                               break;
-               case 1:
-                       if (de->d_name[0] != '.')
-                               break;
+               if (is_dot_or_dotdot(de->d_name))
                        continue;
-               case 38:
+               if (strlen(de->d_name) == 38) {
                        sprintf(name, "%02x", i);
-                       memcpy(name+2, de->d_name, len+1);
+                       memcpy(name+2, de->d_name, 39);
                        if (get_sha1_hex(name, sha1) < 0)
                                break;
                        add_sha1_list(sha1, DIRENT_SORT_HINT(de));
                        continue;
                }
-               if (prefixcmp(de->d_name, "tmp_obj_"))
+               if (!prefixcmp(de->d_name, "tmp_obj_"))
                        continue;
                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
        }
@@ -600,7 +622,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
        }
 
        heads = 0;
-       for (i = 1; i < argc; i++) {
+       for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
                if (!get_sha1(arg, head_sha1)) {
                        struct object *obj = lookup_object(head_sha1);