fsck: add refs report function

Introduce a new struct "fsck_ref_report" to contain the information we need when reporting refs-related messages. With the new "fsck_vreport" function, add a new function "fsck_report_ref" to report refs-related fsck error message. Unlike "report" function uses the exact parameters, we simply pass "struct fsck_ref_report *report" as the parameter. This is because at current we don't know exactly how many fields we need. By passing this parameter, we don't need to change this function prototype when we want to add more information into "fsck_ref_report". We have introduced "fsck_report_ref" function to report the error message for refs. We still need to add the corresponding callback function. Create refs-specific "error_func" callback "fsck_refs_error_function". Last, add "FSCK_REFS_OPTIONS_DEFAULT" macro to create default options when checking ref consistency. 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:27 UTC 2de307cdb2e80840b1fe6741561fed7c99fd8f08
2 files changed +64
fsck.c
+39
@@ -280,6 +280,19 @@ static int report(struct fsck_options *options,
280 return result;
281 }
282
283 +int fsck_report_ref(struct fsck_options *options,
284 + struct fsck_ref_report *report,
285 + enum fsck_msg_id msg_id,
286 + const char *fmt, ...)
287 +{
288 + va_list ap;
289 + int result;
290 + va_start(ap, fmt);
291 + result = fsck_vreport(options, report, msg_id, fmt, ap);
292 + va_end(ap);
293 + return result;
294 +}
295 +
296 void fsck_enable_object_names(struct fsck_options *options)
297 {
298 if (!options->object_names)
@@ -1237,6 +1250,32 @@ int fsck_objects_error_function(struct fsck_options *o,
1250 return 1;
1251 }
1252
1253 +int fsck_refs_error_function(struct fsck_options *options UNUSED,
1254 + void *fsck_report,
1255 + enum fsck_msg_type msg_type,
1256 + enum fsck_msg_id msg_id UNUSED,
1257 + const char *message)
1258 +{
1259 + struct fsck_ref_report *report = fsck_report;
1260 + struct strbuf sb = STRBUF_INIT;
1261 + int ret = 0;
1262 +
1263 + strbuf_addstr(&sb, report->path);
1264 +
1265 + if (report->oid)
1266 + strbuf_addf(&sb, " -> (%s)", oid_to_hex(report->oid));
1267 + else if (report->referent)
1268 + strbuf_addf(&sb, " -> (%s)", report->referent);
1269 +
1270 + if (msg_type == FSCK_WARN)
1271 + warning("%s: %s", sb.buf, message);
1272 + else
1273 + ret = error("%s: %s", sb.buf, message);
1274 +
1275 + strbuf_release(&sb);
1276 + return ret;
1277 +}
1278 +
1279 static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
1280 enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
1281 struct fsck_options *options, const char *blob_type)
fsck.h
+25
@@ -133,11 +133,23 @@ int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
133 enum fsck_msg_id msg_id,
134 const char *message);
135
136 +int fsck_refs_error_function(struct fsck_options *options,
137 + void *fsck_report,
138 + enum fsck_msg_type msg_type,
139 + enum fsck_msg_id msg_id,
140 + const char *message);
141 +
142 struct fsck_object_report {
143 const struct object_id *oid;
144 enum object_type object_type;
145 };
146
147 +struct fsck_ref_report {
148 + const char *path;
149 + const struct object_id *oid;
150 + const char *referent;
151 +};
152 +
153 struct fsck_options {
154 fsck_walk_func walk;
155 fsck_error error_func;
@@ -175,6 +187,9 @@ struct fsck_options {
187 .gitattributes_done = OIDSET_INIT, \
188 .error_func = fsck_objects_error_cb_print_missing_gitmodules, \
189 }
190 +#define FSCK_REFS_OPTIONS_DEFAULT { \
191 + .error_func = fsck_refs_error_function, \
192 +}
193
194 /* descend in all linked child objects
195 * the return value is:
@@ -216,6 +231,16 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
231 */
232 int fsck_finish(struct fsck_options *options);
233
234 +/*
235 + * Report an error or warning for refs.
236 + */
237 +__attribute__((format (printf, 4, 5)))
238 +int fsck_report_ref(struct fsck_options *options,
239 + struct fsck_ref_report *report,
240 + enum fsck_msg_id msg_id,
241 + const char *fmt, ...);
242 +
243 +
244 /*
245 * Subsystem for storing human-readable names for each object.
246 *