fsck: add a unified interface for reporting fsck messages

The static function "report" provided by "fsck.c" aims at checking error type and calling the callback "error_func" to report the message. Both refs and objects need to check the error type of the current fsck message. In order to extract this common behavior, create a new function "fsck_vreport". Instead of using "...", provide "va_list" to allow more flexibility. Instead of changing "report" prototype to be align with the "fsck_vreport" function, we leave the "report" prototype unchanged due to the reason that there are nearly 62 references about "report" function. Simply change "report" function to use "fsck_vreport" to report objects related messages. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Aug 8, 2024 at 19:26 UTC 3473d18fad56b40ac3bce457b3779866461cd921
1 file changed +30 -14
fsck.c
+30 -14
@@ -226,16 +226,15 @@ static int object_on_skiplist(struct fsck_options *opts,
226 return opts && oid && oidset_contains(&opts->skip_oids, oid);
227 }
228
229 -__attribute__((format (printf, 5, 6)))
230 -static int report(struct fsck_options *options,
231 - const struct object_id *oid, enum object_type object_type,
232 - enum fsck_msg_id msg_id, const char *fmt, ...)
229 +/*
230 + * Provide the common functionality for either fscking refs or objects.
231 + * It will get the current msg error type and call the error_func callback
232 + * which is registered in the "fsck_options" struct.
233 + */
234 +static int fsck_vreport(struct fsck_options *options,
235 + void *fsck_report,
236 + enum fsck_msg_id msg_id, const char *fmt, va_list ap)
237 {
234 - va_list ap;
235 - struct fsck_object_report report = {
236 - .oid = oid,
237 - .object_type = object_type
238 - };
238 struct strbuf sb = STRBUF_INIT;
239 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
240 int result;
@@ -243,9 +242,6 @@ static int report(struct fsck_options *options,
242 if (msg_type == FSCK_IGNORE)
243 return 0;
244
246 - if (object_on_skiplist(options, oid))
247 - return 0;
248 -
245 if (msg_type == FSCK_FATAL)
246 msg_type = FSCK_ERROR;
247 else if (msg_type == FSCK_INFO)
@@ -254,11 +250,31 @@ static int report(struct fsck_options *options,
250 prepare_msg_ids();
251 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
252
257 - va_start(ap, fmt);
253 strbuf_vaddf(&sb, fmt, ap);
259 - result = options->error_func(options, &report,
254 + result = options->error_func(options, fsck_report,
255 msg_type, msg_id, sb.buf);
256 strbuf_release(&sb);
257 +
258 + return result;
259 +}
260 +
261 +__attribute__((format (printf, 5, 6)))
262 +static int report(struct fsck_options *options,
263 + const struct object_id *oid, enum object_type object_type,
264 + enum fsck_msg_id msg_id, const char *fmt, ...)
265 +{
266 + va_list ap;
267 + struct fsck_object_report report = {
268 + .oid = oid,
269 + .object_type = object_type
270 + };
271 + int result;
272 +
273 + if (object_on_skiplist(options, oid))
274 + return 0;
275 +
276 + va_start(ap, fmt);
277 + result = fsck_vreport(options, &report, msg_id, fmt, ap);
278 va_end(ap);
279
280 return result;