Skip to content

Commit d934a11

Browse files
vdyegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b24bb9 commit d934a11

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

contrib/scalar/scalar.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,33 @@ static int run_git(const char *arg, ...)
8585
return res;
8686
}
8787

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+
88112
static int set_recommended_config(int reconfigure)
89113
{
90-
struct {
91-
const char *key;
92-
const char *value;
93-
int overwrite_on_reconfigure;
94-
} config[] = {
114+
struct scalar_config config[] = {
95115
/* Required */
96116
{ "am.keepCR", "true", 1 },
97117
{ "core.FSCache", "true", 1 },
@@ -145,17 +165,9 @@ static int set_recommended_config(int reconfigure)
145165
char *value;
146166

147167
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);
159171
}
160172

161173
/*

0 commit comments

Comments
 (0)