]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: make "fsck_error" callback generic
authorshejialuo <shejialuo@gmail.com>
Thu, 8 Aug 2024 11:26:47 +0000 (19:26 +0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Aug 2024 16:36:52 +0000 (09:36 -0700)
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>
builtin/fsck.c
builtin/mktag.c
fsck.c
fsck.h
object-file.c

index 6d86bbe1e98e48124147f0a22167f2013e9512f6..766bbd014da2be98e569cc242c430ed1fe3a6c7f 100644 (file)
@@ -90,12 +90,15 @@ static int objerror(struct object *obj, const char *err)
 }
 
 static int fsck_objects_error_func(struct fsck_options *o UNUSED,
-                                  const struct object_id *oid,
-                                  enum object_type object_type,
+                                  void *fsck_report,
                                   enum fsck_msg_type msg_type,
                                   enum fsck_msg_id msg_id UNUSED,
                                   const char *message)
 {
+       struct fsck_object_report *report = fsck_report;
+       const struct object_id *oid = report->oid;
+       enum object_type object_type = report->object_type;
+
        switch (msg_type) {
        case FSCK_WARN:
                /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
index 4767f1a97e6df25ca09eba0846feaf404e08fe46..c6b644219fb19e2faeba612cbf74c7e3bd55b998 100644 (file)
@@ -18,8 +18,7 @@ static int option_strict = 1;
 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
 
 static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
-                                const struct object_id *oid UNUSED,
-                                enum object_type object_type UNUSED,
+                                void *fsck_report UNUSED,
                                 enum fsck_msg_type msg_type,
                                 enum fsck_msg_id msg_id UNUSED,
                                 const char *message)
diff --git a/fsck.c b/fsck.c
index 8347842cfb2c5915b22b99f4a66033602f0bee4e..cca6ae144f1e23f581f398c22295d7fde9614d45 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -232,6 +232,10 @@ static int report(struct fsck_options *options,
                  enum fsck_msg_id msg_id, const char *fmt, ...)
 {
        va_list ap;
+       struct fsck_object_report report = {
+               .oid = oid,
+               .object_type = object_type
+       };
        struct strbuf sb = STRBUF_INIT;
        enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
        int result;
@@ -252,7 +256,7 @@ static int report(struct fsck_options *options,
 
        va_start(ap, fmt);
        strbuf_vaddf(&sb, fmt, ap);
-       result = options->error_func(options, oid, object_type,
+       result = options->error_func(options, &report,
                                     msg_type, msg_id, sb.buf);
        strbuf_release(&sb);
        va_end(ap);
@@ -1201,12 +1205,14 @@ int fsck_buffer(const struct object_id *oid, enum object_type type,
 }
 
 int fsck_objects_error_function(struct fsck_options *o,
-                       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)
+                               void *fsck_report,
+                               enum fsck_msg_type msg_type,
+                               enum fsck_msg_id msg_id UNUSED,
+                               const char *message)
 {
+       struct fsck_object_report *report = fsck_report;
+       const struct object_id *oid = report->oid;
+
        if (msg_type == FSCK_WARN) {
                warning("object %s: %s", fsck_describe_object(o, oid), message);
                return 0;
@@ -1304,16 +1310,16 @@ int git_fsck_config(const char *var, const char *value,
  */
 
 int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
-                                                  const struct object_id *oid,
-                                                  enum object_type object_type,
+                                                  void *fsck_report,
                                                   enum fsck_msg_type msg_type,
                                                   enum fsck_msg_id msg_id,
                                                   const char *message)
 {
        if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
-               puts(oid_to_hex(oid));
+               struct fsck_object_report *report = fsck_report;
+               puts(oid_to_hex(report->oid));
                return 0;
        }
-       return fsck_objects_error_function(o, oid, object_type,
+       return fsck_objects_error_function(o, fsck_report,
                                           msg_type, msg_id, message);
 }
diff --git a/fsck.h b/fsck.h
index 41ebebbb5949b65e2b26cf7de4eaf61209fe4ae3..3b80d02506f34a66c0efbbf0ca680f5bbf4b5c62 100644 (file)
--- a/fsck.h
+++ b/fsck.h
@@ -114,23 +114,30 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type);
 typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type,
                              void *data, struct fsck_options *options);
 
-/* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */
+/*
+ * Callback for reporting errors either for objects or refs. The "fsck_report"
+ * is a generic pointer that can be used to pass any information.
+ */
 typedef int (*fsck_error)(struct fsck_options *o,
-                         const struct object_id *oid, enum object_type object_type,
+                         void *fsck_report,
                          enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
                          const char *message);
 
 int fsck_objects_error_function(struct fsck_options *o,
-                               const struct object_id *oid, enum object_type object_type,
+                               void *fsck_report,
                                enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
                                const char *message);
 int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
-                                                  const struct object_id *oid,
-                                                  enum object_type object_type,
+                                                  void *fsck_report,
                                                   enum fsck_msg_type msg_type,
                                                   enum fsck_msg_id msg_id,
                                                   const char *message);
 
+struct fsck_object_report {
+       const struct object_id *oid;
+       enum object_type object_type;
+};
+
 struct fsck_options {
        fsck_walk_func walk;
        fsck_error error_func;
index 065103be3ea923e3fcc87bf5e8e366d202544b14..05ac6ebed65bbfad0095ecd407aaf514d29bb285 100644 (file)
@@ -2470,11 +2470,10 @@ int repo_has_object_file(struct repository *r,
  * give more context.
  */
 static int hash_format_check_report(struct fsck_options *opts UNUSED,
-                                    const struct object_id *oid UNUSED,
-                                    enum object_type object_type UNUSED,
-                                    enum fsck_msg_type msg_type UNUSED,
-                                    enum fsck_msg_id msg_id UNUSED,
-                                    const char *message)
+                                   void *fsck_report UNUSED,
+                                   enum fsck_msg_type msg_type UNUSED,
+                                   enum fsck_msg_id msg_id UNUSED,
+                                   const char *message)
 {
        error(_("object fails fsck: %s"), message);
        return 1;