builtin/config: track "fixed value" option via flags only

We track the "fixed value" option via two separate bits: once via the global variable `fixed_value`, and once via the CONFIG_FLAGS_FIXED_VALUE bit in `flags`. This is confusing and may easily lead to issues when one is not aware that this is tracked via two separate mechanisms. Refactor the code to use the flag exclusively. We already pass it to all the required callsites anyway, except for `collect_config()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 15, 2024 at 08:43 UTC ab8bac8bb6d89f3b3eca2972ad468ca863c9f492
1 file changed +7 -7
builtin/config.c
+7 -7
@@ -125,7 +125,6 @@ struct config_display_options {
125
126 static int use_key_regexp;
127 static int do_all;
128 -static int fixed_value;
128
129 #define TYPE_BOOL 1
130 #define TYPE_INT 2
@@ -328,6 +327,7 @@ struct collect_config_data {
327 regex_t *regexp;
328 regex_t *key_regexp;
329 int do_not_match;
330 + unsigned flags;
331 };
332
333 static int collect_config(const char *key_, const char *value_,
@@ -341,7 +341,8 @@ static int collect_config(const char *key_, const char *value_,
341 return 0;
342 if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
343 return 0;
344 - if (fixed_value && strcmp(data->value_pattern, (value_?value_:"")))
344 + if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
345 + strcmp(data->value_pattern, (value_?value_:"")))
346 return 0;
347 if (data->regexp &&
348 (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0)))
@@ -363,6 +364,7 @@ static int get_value(const struct config_location_options *opts,
364 struct collect_config_data data = {
365 .display_opts = display_opts,
366 .values = &values,
367 + .flags = flags,
368 };
369 char *key = NULL;
370 int i;
@@ -1117,6 +1119,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1119 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
1120 const char *comment_arg = NULL;
1121 int actions = 0;
1122 + unsigned flags = 0;
1123 struct option opts[] = {
1124 CONFIG_LOCATION_OPTIONS(location_opts),
1125 OPT_GROUP(N_("Action")),
@@ -1139,13 +1142,12 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1142 OPT_STRING(0, "default", &display_opts.default_value,
1143 N_("value"), N_("with --get, use default value when missing entry")),
1144 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
1142 - OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
1145 + OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
1146 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
1147 N_("respect include directives on lookup")),
1148 OPT_END(),
1149 };
1150 char *value = NULL, *comment = NULL;
1148 - int flags = 0;
1151 int ret = 0;
1152 struct key_value_info default_kvi = KVI_INIT;
1153
@@ -1195,7 +1197,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1197 }
1198
1199 /* check usage of --fixed-value */
1198 - if (fixed_value) {
1200 + if (flags & CONFIG_FLAGS_FIXED_VALUE) {
1201 int allowed_usage = 0;
1202
1203 switch (actions) {
@@ -1226,8 +1228,6 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1228 error(_("--fixed-value only applies with 'value-pattern'"));
1229 exit(129);
1230 }
1229 -
1230 - flags |= CONFIG_FLAGS_FIXED_VALUE;
1231 }
1232
1233 comment = git_config_prepare_comment_string(comment_arg);