Create refs-specific "error_func" callback "fsck_refs_error_function" which could provide the following report messages for files backend 1. "ref_checkee": "fsck error name": "user message". 2. "ref_checkee.sub_ref_checkee": "fsck error name": "user message". 3. "ref_checkee -> (oid hex)": "fsck error name": "user message". 4. "ref_checkee.sub_ref_checkee -> (oid hex)": "fsck error name": "user message". "fsck_refs_error_function" uses the "ref_checkee" and "sub_ref_checkee" in the "fsck_refs_info" to indicate the information of the checked refs. For loose ref and reflog, it only uses the "ref_checkee". For packed refs and reftable refs, when checking the consistency of the file itself, it still only uses "ref_checkee". However, when checking the consistency of the ref or reflog contained in the file ,it will use the "sub_ref_checkee" to indicate that we are not checking the file but the incorporated ref or reflog. "fsck_refs_error_function" will use the "oid" parameter if the caller passes the oid, it will use "oid_to_hex" to get the corresponding hex value to report to the caller. Last, add "FSCK_REFS_OPTIONS_DEFAULT" and "FSCK_REFS_OPTIONS_STRICT" macros to create refs options easily. Mentored-by: Patrick Steinhardt <ps@xxxxxx> Mentored-by: Karthik Nayak <karthik.188@xxxxxxxxx> Signed-off-by: shejialuo <shejialuo@xxxxxxxxx> --- fsck.c | 29 +++++++++++++++++++++++++++++ fsck.h | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/fsck.c b/fsck.c index 9a7e3d8679..e87b13fdc3 100644 --- a/fsck.c +++ b/fsck.c @@ -1247,6 +1247,35 @@ int fsck_objects_error_function(struct fsck_options *o, return 1; } +int fsck_refs_error_function(struct fsck_options *options, + const struct object_id *oid, + enum object_type object_type UNUSED, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id UNUSED, + const char *message) +{ + struct strbuf sb = STRBUF_INIT; + struct fsck_refs_info *refs_info = &options->refs_info; + int ret = 0; + + if (the_repository->ref_storage_format == REF_STORAGE_FORMAT_FILES) { + strbuf_addstr(&sb, refs_info->ref_checkee); + if (refs_info->u.files.sub_ref_checkee) + strbuf_addf(&sb, ".%s", refs_info->u.files.sub_ref_checkee); + + if (oid) + strbuf_addf(&sb, " -> (%s)", oid_to_hex(oid)); + } + + if (msg_type == FSCK_WARN) + warning("%s: %s", sb.buf, message); + else + ret = error("%s: %s", sb.buf, message); + + strbuf_release(&sb); + return ret; +} + static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done, enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type, struct fsck_options *options, const char *blob_type) diff --git a/fsck.h b/fsck.h index 6411437334..a3870ffe2b 100644 --- a/fsck.h +++ b/fsck.h @@ -132,6 +132,12 @@ int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); +int fsck_refs_error_function(struct fsck_options *options, + const struct object_id *oid, + enum object_type object_type, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id, + const char *message); /* * The information for reporting refs-related error message @@ -183,6 +189,13 @@ struct fsck_options { .gitattributes_done = OIDSET_INIT, \ .error_func = fsck_objects_error_cb_print_missing_gitmodules, \ } +#define FSCK_REFS_OPTIONS_DEFAULT { \ + .error_func = fsck_refs_error_function, \ +} +#define FSCK_REFS_OPTIONS_STRICT { \ + .strict = 1, \ + .error_func = fsck_refs_error_function, \ +} /* descend in all linked child objects * the return value is: -- 2.45.2