scalar: move config setting logic into its own function

Create function 'set_scalar_config()' to contain the logic used in setting Scalar-defined Git config settings, including how to handle reconfiguring & overwriting existing values. This function allows future patches to set config values in parts of 'scalar.c' other than 'set_recommended_config()'. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Aug 18, 2022 at 21:40 UTC d934a11c718f35f5593cfb49da994b43e74dd940
1 file changed +28 -16
contrib/scalar/scalar.c
+28 -16
@@ -85,13 +85,33 @@ static int run_git(const char *arg, ...)
85 return res;
86 }
87
88 +struct scalar_config {
89 + const char *key;
90 + const char *value;
91 + int overwrite_on_reconfigure;
92 +};
93 +
94 +static int set_scalar_config(const struct scalar_config *config, int reconfigure)
95 +{
96 + char *value = NULL;
97 + int res;
98 +
99 + if ((reconfigure && config->overwrite_on_reconfigure) ||
100 + git_config_get_string(config->key, &value)) {
101 + trace2_data_string("scalar", the_repository, config->key, "created");
102 + res = git_config_set_gently(config->key, config->value);
103 + } else {
104 + trace2_data_string("scalar", the_repository, config->key, "exists");
105 + res = 0;
106 + }
107 +
108 + free(value);
109 + return res;
110 +}
111 +
112 static int set_recommended_config(int reconfigure)
113 {
90 - struct {
91 - const char *key;
92 - const char *value;
93 - int overwrite_on_reconfigure;
94 - } config[] = {
114 + struct scalar_config config[] = {
115 /* Required */
116 { "am.keepCR", "true", 1 },
117 { "core.FSCache", "true", 1 },
@@ -145,17 +165,9 @@ static int set_recommended_config(int reconfigure)
165 char *value;
166
167 for (i = 0; config[i].key; i++) {
148 - if ((reconfigure && config[i].overwrite_on_reconfigure) ||
149 - git_config_get_string(config[i].key, &value)) {
150 - trace2_data_string("scalar", the_repository, config[i].key, "created");
151 - if (git_config_set_gently(config[i].key,
152 - config[i].value) < 0)
153 - return error(_("could not configure %s=%s"),
154 - config[i].key, config[i].value);
155 - } else {
156 - trace2_data_string("scalar", the_repository, config[i].key, "exists");
157 - free(value);
158 - }
168 + if (set_scalar_config(config + i, reconfigure))
169 + return error(_("could not configure %s=%s"),
170 + config[i].key, config[i].value);
171 }
172
173 /*