fsck: factor out index fsck

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>

Jeff King committed Feb 24, 2023 at 03:07 UTC 8840069a37e69129ed3c2792ddb172557f98e205
1 file changed +30 -24
builtin/fsck.c
+30 -24
@@ -1,4 +1,3 @@
1 -#define USE_THE_INDEX_VARIABLE
1 #include "builtin.h"
2 #include "cache.h"
3 #include "repository.h"
@@ -796,6 +795,35 @@ static int fsck_resolve_undo(struct index_state *istate)
795 return 0;
796 }
797
798 +static void fsck_index(struct index_state *istate)
799 +{
800 + unsigned int i;
801 +
802 + /* TODO: audit for interaction with sparse-index. */
803 + ensure_full_index(istate);
804 + for (i = 0; i < istate->cache_nr; i++) {
805 + unsigned int mode;
806 + struct blob *blob;
807 + struct object *obj;
808 +
809 + mode = istate->cache[i]->ce_mode;
810 + if (S_ISGITLINK(mode))
811 + continue;
812 + blob = lookup_blob(the_repository,
813 + &istate->cache[i]->oid);
814 + if (!blob)
815 + continue;
816 + obj = &blob->object;
817 + obj->flags |= USED;
818 + fsck_put_object_name(&fsck_walk_options, &obj->oid,
819 + ":%s", istate->cache[i]->name);
820 + mark_object_reachable(obj);
821 + }
822 + if (istate->cache_tree)
823 + fsck_cache_tree(istate->cache_tree);
824 + fsck_resolve_undo(istate);
825 +}
826 +
827 static void mark_object_for_connectivity(const struct object_id *oid)
828 {
829 struct object *obj = lookup_unknown_object(the_repository, oid);
@@ -959,29 +987,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
987 verify_index_checksum = 1;
988 verify_ce_order = 1;
989 repo_read_index(the_repository);
962 - /* TODO: audit for interaction with sparse-index. */
963 - ensure_full_index(&the_index);
964 - for (i = 0; i < the_index.cache_nr; i++) {
965 - unsigned int mode;
966 - struct blob *blob;
967 - struct object *obj;
968 -
969 - mode = the_index.cache[i]->ce_mode;
970 - if (S_ISGITLINK(mode))
971 - continue;
972 - blob = lookup_blob(the_repository,
973 - &the_index.cache[i]->oid);
974 - if (!blob)
975 - continue;
976 - obj = &blob->object;
977 - obj->flags |= USED;
978 - fsck_put_object_name(&fsck_walk_options, &obj->oid,
979 - ":%s", the_index.cache[i]->name);
980 - mark_object_reachable(obj);
981 - }
982 - if (the_index.cache_tree)
983 - fsck_cache_tree(the_index.cache_tree);
984 - fsck_resolve_undo(&the_index);
990 + fsck_index(the_repository->index);
991 }
992
993 check_connectivity();