fsck: make "fsck_error" callback generic

The "fsck_error" callback is designed to report the objects-related error messages. It accepts two parameter "oid" and "object_type" which is not generic. In order to provide a unified callback which can report either objects or refs, remove the objects-related parameters and add the generic parameter "void *fsck_report". Create a new "fsck_object_report" structure which incorporates the removed parameters "oid" and "object_type". Then change the corresponding references to adapt to new "fsck_error" callback. 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 0ec5dfe8c45be2efd6350b3a1a3885c795a85578
5 files changed +38 -24
builtin/fsck.c
+5 -2
@@ -90,12 +90,15 @@ static int objerror(struct object *obj, const char *err)
90 }
91
92 static int fsck_objects_error_func(struct fsck_options *o UNUSED,
93 - const struct object_id *oid,
94 - enum object_type object_type,
93 + void *fsck_report,
94 enum fsck_msg_type msg_type,
95 enum fsck_msg_id msg_id UNUSED,
96 const char *message)
97 {
98 + struct fsck_object_report *report = fsck_report;
99 + const struct object_id *oid = report->oid;
100 + enum object_type object_type = report->object_type;
101 +
102 switch (msg_type) {
103 case FSCK_WARN:
104 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
builtin/mktag.c
+1 -2
@@ -18,8 +18,7 @@ static int option_strict = 1;
18 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
19
20 static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
21 - const struct object_id *oid UNUSED,
22 - enum object_type object_type UNUSED,
21 + void *fsck_report UNUSED,
22 enum fsck_msg_type msg_type,
23 enum fsck_msg_id msg_id UNUSED,
24 const char *message)
fsck.c
+16 -10
@@ -232,6 +232,10 @@ static int report(struct fsck_options *options,
232 enum fsck_msg_id msg_id, const char *fmt, ...)
233 {
234 va_list ap;
235 + struct fsck_object_report report = {
236 + .oid = oid,
237 + .object_type = object_type
238 + };
239 struct strbuf sb = STRBUF_INIT;
240 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
241 int result;
@@ -252,7 +256,7 @@ static int report(struct fsck_options *options,
256
257 va_start(ap, fmt);
258 strbuf_vaddf(&sb, fmt, ap);
255 - result = options->error_func(options, oid, object_type,
259 + result = options->error_func(options, &report,
260 msg_type, msg_id, sb.buf);
261 strbuf_release(&sb);
262 va_end(ap);
@@ -1201,12 +1205,14 @@ int fsck_buffer(const struct object_id *oid, enum object_type type,
1205 }
1206
1207 int fsck_objects_error_function(struct fsck_options *o,
1204 - const struct object_id *oid,
1205 - enum object_type object_type UNUSED,
1206 - enum fsck_msg_type msg_type,
1207 - enum fsck_msg_id msg_id UNUSED,
1208 - const char *message)
1208 + void *fsck_report,
1209 + enum fsck_msg_type msg_type,
1210 + enum fsck_msg_id msg_id UNUSED,
1211 + const char *message)
1212 {
1213 + struct fsck_object_report *report = fsck_report;
1214 + const struct object_id *oid = report->oid;
1215 +
1216 if (msg_type == FSCK_WARN) {
1217 warning("object %s: %s", fsck_describe_object(o, oid), message);
1218 return 0;
@@ -1304,16 +1310,16 @@ int git_fsck_config(const char *var, const char *value,
1310 */
1311
1312 int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
1307 - const struct object_id *oid,
1308 - enum object_type object_type,
1313 + void *fsck_report,
1314 enum fsck_msg_type msg_type,
1315 enum fsck_msg_id msg_id,
1316 const char *message)
1317 {
1318 if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1314 - puts(oid_to_hex(oid));
1319 + struct fsck_object_report *report = fsck_report;
1320 + puts(oid_to_hex(report->oid));
1321 return 0;
1322 }
1317 - return fsck_objects_error_function(o, oid, object_type,
1323 + return fsck_objects_error_function(o, fsck_report,
1324 msg_type, msg_id, message);
1325 }
fsck.h
+12 -5
@@ -114,23 +114,30 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type);
114 typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type,
115 void *data, struct fsck_options *options);
116
117 -/* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */
117 +/*
118 + * Callback for reporting errors either for objects or refs. The "fsck_report"
119 + * is a generic pointer that can be used to pass any information.
120 + */
121 typedef int (*fsck_error)(struct fsck_options *o,
119 - const struct object_id *oid, enum object_type object_type,
122 + void *fsck_report,
123 enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
124 const char *message);
125
126 int fsck_objects_error_function(struct fsck_options *o,
124 - const struct object_id *oid, enum object_type object_type,
127 + void *fsck_report,
128 enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
129 const char *message);
130 int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
128 - const struct object_id *oid,
129 - enum object_type object_type,
131 + void *fsck_report,
132 enum fsck_msg_type msg_type,
133 enum fsck_msg_id msg_id,
134 const char *message);
135
136 +struct fsck_object_report {
137 + const struct object_id *oid;
138 + enum object_type object_type;
139 +};
140 +
141 struct fsck_options {
142 fsck_walk_func walk;
143 fsck_error error_func;
object-file.c
+4 -5
@@ -2470,11 +2470,10 @@ int repo_has_object_file(struct repository *r,
2470 * give more context.
2471 */
2472 static int hash_format_check_report(struct fsck_options *opts UNUSED,
2473 - const struct object_id *oid UNUSED,
2474 - enum object_type object_type UNUSED,
2475 - enum fsck_msg_type msg_type UNUSED,
2476 - enum fsck_msg_id msg_id UNUSED,
2477 - const char *message)
2473 + void *fsck_report UNUSED,
2474 + enum fsck_msg_type msg_type UNUSED,
2475 + enum fsck_msg_id msg_id UNUSED,
2476 + const char *message)
2477 {
2478 error(_("object fails fsck: %s"), message);
2479 return 1;