Skip to content

Commit 1c6e351

Browse files
committed
Merge branch 'jk/maint-config-alias-fix' into maint
* jk/maint-config-alias-fix: handle_options(): do not miscount how many arguments were used config: always parse GIT_CONFIG_PARAMETERS during git_config git_config: don't peek at global config_parameters config: make environment parsing routines static
2 parents 6e1a751 + 73546c0 commit 1c6e351

File tree

4 files changed

+30
-48
lines changed

4 files changed

+30
-48
lines changed

cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,6 @@ typedef int (*config_fn_t)(const char *, const char *, void *);
10071007
extern int git_default_config(const char *, const char *, void *);
10081008
extern int git_config_from_file(config_fn_t fn, const char *, void *);
10091009
extern void git_config_push_parameter(const char *text);
1010-
extern int git_config_parse_parameter(const char *text);
1011-
extern int git_config_parse_environment(void);
10121010
extern int git_config_from_parameters(config_fn_t fn, void *data);
10131011
extern int git_config(config_fn_t fn, void *);
10141012
extern int git_config_early(config_fn_t fn, void *, const char *repo_config);

config.c

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ static int zlib_compression_seen;
2020

2121
const char *config_exclusive_filename = NULL;
2222

23-
struct config_item {
24-
struct config_item *next;
25-
char *name;
26-
char *value;
27-
};
28-
static struct config_item *config_parameters;
29-
static struct config_item **config_parameters_tail = &config_parameters;
30-
3123
static void lowercase(char *p)
3224
{
3325
for (; *p; p++)
@@ -47,9 +39,9 @@ void git_config_push_parameter(const char *text)
4739
strbuf_release(&env);
4840
}
4941

50-
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)
5144
{
52-
struct config_item *ct;
5345
struct strbuf tmp = STRBUF_INIT;
5446
struct strbuf **pair;
5547
strbuf_addstr(&tmp, text);
@@ -59,22 +51,19 @@ int git_config_parse_parameter(const char *text)
5951
strbuf_trim(pair[0]);
6052
if (!pair[0]->len) {
6153
strbuf_list_free(pair);
62-
return -1;
54+
return error("bogus config parameter: %s", text);
6355
}
64-
ct = xcalloc(1, sizeof(struct config_item));
65-
ct->name = strbuf_detach(pair[0], NULL);
66-
if (pair[1]) {
67-
strbuf_trim(pair[1]);
68-
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;
6960
}
7061
strbuf_list_free(pair);
71-
lowercase(ct->name);
72-
*config_parameters_tail = ct;
73-
config_parameters_tail = &ct->next;
7462
return 0;
7563
}
7664

77-
int git_config_parse_environment(void) {
65+
int git_config_from_parameters(config_fn_t fn, void *data)
66+
{
7867
const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
7968
char *envw;
8069
const char **argv = NULL;
@@ -92,8 +81,7 @@ int git_config_parse_environment(void) {
9281
}
9382

9483
for (i = 0; i < nr; i++) {
95-
if (git_config_parse_parameter(argv[i]) < 0) {
96-
error("bogus config parameter: %s", argv[i]);
84+
if (git_config_parse_parameter(argv[i], fn, data) < 0) {
9785
free(argv);
9886
free(envw);
9987
return -1;
@@ -102,7 +90,7 @@ int git_config_parse_environment(void) {
10290

10391
free(argv);
10492
free(envw);
105-
return 0;
93+
return nr > 0;
10694
}
10795

10896
static int get_next_char(void)
@@ -839,22 +827,6 @@ int git_config_system(void)
839827
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
840828
}
841829

842-
int git_config_from_parameters(config_fn_t fn, void *data)
843-
{
844-
static int loaded_environment;
845-
const struct config_item *ct;
846-
847-
if (!loaded_environment) {
848-
if (git_config_parse_environment() < 0)
849-
return -1;
850-
loaded_environment = 1;
851-
}
852-
for (ct = config_parameters; ct; ct = ct->next)
853-
if (fn(ct->name, ct->value, data) < 0)
854-
return -1;
855-
return 0;
856-
}
857-
858830
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
859831
{
860832
int ret = 0, found = 0;
@@ -884,9 +856,16 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
884856
found += 1;
885857
}
886858

887-
ret += git_config_from_parameters(fn, data);
888-
if (config_parameters)
889-
found += 1;
859+
switch (git_config_from_parameters(fn, data)) {
860+
case -1: /* error */
861+
ret--;
862+
break;
863+
case 0: /* found nothing */
864+
break;
865+
default: /* found at least one item */
866+
found++;
867+
break;
868+
}
890869

891870
return ret == 0 ? found : ret;
892871
}

git.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void commit_pager_choice(void) {
6666

6767
static int handle_options(const char ***argv, int *argc, int *envchanged)
6868
{
69-
int handled = 0;
69+
const char **orig_argv = *argv;
7070

7171
while (*argc > 0) {
7272
const char *cmd = (*argv)[0];
@@ -116,7 +116,6 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
116116
*envchanged = 1;
117117
(*argv)++;
118118
(*argc)--;
119-
handled++;
120119
} else if (!prefixcmp(cmd, "--git-dir=")) {
121120
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
122121
if (envchanged)
@@ -156,9 +155,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
156155

157156
(*argv)++;
158157
(*argc)--;
159-
handled++;
160158
}
161-
return handled;
159+
return (*argv) - orig_argv;
162160
}
163161

164162
static int handle_alias(int *argcp, const char ***argv)

t/t1300-repo-config.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,11 @@ test_expect_success 'key sanity-checking' '
897897
git config foo."ba =z".bar false
898898
'
899899

900+
test_expect_success 'git -c works with aliases of builtins' '
901+
git config alias.checkconfig "-c foo.check=bar config foo.check" &&
902+
echo bar >expect &&
903+
git checkconfig >actual &&
904+
test_cmp expect actual
905+
'
906+
900907
test_done

0 commit comments

Comments
 (0)