Skip to content

Commit fa6d374

Browse files
committed
Merge branch 'jk/fix-alias-pager-config-key-warnings' into maint
Because the configuration system does not allow "alias.0foo" and "pager.0foo" as the configuration key, the user cannot use '0foo' as a custom command name anyway, but "git 0foo" tried to look these keys up and emitted useless warnings before saying '0foo is not a git command'. These warning messages have been squelched. * jk/fix-alias-pager-config-key-warnings: config: silence warnings for command names with invalid keys
2 parents 0b2cef2 + 9e9de18 commit fa6d374

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

alias.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ char *alias_lookup(const char *alias)
55
char *v = NULL;
66
struct strbuf key = STRBUF_INIT;
77
strbuf_addf(&key, "alias.%s", alias);
8-
git_config_get_string(key.buf, &v);
8+
if (git_config_key_is_valid(key.buf))
9+
git_config_get_string(key.buf, &v);
910
strbuf_release(&key);
1011
return v;
1112
}

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,7 @@ extern int git_config_pathname(const char **, const char *, const char *);
14461446
extern int git_config_set_in_file(const char *, const char *, const char *);
14471447
extern int git_config_set(const char *, const char *);
14481448
extern int git_config_parse_key(const char *, char **, int *);
1449+
extern int git_config_key_is_valid(const char *key);
14491450
extern int git_config_set_multivar(const char *, const char *, const char *, int);
14501451
extern int git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
14511452
extern int git_config_rename_section(const char *, const char *);

config.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@ int git_config_set(const char *key, const char *value)
18481848
* baselen - pointer to int which will hold the length of the
18491849
* section + subsection part, can be NULL
18501850
*/
1851-
int git_config_parse_key(const char *key, char **store_key, int *baselen_)
1851+
static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
18521852
{
18531853
int i, dot, baselen;
18541854
const char *last_dot = strrchr(key, '.');
@@ -1859,12 +1859,14 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
18591859
*/
18601860

18611861
if (last_dot == NULL || last_dot == key) {
1862-
error("key does not contain a section: %s", key);
1862+
if (!quiet)
1863+
error("key does not contain a section: %s", key);
18631864
return -CONFIG_NO_SECTION_OR_NAME;
18641865
}
18651866

18661867
if (!last_dot[1]) {
1867-
error("key does not contain variable name: %s", key);
1868+
if (!quiet)
1869+
error("key does not contain variable name: %s", key);
18681870
return -CONFIG_NO_SECTION_OR_NAME;
18691871
}
18701872

@@ -1875,7 +1877,8 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
18751877
/*
18761878
* Validate the key and while at it, lower case it for matching.
18771879
*/
1878-
*store_key = xmalloc(strlen(key) + 1);
1880+
if (store_key)
1881+
*store_key = xmalloc(strlen(key) + 1);
18791882

18801883
dot = 0;
18811884
for (i = 0; key[i]; i++) {
@@ -1886,26 +1889,42 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
18861889
if (!dot || i > baselen) {
18871890
if (!iskeychar(c) ||
18881891
(i == baselen + 1 && !isalpha(c))) {
1889-
error("invalid key: %s", key);
1892+
if (!quiet)
1893+
error("invalid key: %s", key);
18901894
goto out_free_ret_1;
18911895
}
18921896
c = tolower(c);
18931897
} else if (c == '\n') {
1894-
error("invalid key (newline): %s", key);
1898+
if (!quiet)
1899+
error("invalid key (newline): %s", key);
18951900
goto out_free_ret_1;
18961901
}
1897-
(*store_key)[i] = c;
1902+
if (store_key)
1903+
(*store_key)[i] = c;
18981904
}
1899-
(*store_key)[i] = 0;
1905+
if (store_key)
1906+
(*store_key)[i] = 0;
19001907

19011908
return 0;
19021909

19031910
out_free_ret_1:
1904-
free(*store_key);
1905-
*store_key = NULL;
1911+
if (store_key) {
1912+
free(*store_key);
1913+
*store_key = NULL;
1914+
}
19061915
return -CONFIG_INVALID_KEY;
19071916
}
19081917

1918+
int git_config_parse_key(const char *key, char **store_key, int *baselen)
1919+
{
1920+
return git_config_parse_key_1(key, store_key, baselen, 0);
1921+
}
1922+
1923+
int git_config_key_is_valid(const char *key)
1924+
{
1925+
return !git_config_parse_key_1(key, NULL, NULL, 1);
1926+
}
1927+
19091928
/*
19101929
* If value==NULL, unset in (remove from) config,
19111930
* if value_regex!=NULL, disregard key/value pairs where value does not match.

pager.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ int check_pager_config(const char *cmd)
150150
struct strbuf key = STRBUF_INIT;
151151
const char *value = NULL;
152152
strbuf_addf(&key, "pager.%s", cmd);
153-
if (!git_config_get_value(key.buf, &value)) {
153+
if (git_config_key_is_valid(key.buf) &&
154+
!git_config_get_value(key.buf, &value)) {
154155
int b = git_config_maybe_bool(key.buf, value);
155156
if (b >= 0)
156157
want = b;

t/t7006-pager.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,13 @@ test_expect_success TTY 'external command pagers override sub-commands' '
447447
test_cmp expect actual
448448
'
449449

450+
test_expect_success 'command with underscores does not complain' '
451+
write_script git-under_score <<-\EOF &&
452+
echo ok
453+
EOF
454+
git --exec-path=. under_score >actual 2>&1 &&
455+
echo ok >expect &&
456+
test_cmp expect actual
457+
'
458+
450459
test_done

0 commit comments

Comments
 (0)