Skip to content

Commit 06eb708

Browse files
peffgitster
authored andcommitted
config: always parse GIT_CONFIG_PARAMETERS during git_config
Previously we parsed GIT_CONFIG_PARAMETERS lazily into a linked list, and then checked that list during future invocations of git_config. However, that ignores the fact that the environment variable could change during our run (e.g., because we parse more "-c" as part of an alias). Instead, let's just re-parse the environment variable each time. It's generally not very big, and it's no more work than parsing the config files, anyway. As a bonus, we can ditch all of the linked list storage code entirely, making the code much simpler. The test unfortunately still does not pass because of an unrelated bug in handle_options. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a0c9ee commit 06eb708

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

config.c

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ static int zlib_compression_seen;
2020

2121
const char *config_exclusive_filename = NULL;
2222

23-
struct config_item
24-
{
25-
struct config_item *next;
26-
char *name;
27-
char *value;
28-
};
29-
static struct config_item *config_parameters;
30-
static struct config_item **config_parameters_tail = &config_parameters;
31-
3223
static void lowercase(char *p)
3324
{
3425
for (; *p; p++)
@@ -48,9 +39,9 @@ void git_config_push_parameter(const char *text)
4839
strbuf_release(&env);
4940
}
5041

51-
static int git_config_parse_parameter(const char *text)
42+
static int git_config_parse_parameter(const char *text,
43+
config_fn_t fn, void *data)
5244
{
53-
struct config_item *ct;
5445
struct strbuf tmp = STRBUF_INIT;
5546
struct strbuf **pair;
5647
strbuf_addstr(&tmp, text);
@@ -60,22 +51,19 @@ static int git_config_parse_parameter(const char *text)
6051
strbuf_trim(pair[0]);
6152
if (!pair[0]->len) {
6253
strbuf_list_free(pair);
63-
return -1;
54+
return error("bogus config parameter: %s", text);
6455
}
65-
ct = xcalloc(1, sizeof(struct config_item));
66-
ct->name = strbuf_detach(pair[0], NULL);
67-
if (pair[1]) {
68-
strbuf_trim(pair[1]);
69-
ct->value = strbuf_detach(pair[1], NULL);
56+
lowercase(pair[0]->buf);
57+
if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
58+
strbuf_list_free(pair);
59+
return -1;
7060
}
7161
strbuf_list_free(pair);
72-
lowercase(ct->name);
73-
*config_parameters_tail = ct;
74-
config_parameters_tail = &ct->next;
7562
return 0;
7663
}
7764

78-
static int git_config_parse_environment(void) {
65+
int git_config_from_parameters(config_fn_t fn, void *data)
66+
{
7967
const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
8068
char *envw;
8169
const char **argv = NULL;
@@ -93,8 +81,7 @@ static int git_config_parse_environment(void) {
9381
}
9482

9583
for (i = 0; i < nr; i++) {
96-
if (git_config_parse_parameter(argv[i]) < 0) {
97-
error("bogus config parameter: %s", argv[i]);
84+
if (git_config_parse_parameter(argv[i], fn, data) < 0) {
9885
free(argv);
9986
free(envw);
10087
return -1;
@@ -103,7 +90,7 @@ static int git_config_parse_environment(void) {
10390

10491
free(argv);
10592
free(envw);
106-
return 0;
93+
return nr > 0;
10794
}
10895

10996
static int get_next_char(void)
@@ -819,22 +806,6 @@ int git_config_global(void)
819806
return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
820807
}
821808

822-
int git_config_from_parameters(config_fn_t fn, void *data)
823-
{
824-
static int loaded_environment;
825-
const struct config_item *ct;
826-
827-
if (!loaded_environment) {
828-
if (git_config_parse_environment() < 0)
829-
return -1;
830-
loaded_environment = 1;
831-
}
832-
for (ct = config_parameters; ct; ct = ct->next)
833-
if (fn(ct->name, ct->value, data) < 0)
834-
return -1;
835-
return config_parameters != NULL;
836-
}
837-
838809
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
839810
{
840811
int ret = 0, found = 0;

t/t1300-repo-config.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,4 +854,11 @@ test_expect_success 'git -c "key=value" support' '
854854
test_must_fail git -c core.name=value config name
855855
'
856856

857+
test_expect_failure 'git -c works with aliases of builtins' '
858+
git config alias.checkconfig "-c foo.check=bar config foo.check" &&
859+
echo bar >expect &&
860+
git checkconfig >actual &&
861+
test_cmp expect actual
862+
'
863+
857864
test_done

0 commit comments

Comments
 (0)