Skip to content

Commit ab6f79d

Browse files
shejialuogitster
authored andcommitted
refs: set up ref consistency check infrastructure
The "struct ref_store" is the base class which contains the "be" pointer which provides backend-specific functions whose interfaces are defined in the "ref_storage_be". We could reuse this polymorphism to define only one interface. For every backend, we need to provide its own function pointer. The interfaces defined in the `ref_storage_be` are carefully structured in semantic. It's organized as the five parts: 1. The name and the initialization interfaces. 2. The ref transaction interfaces. 3. The ref internal interfaces (pack, rename and copy). 4. The ref filesystem interfaces. 5. The reflog related interfaces. To keep consistent with the git-fsck(1), add a new interface named "fsck_refs_fn" to the end of "ref_storage_be". This semantic cannot be grouped into any above five categories. Explicitly add blank line to make it different from others. Last, implement placeholder functions for each ref backends. Mentored-by: Patrick Steinhardt <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2de307c commit ab6f79d

File tree

7 files changed

+58
-1
lines changed

7 files changed

+58
-1
lines changed

refs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ int check_refname_format(const char *refname, int flags)
318318
return check_or_sanitize_refname(refname, flags, NULL);
319319
}
320320

321+
int refs_fsck(struct ref_store *refs, struct fsck_options *o)
322+
{
323+
return refs->be->fsck(refs, o);
324+
}
325+
321326
void sanitize_refname_component(const char *refname, struct strbuf *out)
322327
{
323328
if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out))

refs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "commit.h"
55
#include "repository.h"
66

7+
struct fsck_options;
78
struct object_id;
89
struct ref_store;
910
struct strbuf;
@@ -541,6 +542,13 @@ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_dat
541542
*/
542543
int check_refname_format(const char *refname, int flags);
543544

545+
/*
546+
* Check the reference database for consistency. Return 0 if refs and
547+
* reflogs are consistent, and non-zero otherwise. The errors will be
548+
* written to stderr.
549+
*/
550+
int refs_fsck(struct ref_store *refs, struct fsck_options *o);
551+
544552
/*
545553
* Apply the rules from check_refname_format, but mutate the result until it
546554
* is acceptable, and place the result in "out".

refs/debug.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
419419
return res;
420420
}
421421

422+
static int debug_fsck(struct ref_store *ref_store,
423+
struct fsck_options *o)
424+
{
425+
struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
426+
int res = drefs->refs->be->fsck(drefs->refs, o);
427+
trace_printf_key(&trace_refs, "fsck: %d\n", res);
428+
return res;
429+
}
430+
422431
struct ref_storage_be refs_be_debug = {
423432
.name = "debug",
424433
.init = NULL,
@@ -451,4 +460,6 @@ struct ref_storage_be refs_be_debug = {
451460
.create_reflog = debug_create_reflog,
452461
.delete_reflog = debug_delete_reflog,
453462
.reflog_expire = debug_reflog_expire,
463+
464+
.fsck = debug_fsck,
454465
};

refs/files-backend.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3408,6 +3408,15 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
34083408
return ret;
34093409
}
34103410

3411+
static int files_fsck(struct ref_store *ref_store,
3412+
struct fsck_options *o)
3413+
{
3414+
struct files_ref_store *refs =
3415+
files_downcast(ref_store, REF_STORE_READ, "fsck");
3416+
3417+
return refs->packed_ref_store->be->fsck(refs->packed_ref_store, o);
3418+
}
3419+
34113420
struct ref_storage_be refs_be_files = {
34123421
.name = "files",
34133422
.init = files_ref_store_init,
@@ -3434,5 +3443,7 @@ struct ref_storage_be refs_be_files = {
34343443
.reflog_exists = files_reflog_exists,
34353444
.create_reflog = files_create_reflog,
34363445
.delete_reflog = files_delete_reflog,
3437-
.reflog_expire = files_reflog_expire
3446+
.reflog_expire = files_reflog_expire,
3447+
3448+
.fsck = files_fsck,
34383449
};

refs/packed-backend.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,12 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
17351735
return empty_ref_iterator_begin();
17361736
}
17371737

1738+
static int packed_fsck(struct ref_store *ref_store,
1739+
struct fsck_options *o)
1740+
{
1741+
return 0;
1742+
}
1743+
17381744
struct ref_storage_be refs_be_packed = {
17391745
.name = "packed",
17401746
.init = packed_ref_store_init,
@@ -1762,4 +1768,6 @@ struct ref_storage_be refs_be_packed = {
17621768
.create_reflog = NULL,
17631769
.delete_reflog = NULL,
17641770
.reflog_expire = NULL,
1771+
1772+
.fsck = packed_fsck,
17651773
};

refs/refs-internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "refs.h"
55
#include "iterator.h"
66

7+
struct fsck_options;
78
struct ref_transaction;
89

910
/*
@@ -650,6 +651,9 @@ typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname,
650651
typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname,
651652
struct strbuf *referent);
652653

654+
typedef int fsck_fn(struct ref_store *ref_store,
655+
struct fsck_options *o);
656+
653657
struct ref_storage_be {
654658
const char *name;
655659
ref_store_init_fn *init;
@@ -677,6 +681,8 @@ struct ref_storage_be {
677681
create_reflog_fn *create_reflog;
678682
delete_reflog_fn *delete_reflog;
679683
reflog_expire_fn *reflog_expire;
684+
685+
fsck_fn *fsck;
680686
};
681687

682688
extern struct ref_storage_be refs_be_files;

refs/reftable-backend.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,12 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
23032303
return ret;
23042304
}
23052305

2306+
static int reftable_be_fsck(struct ref_store *ref_store,
2307+
struct fsck_options *o)
2308+
{
2309+
return 0;
2310+
}
2311+
23062312
struct ref_storage_be refs_be_reftable = {
23072313
.name = "reftable",
23082314
.init = reftable_be_init,
@@ -2330,4 +2336,6 @@ struct ref_storage_be refs_be_reftable = {
23302336
.create_reflog = reftable_be_create_reflog,
23312337
.delete_reflog = reftable_be_delete_reflog,
23322338
.reflog_expire = reftable_be_reflog_expire,
2339+
2340+
.fsck = reftable_be_fsck,
23332341
};

0 commit comments

Comments
 (0)