fsck.c: add an fsck_set_msg_type() API that takes enums

Change code I added in acf9de4c94e (mktag: use fsck instead of custom verify_tag(), 2021-01-05) to make use of a new API function that takes the fsck_msg_{id,type} types, instead of arbitrary strings that we'll (hopefully) parse into those types. At the time that the fsck_set_msg_type() API was introduced in 0282f4dced0 (fsck: offer a function to demote fsck errors to warnings, 2015-06-22) it was only intended to be used to parse user-supplied data. For things that are purely internal to the C code it makes sense to have the compiler check these arguments, and to skip the sanity checking of the data in fsck_set_msg_type() which is redundant to checks we get from the compiler. 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 53692df2b82f9d5ce15779da7d5227f1b027e193
3 files changed +22 -11
builtin/mktag.c
+2 -1
@@ -88,7 +88,8 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
88 die_errno(_("could not read from stdin"));
89
90 fsck_options.error_func = mktag_fsck_error_func;
91 - fsck_set_msg_type(&fsck_options, "extraheaderentry", "warn");
91 + fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
92 + FSCK_WARN);
93 /* config might set fsck.extraHeaderEntry=* again */
94 git_config(git_fsck_config, &fsck_options);
95 if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options,
fsck.c
+17 -10
@@ -132,6 +132,22 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type)
132 return 1;
133 }
134
135 +void fsck_set_msg_type_from_ids(struct fsck_options *options,
136 + enum fsck_msg_id msg_id,
137 + enum fsck_msg_type msg_type)
138 +{
139 + if (!options->msg_type) {
140 + int i;
141 + enum fsck_msg_type *severity;
142 + ALLOC_ARRAY(severity, FSCK_MSG_MAX);
143 + for (i = 0; i < FSCK_MSG_MAX; i++)
144 + severity[i] = fsck_msg_type(i, options);
145 + options->msg_type = severity;
146 + }
147 +
148 + options->msg_type[msg_id] = msg_type;
149 +}
150 +
151 void fsck_set_msg_type(struct fsck_options *options,
152 const char *msg_id_str, const char *msg_type_str)
153 {
@@ -144,16 +160,7 @@ void fsck_set_msg_type(struct fsck_options *options,
160 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
161 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
162
147 - if (!options->msg_type) {
148 - int i;
149 - enum fsck_msg_type *severity;
150 - ALLOC_ARRAY(severity, FSCK_MSG_MAX);
151 - for (i = 0; i < FSCK_MSG_MAX; i++)
152 - severity[i] = fsck_msg_type(i, options);
153 - options->msg_type = severity;
154 - }
155 -
156 - options->msg_type[msg_id] = msg_type;
163 + fsck_set_msg_type_from_ids(options, msg_id, msg_type);
164 }
165
166 void fsck_set_msg_types(struct fsck_options *options, const char *values)
fsck.h
+3
@@ -82,6 +82,9 @@ enum fsck_msg_id {
82 struct fsck_options;
83 struct object;
84
85 +void fsck_set_msg_type_from_ids(struct fsck_options *options,
86 + enum fsck_msg_id msg_id,
87 + enum fsck_msg_type msg_type);
88 void fsck_set_msg_type(struct fsck_options *options,
89 const char *msg_id, const char *msg_type);
90 void fsck_set_msg_types(struct fsck_options *options, const char *values);