Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Kyle Lippincott <spectral@google.com>,
	Eric Sunshine <sunshine@sunshineco.com>
Subject: [PATCH v2 21/21] builtin/config: pass data between callbacks via local variables
Date: Mon, 13 May 2024 12:23:45 +0200	[thread overview]
Message-ID: <0496b958e24a4d0191821d3f6c8c0d2028f36452.1715595550.git.ps@pks.im> (raw)
In-Reply-To: <cover.1715595550.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 5123 bytes --]

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>
---
 builtin/config.c | 90 ++++++++++++++++++++++++++++--------------------
 1 file changed, 52 insertions(+), 38 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 2ab7d24b62..945a1fd82d 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -517,21 +517,24 @@ static char *normalize_value(const char *key, const char *value,
 	BUG("cannot normalize type %d", type);
 }
 
-static int get_color_found;
-static const char *get_color_slot;
-static const char *get_colorbool_slot;
-static char parsed_color[COLOR_MAXLEN];
+struct get_color_config_data {
+	int get_color_found;
+	const char *get_color_slot;
+	char parsed_color[COLOR_MAXLEN];
+};
 
 static int git_get_color_config(const char *var, const char *value,
 				const struct config_context *ctx UNUSED,
-				void *cb UNUSED)
+				void *cb)
 {
-	if (!strcmp(var, get_color_slot)) {
+	struct get_color_config_data *data = cb;
+
+	if (!strcmp(var, data->get_color_slot)) {
 		if (!value)
 			config_error_nonbool(var);
-		if (color_parse(value, parsed_color) < 0)
+		if (color_parse(value, data->parsed_color) < 0)
 			return -1;
-		get_color_found = 1;
+		data->get_color_found = 1;
 	}
 	return 0;
 }
@@ -539,66 +542,77 @@ static int git_get_color_config(const char *var, const char *value,
 static void get_color(const struct config_location_options *opts,
 		      const char *var, const char *def_color)
 {
-	get_color_slot = var;
-	get_color_found = 0;
-	parsed_color[0] = '\0';
-	config_with_options(git_get_color_config, NULL,
+	struct get_color_config_data data = {
+		.get_color_slot = var,
+		.parsed_color[0] = '\0',
+	};
+
+	config_with_options(git_get_color_config, &data,
 			    &opts->source, the_repository,
 			    &opts->options);
 
-	if (!get_color_found && def_color) {
-		if (color_parse(def_color, parsed_color) < 0)
+	if (!data.get_color_found && def_color) {
+		if (color_parse(def_color, data.parsed_color) < 0)
 			die(_("unable to parse default color value"));
 	}
 
-	fputs(parsed_color, stdout);
+	fputs(data.parsed_color, stdout);
 }
 
-static int get_colorbool_found;
-static int get_diff_color_found;
-static int get_color_ui_found;
+struct get_colorbool_config_data {
+	int get_colorbool_found;
+	int get_diff_color_found;
+	int get_color_ui_found;
+	const char *get_colorbool_slot;
+};
+
 static int git_get_colorbool_config(const char *var, const char *value,
 				    const struct config_context *ctx UNUSED,
-				    void *data UNUSED)
+				    void *cb)
 {
-	if (!strcmp(var, get_colorbool_slot))
-		get_colorbool_found = git_config_colorbool(var, value);
+	struct get_colorbool_config_data *data = cb;
+
+	if (!strcmp(var, data->get_colorbool_slot))
+		data->get_colorbool_found = git_config_colorbool(var, value);
 	else if (!strcmp(var, "diff.color"))
-		get_diff_color_found = git_config_colorbool(var, value);
+		data->get_diff_color_found = git_config_colorbool(var, value);
 	else if (!strcmp(var, "color.ui"))
-		get_color_ui_found = git_config_colorbool(var, value);
+		data->get_color_ui_found = git_config_colorbool(var, value);
 	return 0;
 }
 
 static int get_colorbool(const struct config_location_options *opts,
 			 const char *var, int print)
 {
-	get_colorbool_slot = var;
-	get_colorbool_found = -1;
-	get_diff_color_found = -1;
-	get_color_ui_found = -1;
-	config_with_options(git_get_colorbool_config, NULL,
+	struct get_colorbool_config_data data = {
+		.get_colorbool_slot = var,
+		.get_colorbool_found = -1,
+		.get_diff_color_found = -1,
+		.get_color_ui_found = -1,
+	};
+
+	config_with_options(git_get_colorbool_config, &data,
 			    &opts->source, the_repository,
 			    &opts->options);
 
-	if (get_colorbool_found < 0) {
-		if (!strcmp(get_colorbool_slot, "color.diff"))
-			get_colorbool_found = get_diff_color_found;
-		if (get_colorbool_found < 0)
-			get_colorbool_found = get_color_ui_found;
+	if (data.get_colorbool_found < 0) {
+		if (!strcmp(data.get_colorbool_slot, "color.diff"))
+			data.get_colorbool_found = data.get_diff_color_found;
+		if (data.get_colorbool_found < 0)
+			data.get_colorbool_found = data.get_color_ui_found;
 	}
 
-	if (get_colorbool_found < 0)
+	if (data.get_colorbool_found < 0)
 		/* default value if none found in config */
-		get_colorbool_found = GIT_COLOR_AUTO;
+		data.get_colorbool_found = GIT_COLOR_AUTO;
 
-	get_colorbool_found = want_color(get_colorbool_found);
+	data.get_colorbool_found = want_color(data.get_colorbool_found);
 
 	if (print) {
-		printf("%s\n", get_colorbool_found ? "true" : "false");
+		printf("%s\n", data.get_colorbool_found ? "true" : "false");
 		return 0;
 	} else
-		return get_colorbool_found ? 0 : 1;
+		return data.get_colorbool_found ? 0 : 1;
 }
 
 static void check_write(const struct git_config_source *source)
-- 
2.45.GIT


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2024-05-13 10:23 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10 11:24 [PATCH 00/21] builtin/config: remove global state Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 01/21] builtin/config: stop printing full usage on misuse Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 02/21] builtin/config: move legacy mode into its own function Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 03/21] builtin/config: move subcommand options into `cmd_config()` Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 04/21] builtin/config: move legacy " Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 05/21] builtin/config: move actions into `cmd_config_actions()` Patrick Steinhardt
2024-05-10 20:42   ` Kyle Lippincott
2024-05-13 10:20     ` Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 06/21] builtin/config: check for writeability after source is set up Patrick Steinhardt
2024-05-10 20:46   ` Kyle Lippincott
2024-05-13 10:21     ` Patrick Steinhardt
2024-05-10 11:24 ` [PATCH 07/21] config: make the config source const Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 08/21] builtin/config: refactor functions to have common exit paths Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 09/21] builtin/config: move location options into local variables Patrick Steinhardt
2024-05-10 11:34   ` Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 10/21] builtin/config: move display " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 11/21] builtin/config: move type options into display options Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 12/21] builtin/config: move default value " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 13/21] builtin/config: move `respect_includes_opt` into location options Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 14/21] builtin/config: convert `do_not_match` to a local variable Patrick Steinhardt
2024-05-11 16:42   ` Eric Sunshine
2024-05-10 11:25 ` [PATCH 15/21] builtin/config: convert `value_pattern` " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 16/21] builtin/config: convert `regexp` " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 17/21] builtin/config: convert `key_regexp` " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 18/21] builtin/config: convert `key` " Patrick Steinhardt
2024-05-10 11:25 ` [PATCH 19/21] builtin/config: track "fixed value" option via flags only Patrick Steinhardt
2024-05-11 16:52   ` Eric Sunshine
2024-05-10 11:26 ` [PATCH 20/21] builtin/config: convert flags to a local variable Patrick Steinhardt
2024-05-10 11:26 ` [PATCH 21/21] builtin/config: pass data between callbacks via local variables Patrick Steinhardt
2024-05-10 17:40 ` [PATCH 00/21] builtin/config: remove global state Junio C Hamano
2024-05-10 23:08 ` Kyle Lippincott
2024-05-13 10:21 ` [PATCH v2 " Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 01/21] builtin/config: stop printing full usage on misuse Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 02/21] builtin/config: move legacy mode into its own function Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 03/21] builtin/config: move subcommand options into `cmd_config()` Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 04/21] builtin/config: move legacy " Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 05/21] builtin/config: move actions into `cmd_config_actions()` Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 06/21] builtin/config: check for writeability after source is set up Patrick Steinhardt
2024-05-14 21:45     ` Taylor Blau
2024-05-15  5:58       ` Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 07/21] config: make the config source const Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 08/21] builtin/config: refactor functions to have common exit paths Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 09/21] builtin/config: move location options into local variables Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 10/21] builtin/config: move display " Patrick Steinhardt
2024-05-13 10:22   ` [PATCH v2 11/21] builtin/config: move type options into display options Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 12/21] builtin/config: move default value " Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 13/21] builtin/config: move `respect_includes_opt` into location options Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 14/21] builtin/config: convert `do_not_match` to a local variable Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 15/21] builtin/config: convert `value_pattern` " Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 16/21] builtin/config: convert `regexp` " Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 17/21] builtin/config: convert `key_regexp` " Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 18/21] builtin/config: convert `key` " Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 19/21] builtin/config: track "fixed value" option via flags only Patrick Steinhardt
2024-05-13 10:23   ` [PATCH v2 20/21] builtin/config: convert flags to a local variable Patrick Steinhardt
2024-05-13 10:23   ` Patrick Steinhardt [this message]
2024-05-13 21:57   ` [PATCH v2 00/21] builtin/config: remove global state Kyle Lippincott
2024-05-14 14:48   ` Junio C Hamano
2024-05-14 14:52     ` Patrick Steinhardt
2024-05-15  5:53       ` Patrick Steinhardt
2024-05-15  6:41 ` [PATCH v3 " Patrick Steinhardt
2024-05-15  6:41   ` [PATCH v3 01/21] builtin/config: stop printing full usage on misuse Patrick Steinhardt
2024-05-15  6:41   ` [PATCH v3 02/21] builtin/config: move legacy mode into its own function Patrick Steinhardt
2024-05-15  6:41   ` [PATCH v3 03/21] builtin/config: move subcommand options into `cmd_config()` Patrick Steinhardt
2024-05-15  6:41   ` [PATCH v3 04/21] builtin/config: move legacy " Patrick Steinhardt
2024-05-15  6:41   ` [PATCH v3 05/21] builtin/config: move actions into `cmd_config_actions()` Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 06/21] builtin/config: check for writeability after source is set up Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 07/21] config: make the config source const Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 08/21] builtin/config: refactor functions to have common exit paths Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 09/21] builtin/config: move location options into local variables Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 10/21] builtin/config: move display " Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 11/21] builtin/config: move type options into display options Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 12/21] builtin/config: move default value " Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 13/21] builtin/config: move `respect_includes_opt` into location options Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 14/21] builtin/config: convert `do_not_match` to a local variable Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 15/21] builtin/config: convert `value_pattern` " Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 16/21] builtin/config: convert `regexp` " Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 17/21] builtin/config: convert `key_regexp` " Patrick Steinhardt
2024-05-15  6:42   ` [PATCH v3 18/21] builtin/config: convert `key` " Patrick Steinhardt
2024-05-15  6:43   ` [PATCH v3 19/21] builtin/config: track "fixed value" option via flags only Patrick Steinhardt
2024-05-15  6:43   ` [PATCH v3 20/21] builtin/config: convert flags to a local variable Patrick Steinhardt
2024-05-15  6:43   ` [PATCH v3 21/21] builtin/config: pass data between callbacks via local variables Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0496b958e24a4d0191821d3f6c8c0d2028f36452.1715595550.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=spectral@google.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).