]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: factor out index fsck
authorJeff King <peff@peff.net>
Fri, 24 Feb 2023 08:07:32 +0000 (03:07 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Feb 2023 17:30:58 +0000 (09:30 -0800)
The code to fsck an index operates directly on the_index. Let's move it
into its own function in preparation for handling the index files from
other worktrees.

Since we now have only a single reference to the_index, let's drop
our USE_THE_INDEX_VARIABLE definition and just use the_repository.index
directly. That's a minor cleanup, but also ensures that we didn't miss
any references when moving the code into fsck_index().

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fsck.c

index d207bd909b4b6da0671628c20bb29e98f427b550..fa101e0db2efaf522ab3784c0dfdfd847a014968 100644 (file)
@@ -1,4 +1,3 @@
-#define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
 #include "repository.h"
@@ -796,6 +795,35 @@ static int fsck_resolve_undo(struct index_state *istate)
        return 0;
 }
 
+static void fsck_index(struct index_state *istate)
+{
+       unsigned int i;
+
+       /* TODO: audit for interaction with sparse-index. */
+       ensure_full_index(istate);
+       for (i = 0; i < istate->cache_nr; i++) {
+               unsigned int mode;
+               struct blob *blob;
+               struct object *obj;
+
+               mode = istate->cache[i]->ce_mode;
+               if (S_ISGITLINK(mode))
+                       continue;
+               blob = lookup_blob(the_repository,
+                                  &istate->cache[i]->oid);
+               if (!blob)
+                       continue;
+               obj = &blob->object;
+               obj->flags |= USED;
+               fsck_put_object_name(&fsck_walk_options, &obj->oid,
+                                    ":%s", istate->cache[i]->name);
+               mark_object_reachable(obj);
+       }
+       if (istate->cache_tree)
+               fsck_cache_tree(istate->cache_tree);
+       fsck_resolve_undo(istate);
+}
+
 static void mark_object_for_connectivity(const struct object_id *oid)
 {
        struct object *obj = lookup_unknown_object(the_repository, oid);
@@ -959,29 +987,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
                verify_index_checksum = 1;
                verify_ce_order = 1;
                repo_read_index(the_repository);
-               /* TODO: audit for interaction with sparse-index. */
-               ensure_full_index(&the_index);
-               for (i = 0; i < the_index.cache_nr; i++) {
-                       unsigned int mode;
-                       struct blob *blob;
-                       struct object *obj;
-
-                       mode = the_index.cache[i]->ce_mode;
-                       if (S_ISGITLINK(mode))
-                               continue;
-                       blob = lookup_blob(the_repository,
-                                          &the_index.cache[i]->oid);
-                       if (!blob)
-                               continue;
-                       obj = &blob->object;
-                       obj->flags |= USED;
-                       fsck_put_object_name(&fsck_walk_options, &obj->oid,
-                                            ":%s", the_index.cache[i]->name);
-                       mark_object_reachable(obj);
-               }
-               if (the_index.cache_tree)
-                       fsck_cache_tree(the_index.cache_tree);
-               fsck_resolve_undo(&the_index);
+               fsck_index(the_repository->index);
        }
 
        check_connectivity();