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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
May 15, 2024 at 08:43 UTC
9c62534377f988f36b7565a83a276d5b9099951c
1 file changed
+52
-38
builtin/config.c
+52
-38
@@ -518,21 +518,24 @@ static char *normalize_value(const char *key, const char *value,
518
BUG("cannot normalize type %d", type);
519
}
520
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
+};
526
527
static int git_get_color_config(const char *var, const char *value,
528
const struct config_context *ctx UNUSED,
528
- void *cb UNUSED)
529
+ void *cb)
530
{
530
- if (!strcmp(var, get_color_slot)) {
531
+ struct get_color_config_data *data = cb;
532
+
533
+ if (!strcmp(var, data->get_color_slot)) {
534
if (!value)
535
config_error_nonbool(var);
533
- if (color_parse(value, parsed_color) < 0)
536
+ if (color_parse(value, data->parsed_color) < 0)
537
return -1;
535
- get_color_found = 1;
538
+ data->get_color_found = 1;
539
}
540
return 0;
541
}
@@ -540,66 +543,77 @@ static int git_get_color_config(const char *var, const char *value,
543
static void get_color(const struct config_location_options *opts,
544
const char *var, const char *def_color)
545
{
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,
552
&opts->source, the_repository,
553
&opts->options);
554
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)
557
die(_("unable to parse default color value"));
558
}
559
555
- fputs(parsed_color, stdout);
560
+ fputs(data.parsed_color, stdout);
561
}
562
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
+
570
static int git_get_colorbool_config(const char *var, const char *value,
571
const struct config_context *ctx UNUSED,
563
- void *data UNUSED)
572
+ void *cb)
573
{
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);
578
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);
580
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);
582
return 0;
583
}
584
585
static int get_colorbool(const struct config_location_options *opts,
586
const char *var, int print)
587
{
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,
596
&opts->source, the_repository,
597
&opts->options);
598
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;
604
}
605
592
- if (get_colorbool_found < 0)
606
+ if (data.get_colorbool_found < 0)
607
/* default value if none found in config */
594
- get_colorbool_found = GIT_COLOR_AUTO;
608
+ data.get_colorbool_found = GIT_COLOR_AUTO;
609
596
- get_colorbool_found = want_color(get_colorbool_found);
610
+ data.get_colorbool_found = want_color(data.get_colorbool_found);
611
612
if (print) {
599
- printf("%s\n", get_colorbool_found ? "true" : "false");
613
+ printf("%s\n", data.get_colorbool_found ? "true" : "false");
614
return 0;
615
} else
602
- return get_colorbool_found ? 0 : 1;
616
+ return data.get_colorbool_found ? 0 : 1;
617
}
618
619
static void check_write(const struct git_config_source *source)