Remove unused object-ref code
[gitweb.git] / builtin-fsck.c
index 8d12287f037c499acad26ea81acab73490c38d5c..7321ab236b6925ef12f9e169b130eb53428d81a5 100644 (file)
@@ -8,6 +8,8 @@
 #include "pack.h"
 #include "cache-tree.h"
 #include "tree-walk.h"
+#include "fsck.h"
+#include "parse-options.h"
 
 #define REACHABLE 0x0001
 #define SEEN      0x0002
@@ -62,13 +64,73 @@ static int objwarning(struct object *obj, const char *err, ...)
        return -1;
 }
 
+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",
+                          typename(parent->type), sha1_to_hex(parent->sha1));
+               printf("broken link from %7s %s\n",
+                          (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
+               errors_found |= ERROR_REACHABLE;
+               return 1;
+       }
+
+       if (type != OBJ_ANY && obj->type != type)
+               objerror(parent, "wrong object type in link");
+
+       if (obj->flags & REACHABLE)
+               return 0;
+       obj->flags |= REACHABLE;
+       if (!obj->parsed) {
+               if (parent && !has_sha1_file(obj->sha1)) {
+                       printf("broken link from %7s %s\n",
+                                typename(parent->type), sha1_to_hex(parent->sha1));
+                       printf("              to %7s %s\n",
+                                typename(obj->type), sha1_to_hex(obj->sha1));
+                       errors_found |= ERROR_REACHABLE;
+               }
+               return 1;
+       }
+
+       if (obj->type == OBJ_TREE) {
+               obj->parsed = 0;
+               tree = (struct tree *)obj;
+               if (parse_tree(tree) < 0)
+                       return 1; /* error already displayed */
+       }
+       result = fsck_walk(obj, mark_object, obj);
+       if (tree) {
+               free(tree->buffer);
+               tree->buffer = NULL;
+       }
+       if (result < 0)
+               result = 1;
+
+       return result;
+}
+
+static void mark_object_reachable(struct object *obj)
+{
+       mark_object(obj, OBJ_ANY, 0);
+}
+
+static int mark_used(struct object *obj, int type, void *data)
+{
+       if (!obj)
+               return 1;
+       obj->used = 1;
+       return 0;
+}
+
 /*
  * Check a single reachable object
  */
 static void check_reachable_object(struct object *obj)
 {
-       const struct object_refs *refs;
-
        /*
         * We obviously want the object to be parsed,
         * except if it was in a pack-file and we didn't
@@ -81,25 +143,6 @@ static void check_reachable_object(struct object *obj)
                errors_found |= ERROR_REACHABLE;
                return;
        }
-
-       /*
-        * Check that everything that we try to reference is also good.
-        */
-       refs = lookup_object_refs(obj);
-       if (refs) {
-               unsigned j;
-               for (j = 0; j < refs->count; j++) {
-                       struct object *ref = refs->ref[j];
-                       if (ref->parsed ||
-                           (has_sha1_file(ref->sha1)))
-                               continue;
-                       printf("broken link from %7s %s\n",
-                              typename(obj->type), sha1_to_hex(obj->sha1));
-                       printf("              to %7s %s\n",
-                              typename(ref->type), sha1_to_hex(ref->sha1));
-                       errors_found |= ERROR_REACHABLE;
-               }
-       }
 }
 
 /*
@@ -413,6 +456,8 @@ static int fsck_sha1(const unsigned char *sha1)
        if (obj->flags & SEEN)
                return 0;
        obj->flags |= SEEN;
+       if (fsck_walk(obj, mark_used, 0))
+               objerror(obj, "broken links");
        if (obj->type == OBJ_BLOB)
                return 0;
        if (obj->type == OBJ_TREE)
@@ -537,13 +582,13 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
                obj = lookup_object(osha1);
                if (obj) {
                        obj->used = 1;
-                       mark_reachable(obj, REACHABLE);
+                       mark_object_reachable(obj);
                }
        }
        obj = lookup_object(nsha1);
        if (obj) {
                obj->used = 1;
-               mark_reachable(obj, REACHABLE);
+               mark_object_reachable(obj);
        }
        return 0;
 }
@@ -554,23 +599,26 @@ static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, in
        return 0;
 }
 
+static int is_branch(const char *refname)
+{
+       return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
+}
+
 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 {
        struct object *obj;
 
-       obj = lookup_object(sha1);
+       obj = parse_object(sha1);
        if (!obj) {
-               if (has_sha1_file(sha1)) {
-                       default_refs++;
-                       return 0; /* it is in a pack */
-               }
                error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
                /* We'll continue with the rest despite the error.. */
                return 0;
        }
