Skip to content

Commit 0d30fee

Browse files
derrickstoleegitster
authored andcommitted
fsck: create scaffolding for rev-index checks
The 'fsck' builtin checks many of Git's on-disk data structures, but does not currently validate the pack rev-index files (a .rev file to pair with a .pack and .idx file). Before doing a more-involved check process, create the scaffolding within builtin/fsck.c to have a new error type and add that error type when the API method verify_pack_revindex() returns an error. That method does nothing currently, but we will add checks to it in later changes. For now, check that 'git fsck' succeeds without any errors in the normal case. Future checks will be paired with tests that corrupt the .rev file appropriately. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c63503 commit 0d30fee

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

builtin/fsck.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "resolve-undo.h"
2525
#include "run-command.h"
2626
#include "worktree.h"
27+
#include "pack-revindex.h"
2728

2829
#define REACHABLE 0x0001
2930
#define SEEN 0x0002
@@ -53,6 +54,7 @@ static int name_objects;
5354
#define ERROR_REFS 010
5455
#define ERROR_COMMIT_GRAPH 020
5556
#define ERROR_MULTI_PACK_INDEX 040
57+
#define ERROR_PACK_REV_INDEX 0100
5658

5759
static const char *describe_object(const struct object_id *oid)
5860
{
@@ -856,6 +858,32 @@ static int mark_packed_for_connectivity(const struct object_id *oid,
856858
return 0;
857859
}
858860

861+
static int check_pack_rev_indexes(struct repository *r, int show_progress)
862+
{
863+
struct progress *progress = NULL;
864+
uint32_t pack_count = 0;
865+
int res = 0;
866+
867+
if (show_progress) {
868+
for (struct packed_git *p = get_all_packs(the_repository); p; p = p->next)
869+
pack_count++;
870+
progress = start_delayed_progress("Verifying reverse pack-indexes", pack_count);
871+
pack_count = 0;
872+
}
873+
874+
for (struct packed_git *p = get_all_packs(the_repository); p; p = p->next) {
875+
if (!load_pack_revindex(the_repository, p) &&
876+
verify_pack_revindex(p)) {
877+
error(_("invalid rev-index for pack '%s'"), p->pack_name);
878+
res = ERROR_PACK_REV_INDEX;
879+
}
880+
display_progress(progress, ++pack_count);
881+
}
882+
stop_progress(&progress);
883+
884+
return res;
885+
}
886+
859887
static char const * const fsck_usage[] = {
860888
N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
861889
" [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
@@ -1019,6 +1047,8 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
10191047
free_worktrees(worktrees);
10201048
}
10211049

1050+
errors_found |= check_pack_rev_indexes(the_repository, show_progress);
1051+
10221052
check_connectivity();
10231053

10241054
if (the_repository->settings.core_commit_graph) {

pack-revindex.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ int load_pack_revindex(struct repository *r, struct packed_git *p)
301301
return -1;
302302
}
303303

304+
/*
305+
* verify_pack_revindex verifies that the on-disk rev-index for the given
306+
* pack-file is the same that would be created if written from scratch.
307+
*
308+
* A negative number is returned on error.
309+
*/
310+
int verify_pack_revindex(struct packed_git *p)
311+
{
312+
return 0;
313+
}
314+
304315
int load_midx_revindex(struct multi_pack_index *m)
305316
{
306317
struct strbuf revindex_name = STRBUF_INIT;

pack-revindex.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ struct repository;
5151
*/
5252
int load_pack_revindex(struct repository *r, struct packed_git *p);
5353

54+
/*
55+
* verify_pack_revindex verifies that the on-disk rev-index for the given
56+
* pack-file is the same that would be created if written from scratch.
57+
*
58+
* A negative number is returned on error.
59+
*/
60+
int verify_pack_revindex(struct packed_git *p);
61+
5462
/*
5563
* load_midx_revindex loads the '.rev' file corresponding to the given
5664
* multi-pack index by mmap-ing it and assigning pointers in the

t/t5325-reverse-index.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,18 @@ test_expect_success 'revindex in-memory vs on-disk' '
131131
test_cmp on-disk in-core
132132
)
133133
'
134+
135+
test_expect_success 'fsck succeeds on good rev-index' '
136+
test_when_finished rm -fr repo &&
137+
git init repo &&
138+
(
139+
cd repo &&
140+
141+
test_commit commit &&
142+
git -c pack.writeReverseIndex=true repack -ad &&
143+
git fsck 2>err &&
144+
test_must_be_empty err
145+
)
146+
'
147+
134148
test_done

0 commit comments

Comments
 (0)