Skip to content

Commit 2de307c

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3473d18 commit 2de307c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

fsck.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ static int report(struct fsck_options *options,
280280
return result;
281281
}
282282

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+
283296
void fsck_enable_object_names(struct fsck_options *options)
284297
{
285298
if (!options->object_names)
@@ -1237,6 +1250,32 @@ int fsck_objects_error_function(struct fsck_options *o,
12371250
return 1;
12381251
}
12391252

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+
12401279
static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
12411280
enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
12421281
struct fsck_options *options, const char *blob_type)

fsck.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,23 @@ int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
133133
enum fsck_msg_id msg_id,
134134
const char *message);
135135

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+
136142
struct fsck_object_report {
137143
const struct object_id *oid;
138144
enum object_type object_type;
139145
};
140146

147+
struct fsck_ref_report {
148+
const char *path;
149+
const struct object_id *oid;
150+
const char *referent;
151+
};
152+
141153
struct fsck_options {
142154
fsck_walk_func walk;
143155
fsck_error error_func;
@@ -175,6 +187,9 @@ struct fsck_options {
175187
.gitattributes_done = OIDSET_INIT, \
176188
.error_func = fsck_objects_error_cb_print_missing_gitmodules, \
177189
}
190+
#define FSCK_REFS_OPTIONS_DEFAULT { \
191+
.error_func = fsck_refs_error_function, \
192+
}
178193

179194
/* descend in all linked child objects
180195
* the return value is:
@@ -216,6 +231,16 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
216231
*/
217232
int fsck_finish(struct fsck_options *options);
218233

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+
219244
/*
220245
* Subsystem for storing human-readable names for each object.
221246
*

0 commit comments

Comments
 (0)