+       if (obj->type != OBJ_COMMIT && is_branch(refname))
+               error("%s: not a commit", refname);
        default_refs++;
        obj->used = 1;
-       mark_reachable(obj, REACHABLE);
+       mark_object_reachable(obj);
 
        return 0;
 }
@@ -656,7 +704,7 @@ static int fsck_cache_tree(struct cache_tree *it)
                              sha1_to_hex(it->sha1));
                        return 1;
                }
-               mark_reachable(obj, REACHABLE);
+               mark_object_reachable(obj);
                obj->used = 1;
                if (obj->type != OBJ_TREE)
                        err |= objerror(obj, "non-tree in cache-tree");
@@ -666,60 +714,35 @@ static int fsck_cache_tree(struct cache_tree *it)
        return err;
 }
 
-static const char fsck_usage[] =
-"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
-"[--strict] [--verbose] <head-sha1>*]";
+static char const * const fsck_usage[] = {
+       "git-fsck [options] [<object>...]",
+       NULL
+};
+
+static struct option fsck_opts[] = {
+       OPT__VERBOSE(&verbose),
+       OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
+       OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
+       OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
+       OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
+       OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
+       OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
+       OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
+       OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
+                               "write dangling objects in .git/lost-found"),
+       OPT_END(),
+};
 
 int cmd_fsck(int argc, const char **argv, const char *prefix)
 {
        int i, heads;
 
-       track_object_refs = 1;
        errors_found = 0;
 
-       for (i = 1; i < argc; i++) {
-               const char *arg = argv[i];
-
-               if (!strcmp(arg, "--unreachable")) {
-                       show_unreachable = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--tags")) {
-                       show_tags = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--root")) {
-                       show_root = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--cache")) {
-                       keep_cache_objects = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--no-reflogs")) {
-                       include_reflogs = 0;
-                       continue;
-               }
-               if (!strcmp(arg, "--full")) {
-                       check_full = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--strict")) {
-                       check_strict = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--verbose")) {
-                       verbose = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "--lost-found")) {
-                       check_full = 1;
-                       include_reflogs = 0;
-                       write_lost_and_found = 1;
-                       continue;
-               }
-               if (*arg == '-')
-                       usage(fsck_usage);
+       argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
+       if (write_lost_and_found) {
+               check_full = 1;
+               include_reflogs = 0;
        }
 
        fsck_head_link();
@@ -741,22 +764,18 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
                        verify_pack(p, 0);
 
                for (p = packed_git; p; p = p->next) {
-                       uint32_t i, num;
+                       uint32_t j, num;
                        if (open_pack_index(p))
                                continue;
                        num = p->num_objects;
-                       for (i = 0; i < num; i++)
-                               fsck_sha1(nth_packed_object_sha1(p, i));
+                       for (j = 0; j < num; j++)
+                               fsck_sha1(nth_packed_object_sha1(p, j));
                }
        }
 
        heads = 0;
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
-
-               if (*arg == '-')
-                       continue;
-
                if (!get_sha1(arg, head_sha1)) {
                        struct object *obj = lookup_object(head_sha1);
 
@@ -765,7 +784,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
                                continue;
 
                        obj->used = 1;
-                       mark_reachable(obj, REACHABLE);
+                       mark_object_reachable(obj);
                        heads++;
                        continue;
                }
@@ -783,7 +802,6 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
        }
 
        if (keep_cache_objects) {
-               int i;
                read_cache();
                for (i = 0; i < active_nr; i++) {
                        unsigned int mode;
@@ -798,7 +816,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
                                continue;
                        obj = &blob->object;
                        obj->used = 1;
-                       mark_reachable(obj, REACHABLE);
+                       mark_object_reachable(obj);
                }
                if (active_cache_tree)
                        fsck_cache_tree(active_cache_tree);