Skip to content

Commit 9c62534

Browse files
pks-tgitster
authored andcommitted
builtin/config: pass data between callbacks via local variables
We use several global variables to pass data between callers and callbacks in `get_color()` and `get_colorbool()`. Convert those to use callback data structures instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35a7cfd commit 9c62534

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

builtin/config.c

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -518,88 +518,102 @@ static char *normalize_value(const char *key, const char *value,
518518
BUG("cannot normalize type %d", type);
519519
}
520520

521-
static int get_color_found;
522-
static const char *get_color_slot;
523-
static const char *get_colorbool_slot;
524-
static char parsed_color[COLOR_MAXLEN];
521+
struct get_color_config_data {
522+
int get_color_found;
523+
const char *get_color_slot;
524+
char parsed_color[COLOR_MAXLEN];
525+
};
525526

526527
static int git_get_color_config(const char *var, const char *value,
527528
const struct config_context *ctx UNUSED,
528-
void *cb UNUSED)
529+
void *cb)
529530
{
530-
if (!strcmp(var, get_color_slot)) {
531+
struct get_color_config_data *data = cb;
532+
533+
if (!strcmp(var, data->get_color_slot)) {
531534
if (!value)
532535
config_error_nonbool(var);
533-
if (color_parse(value, parsed_color) < 0)
536+
if (color_parse(value, data->parsed_color) < 0)
534537
return -1;
535-
get_color_found = 1;
538+
data->get_color_found = 1;
536539
}
537540
return 0;
538541
}
539542

540543
static void get_color(const struct config_location_options *opts,
541544
const char *var, const char *def_color)
542545
{
543-
get_color_slot = var;
544-
get_color_found = 0;
545-
parsed_color[0] = '\0';
546-
config_with_options(git_get_color_config, NULL,
546+
struct get_color_config_data data = {
547+
.get_color_slot = var,
548+
.parsed_color[0] = '\0',
549+
};
550+
551+
config_with_options(git_get_color_config, &data,
547552
&opts->source, the_repository,
548553
&opts->options);
549554

550-
if (!get_color_found && def_color) {
551-
if (color_parse(def_color, parsed_color) < 0)
555+
if (!data.get_color_found && def_color) {
556+
if (color_parse(def_color, data.parsed_color) < 0)
552557
die(_("unable to parse default color value"));
553558
}
554559

555-
fputs(parsed_color, stdout);
560+
fputs(data.parsed_color, stdout);
556561
}
557562

558-
static int get_colorbool_found;
559-
static int get_diff_color_found;
560-
static int get_color_ui_found;
563+
struct get_colorbool_config_data {
564+
int get_colorbool_found;
565+
int get_diff_color_found;
566+
int get_color_ui_found;
567+
const char *get_colorbool_slot;
568+
};
569+
561570
static int git_get_colorbool_config(const char *var, const char *value,
562571
const struct config_context *ctx UNUSED,
563-
void *data UNUSED)
572+
void *cb)
564573
{
565-
if (!strcmp(var, get_colorbool_slot))
566-
get_colorbool_found = git_config_colorbool(var, value);
574+
struct get_colorbool_config_data *data = cb;
575+
576+
if (!strcmp(var, data->get_colorbool_slot))
577+
data->get_colorbool_found = git_config_colorbool(var, value);
567578
else if (!strcmp(var, "diff.color"))
568-
get_diff_color_found = git_config_colorbool(var, value);
579+
data->get_diff_color_found = git_config_colorbool(var, value);
569580
else if (!strcmp(var, "color.ui"))
570-
get_color_ui_found = git_config_colorbool(var, value);
581+
data->get_color_ui_found = git_config_colorbool(var, value);
571582
return 0;
572583
}
573584

574585
static int get_colorbool(const struct config_location_options *opts,
575586
const char *var, int print)
576587
{
577-
get_colorbool_slot = var;
578-
get_colorbool_found = -1;
579-
get_diff_color_found = -1;
580-
get_color_ui_found = -1;
581-
config_with_options(git_get_colorbool_config, NULL,
588+
struct get_colorbool_config_data data = {
589+
.get_colorbool_slot = var,
590+
.get_colorbool_found = -1,
591+
.get_diff_color_found = -1,
592+
.get_color_ui_found = -1,
593+
};
594+
595+
config_with_options(git_get_colorbool_config, &data,
582596
&opts->source, the_repository,
583597
&opts->options);
584598

585-
if (get_colorbool_found < 0) {
586-
if (!strcmp(get_colorbool_slot, "color.diff"))
587-
get_colorbool_found = get_diff_color_found;
588-
if (get_colorbool_found < 0)
589-
get_colorbool_found = get_color_ui_found;
599+
if (data.get_colorbool_found < 0) {
600+
if (!strcmp(data.get_colorbool_slot, "color.diff"))
601+
data.get_colorbool_found = data.get_diff_color_found;
602+
if (data.get_colorbool_found < 0)
603+
data.get_colorbool_found = data.get_color_ui_found;
590604
}
591605

592-
if (get_colorbool_found < 0)
606+
if (data.get_colorbool_found < 0)
593607
/* default value if none found in config */
594-
get_colorbool_found = GIT_COLOR_AUTO;
608+
data.get_colorbool_found = GIT_COLOR_AUTO;
595609

596-
get_colorbool_found = want_color(get_colorbool_found);
610+
data.get_colorbool_found = want_color(data.get_colorbool_found);
597611

598612
if (print) {
599-
printf("%s\n", get_colorbool_found ? "true" : "false");
613+
printf("%s\n", data.get_colorbool_found ? "true" : "false");
600614
return 0;
601615
} else
602-
return get_colorbool_found ? 0 : 1;
616+
return data.get_colorbool_found ? 0 : 1;
603617
}
604618

605619
static void check_write(const struct git_config_source *source)

0 commit comments

Comments
 (0)