fsck.h: move FSCK_{FATAL,INFO,ERROR,WARN,IGNORE} into an enum

Move the FSCK_{FATAL,INFO,ERROR,WARN,IGNORE} defines into a new fsck_msg_type enum. These defines were originally introduced in: - ba002f3b28a (builtin-fsck: move common object checking code to fsck.c, 2008-02-25) - f50c4407305 (fsck: disallow demoting grave fsck errors to warnings, 2015-06-22) - efaba7cc77f (fsck: optionally ignore specific fsck issues completely, 2015-06-22) - f27d05b1704 (fsck: allow upgrading fsck warnings to errors, 2015-06-22) The reason these were defined in two different places is because we use FSCK_{IGNORE,INFO,FATAL} only in fsck.c, but FSCK_{ERROR,WARN} are used by external callbacks. Untangling that would take some more work, since we expose the new "enum fsck_msg_type" to both. Similar to "enum object_type" it's not worth structuring the API in such a way that only those who need FSCK_{ERROR,WARN} pass around a different type. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 28, 2021 at 15:15 UTC 1b32b59f9bd78b3475195a6e99c629a5ffefdea0
5 files changed +25 -20
builtin/fsck.c
+1 -1
@@ -84,7 +84,7 @@ static int objerror(struct object *obj, const char *err)
84 static int fsck_error_func(struct fsck_options *o,
85 const struct object_id *oid,
86 enum object_type object_type,
87 - int msg_type, const char *message)
87 + enum fsck_msg_type msg_type, const char *message)
88 {
89 switch (msg_type) {
90 case FSCK_WARN:
builtin/index-pack.c
+2 -1
@@ -1716,7 +1716,8 @@ static void show_pack_info(int stat_only)
1716 static int print_dangling_gitmodules(struct fsck_options *o,
1717 const struct object_id *oid,
1718 enum object_type object_type,
1719 - int msg_type, const char *message)
1719 + enum fsck_msg_type msg_type,
1720 + const char *message)
1721 {
1722 /*
1723 * NEEDSWORK: Plumb the MSG_ID (from fsck.c) here and use it
builtin/mktag.c
+2 -1
@@ -17,7 +17,8 @@ static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
17 static int mktag_fsck_error_func(struct fsck_options *o,
18 const struct object_id *oid,
19 enum object_type object_type,
20 - int msg_type, const char *message)
20 + enum fsck_msg_type msg_type,
21 + const char *message)
22 {
23 switch (msg_type) {
24 case FSCK_WARN:
fsck.c
+10 -11
@@ -22,9 +22,6 @@
22 static struct oidset gitmodules_found = OIDSET_INIT;
23 static struct oidset gitmodules_done = OIDSET_INIT;
24
25 -#define FSCK_FATAL -1
26 -#define FSCK_INFO -2
27 -
25 #define FOREACH_MSG_ID(FUNC) \
26 /* fatal errors */ \
27 FUNC(NUL_IN_HEADER, FATAL) \
@@ -97,7 +94,7 @@ static struct {
94 const char *id_string;
95 const char *downcased;
96 const char *camelcased;
100 - int msg_type;
97 + enum fsck_msg_type msg_type;
98 } msg_id_info[FSCK_MSG_MAX + 1] = {
99 FOREACH_MSG_ID(MSG_ID)
100 { NULL, NULL, NULL, -1 }
@@ -164,13 +161,13 @@ void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
161 list_config_item(list, prefix, msg_id_info[i].camelcased);
162 }
163
167 -static int fsck_msg_type(enum fsck_msg_id msg_id,
164 +static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
165 struct fsck_options *options)
166 {
167 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
168
169 if (!options->msg_type) {
173 - int msg_type = msg_id_info[msg_id].msg_type;
170 + enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
171
172 if (options->strict && msg_type == FSCK_WARN)
173 msg_type = FSCK_ERROR;
@@ -180,7 +177,7 @@ static int fsck_msg_type(enum fsck_msg_id msg_id,
177 return options->msg_type[msg_id];
178 }
179
183 -static int parse_msg_type(const char *str)
180 +static enum fsck_msg_type parse_msg_type(const char *str)
181 {
182 if (!strcmp(str, "error"))
183 return FSCK_ERROR;
@@ -203,7 +200,8 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type)
200 void fsck_set_msg_type(struct fsck_options *options,
201 const char *msg_id_str, const char *msg_type_str)
202 {
206 - int msg_id = parse_msg_id(msg_id_str), msg_type;
203 + int msg_id = parse_msg_id(msg_id_str);
204 + enum fsck_msg_type msg_type;
205
206 if (msg_id < 0)
207 die("Unhandled message id: %s", msg_id_str);
@@ -214,7 +212,7 @@ void fsck_set_msg_type(struct fsck_options *options,
212
213 if (!options->msg_type) {
214 int i;
217 - int *severity;
215 + enum fsck_msg_type *severity;
216 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
217 for (i = 0; i < FSCK_MSG_MAX; i++)
218 severity[i] = fsck_msg_type(i, options);
@@ -275,7 +273,8 @@ static int report(struct fsck_options *options,
273 {
274 va_list ap;
275 struct strbuf sb = STRBUF_INIT;
278 - int msg_type = fsck_msg_type(msg_id, options), result;
276 + enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
277 + int result;
278
279 if (msg_type == FSCK_IGNORE)
280 return 0;
@@ -1247,7 +1246,7 @@ int fsck_object(struct object *obj, void *data, unsigned long size,
1246 int fsck_error_function(struct fsck_options *o,
1247 const struct object_id *oid,
1248 enum object_type object_type,
1250 - int msg_type, const char *message)
1249 + enum fsck_msg_type msg_type, const char *message)
1250 {
1251 if (msg_type == FSCK_WARN) {
1252 warning("object %s: %s", fsck_describe_object(o, oid), message);
fsck.h
+10 -6
@@ -3,9 +3,13 @@
3
4 #include "oidset.h"
5
6 -#define FSCK_ERROR 1
7 -#define FSCK_WARN 2
8 -#define FSCK_IGNORE 3
6 +enum fsck_msg_type {
7 + FSCK_INFO = -2,
8 + FSCK_FATAL = -1,
9 + FSCK_ERROR = 1,
10 + FSCK_WARN,
11 + FSCK_IGNORE
12 +};
13
14 struct fsck_options;
15 struct object;
@@ -29,17 +33,17 @@ typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type,
33 /* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */
34 typedef int (*fsck_error)(struct fsck_options *o,
35 const struct object_id *oid, enum object_type object_type,
32 - int msg_type, const char *message);
36 + enum fsck_msg_type msg_type, const char *message);
37
38 int fsck_error_function(struct fsck_options *o,
39 const struct object_id *oid, enum object_type object_type,
36 - int msg_type, const char *message);
40 + enum fsck_msg_type msg_type, const char *message);
41
42 struct fsck_options {
43 fsck_walk_func walk;
44 fsck_error error_func;
45 unsigned strict:1;
42 - int *msg_type;
46 + enum fsck_msg_type *msg_type;
47 struct oidset skiplist;
48 kh_oid_map_t *object_names;
49 };