From: Stefan Beller <hidden> Date: 2017-02-24 20:43:20
parse_config_key was introduced in 1b86bbb0ade (config: add helper
function for parsing key names, 2013-01-22), the NEEDSWORK that is removed
in this patch was introduced at daebaa7813 (upload/receive-pack: allow
hiding ref hierarchies, 2013-01-18), which is only a couple days apart,
so presumably the code replaced in this patch was only introduced due
to not wanting to wait on the proper helper function being available.
Make the condition easier to read by using parse_config_key.
Signed-off-by: Stefan Beller <redacted>
---
When investigating the state of the art for parsing config options, I saw
opportunity for a small drive-by patch in an area that I did not look at for
a long time.
The authors of the two mentioned commits are Jeff and Junio, so maybe you
remember another reason for this NEEDSWORK here?
Thanks,
Stefan
refs.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;intparse_hide_refs_config(constchar*var,constchar*value,constchar*section){+constchar*subsection,*key;+intsubsection_len;if(!strcmp("transfer.hiderefs",var)||-/* NEEDSWORK: use parse_config_key() once both are merged */-(starts_with(var,section)&&var[strlen(section)]=='.'&&-!strcmp(var+strlen(section),".hiderefs"))){+(!parse_config_key(var,section,&subsection,&subsection_len,&key)+&&!strcmp(key,"hiderefs"))){char*ref;intlen;
From: Jeff King <hidden> Date: 2017-02-24 20:39:48
On Fri, Feb 24, 2017 at 12:33:35PM -0800, Stefan Beller wrote:
parse_config_key was introduced in 1b86bbb0ade (config: add helper
function for parsing key names, 2013-01-22), the NEEDSWORK that is removed
in this patch was introduced at daebaa7813 (upload/receive-pack: allow
hiding ref hierarchies, 2013-01-18), which is only a couple days apart,
so presumably the code replaced in this patch was only introduced due
to not wanting to wait on the proper helper function being available.
Make the condition easier to read by using parse_config_key.
Signed-off-by: Stefan Beller <redacted>
---
When investigating the state of the art for parsing config options, I saw
opportunity for a small drive-by patch in an area that I did not look at for
a long time.
The authors of the two mentioned commits are Jeff and Junio, so maybe you
remember another reason for this NEEDSWORK here?
No, I think the reasoning you gave in the commit message is exactly
what happened.
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;intparse_hide_refs_config(constchar*var,constchar*value,constchar*section){+constchar*subsection,*key;+intsubsection_len;if(!strcmp("transfer.hiderefs",var)||-/* NEEDSWORK: use parse_config_key() once both are merged */-(starts_with(var,section)&&var[strlen(section)]=='.'&&-!strcmp(var+strlen(section),".hiderefs"))){+(!parse_config_key(var,section,&subsection,&subsection_len,&key)+&&!strcmp(key,"hiderefs"))){
This will start parsing "receive.foobar.hiderefs", which we don't want.
I think you need:
!parse_config_key(var, section, &subsection, &subsection_len, &key) &&
!subsection &&
!strcmp(key, "hiderefs")
Perhaps passing NULL for the subsection variable should cause
parse_config_key to return failure when there is a non-empty subsection.
-Peff
PS Outside of parse_config_key, this code would be nicer if it used
skip_prefix() instead of starts_with(). Since it's going away, I
don't think it matters, but I note that parse_config_key could
probably benefit from the same.
From: Stefan Beller <hidden> Date: 2017-02-24 20:43:42
parse_config_key was introduced in 1b86bbb0ade (config: add helper
function for parsing key names, 2013-01-22), the NEEDSWORK that is removed
in this patch was introduced at daebaa7813 (upload/receive-pack: allow
hiding ref hierarchies, 2013-01-18), which is only a couple days apart,
so presumably the code replaced in this patch was only introduced due
to not wanting to wait on the proper helper function being available.
Make the condition easier to read by using parse_config_key.
Signed-off-by: Stefan Beller <redacted>
---
refs.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;intparse_hide_refs_config(constchar*var,constchar*value,constchar*section){+constchar*subsection,*key;+intsubsection_len;if(!strcmp("transfer.hiderefs",var)||-/* NEEDSWORK: use parse_config_key() once both are merged */-(starts_with(var,section)&&var[strlen(section)]=='.'&&-!strcmp(var+strlen(section),".hiderefs"))){+(!parse_config_key(var,section,&subsection,&subsection_len,&key)+&&!subsection&&!strcmp(key,"hiderefs"))){char*ref;intlen;
From: Jeff King <hidden> Date: 2017-02-24 20:47:32
On Fri, Feb 24, 2017 at 12:43:35PM -0800, Stefan Beller wrote:
parse_config_key was introduced in 1b86bbb0ade (config: add helper
function for parsing key names, 2013-01-22), the NEEDSWORK that is removed
in this patch was introduced at daebaa7813 (upload/receive-pack: allow
hiding ref hierarchies, 2013-01-18), which is only a couple days apart,
so presumably the code replaced in this patch was only introduced due
to not wanting to wait on the proper helper function being available.
Make the condition easier to read by using parse_config_key.
[...]
if (!strcmp("transfer.hiderefs", var) ||
- /* NEEDSWORK: use parse_config_key() once both are merged */
- (starts_with(var, section) && var[strlen(section)] == '.' &&
- !strcmp(var + strlen(section), ".hiderefs"))) {
+ (!parse_config_key(var, section, &subsection, &subsection_len, &key)
+ && !subsection && !strcmp(key, "hiderefs"))) {
From: Jeff King <hidden> Date: 2017-02-24 21:13:42
On Fri, Feb 24, 2017 at 03:39:40PM -0500, Jeff King wrote:
This will start parsing "receive.foobar.hiderefs", which we don't want.
I think you need:
!parse_config_key(var, section, &subsection, &subsection_len, &key) &&
!subsection &&
!strcmp(key, "hiderefs")
Perhaps passing NULL for the subsection variable should cause
parse_config_key to return failure when there is a non-empty subsection.
-Peff
PS Outside of parse_config_key, this code would be nicer if it used
skip_prefix() instead of starts_with(). Since it's going away, I
don't think it matters, but I note that parse_config_key could
probably benefit from the same.
While I'm thinking about it, here are patches to do that. The third one
I'd probably squash into yours (after ordering it to the end).
[1/3]: parse_config_key: use skip_prefix instead of starts_with
[2/3]: parse_config_key: allow matching single-level config
[3/3]: parse_hide_refs_config: tell parse_config_key we don't want a subsection
cache.h | 5 ++++-
config.c | 15 +++++++++------
refs.c | 7 +++----
3 files changed, 16 insertions(+), 11 deletions(-)
From: Jeff King <hidden> Date: 2017-02-24 21:14:08
This saves us having to repeatedly add in "section_len" (and
also avoids walking over the first part of the string
multiple times for a strlen() and strrchr()).
Signed-off-by: Jeff King <redacted>
---
config.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
@@ -2536,11 +2536,10 @@ int parse_config_key(const char *var,constchar**subsection,int*subsection_len,constchar**key){-intsection_len=strlen(section);constchar*dot;/* Does it start with "section." ? */-if(!starts_with(var,section)||var[section_len]!='.')+if(!skip_prefix(var,section,&var)||*var!='.')return-1;/*
@@ -2552,12 +2551,12 @@ int parse_config_key(const char *var,*key=dot+1;/* Did we have a subsection at all? */-if(dot==var+section_len){+if(dot==var){*subsection=NULL;*subsection_len=0;}else{-*subsection=var+section_len+1;+*subsection=var+1;*subsection_len=dot-*subsection;}
From: Jeff King <hidden> Date: 2017-02-24 21:14:49
The parse_config_key() function was introduced to make it
easier to match "section.subsection.key" variables. It also
handles the simpler "section.key", and the caller is
responsible for distinguishing the two from its
out-parameters.
Most callers who _only_ want "section.key" would just use a
strcmp(var, "section.key"), since there is no parsing
required. However, they may still use parse_config_key() if
their "section" variable isn't a constant (an example of
this is in parse_hide_refs_config).
Using the parse_config_key is a bit clunky, though:
const char *subsection;
int subsection_len;
const char *key;
if (!parse_config_key(var, section, &subsection, &subsection_len, &key) &&
!subsection) {
/* matched! */
}
Instead, let's treat a NULL subsection as an indication that
the caller does not expect one. That lets us write:
const char *key;
if (!parse_config_key(var, section, NULL, NULL, &key)) {
/* matched! */
}
Existing callers should be unaffected, as passing a NULL
subsection would currently segfault.
Signed-off-by: Jeff King <redacted>
---
cache.h | 5 ++++-
config.c | 8 ++++++--
2 files changed, 10 insertions(+), 3 deletions(-)
@@ -2552,10 +2552,14 @@ int parse_config_key(const char *var,/* Did we have a subsection at all? */if(dot==var){-*subsection=NULL;-*subsection_len=0;+if(subsection){+*subsection=NULL;+*subsection_len=0;+}}else{+if(!subsection)+return-1;*subsection=var+1;*subsection_len=dot-*subsection;}
From: Jeff King <hidden> Date: 2017-02-24 21:15:04
This lets us avoid declaring some otherwise useless
variables.
Signed-off-by: Jeff King <redacted>
---
refs.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)