Skip to content

Commit 1f9a980

Browse files
committed
Merge branch 'jk/maint-config-alias-fix'
* 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 Conflicts: config.c
2 parents fb674d7 + 73546c0 commit 1f9a980

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,6 @@ typedef int (*config_fn_t)(const char *, const char *, void *);
10401040
extern int git_default_config(const char *, const char *, void *);
10411041
extern int git_config_from_file(config_fn_t fn, const char *, void *);
10421042
extern void git_config_push_parameter(const char *text);
1043-
extern int git_config_parse_parameter(const char *text);
1044-
extern int git_config_parse_environment(void);
10451043
extern int git_config_from_parameters(config_fn_t fn, void *data);
10461044
extern int git_config(config_fn_t fn, void *);
10471045
extern int git_config_early(config_fn_t fn, void *, const char *repo_config);

config.c

Lines changed: 23 additions & 39 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)
@@ -837,20 +825,9 @@ int git_config_system(void)
837825
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
838826
}
839827

840-
int git_config_from_parameters(config_fn_t fn, void *data)
828+
int git_config_global(void)
841829
{
842-
static int loaded_environment;
843-
const struct config_item *ct;
844-
845-
if (!loaded_environment) {
846-
if (git_config_parse_environment() < 0)
847-
return -1;
848-
loaded_environment = 1;
849-
}
850-
for (ct = config_parameters; ct; ct = ct->next)
851-
if (fn(ct->name, ct->value, data) < 0)
852-
return -1;
853-
return 0;
830+
return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
854831
}
855832

856833
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
@@ -882,9 +859,16 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
882859
found += 1;
883860
}
884861

885-
ret += git_config_from_parameters(fn, data);
886-
if (config_parameters)
887-
found += 1;
862+
switch (git_config_from_parameters(fn, data)) {
863+
case -1: /* error */
864+
ret--;
865+
break;
866+
case 0: /* found nothing */
867+
break;
868+
default: /* found at least one item */
869+
found++;
870+
break;
871+
}
888872

889873
return ret == 0 ? found : ret;
890874
}

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];
@@ -122,7 +122,6 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
122122
*envchanged = 1;
123123
(*argv)++;
124124
(*argc)--;
125-
handled++;
126125
} else if (!prefixcmp(cmd, "--git-dir=")) {
127126
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
128127
if (envchanged)
@@ -162,9 +161,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
162161

163162
(*argv)++;
164163
(*argc)--;
165-
handled++;
166164
}
167-
return handled;
165+
return (*argv) - orig_argv;
168166
}
169167

170168
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)