Skip to contentSkip to conversation

refs: parse_hide_refs_config to use parse_config_key

PATCH In review Stefan Beller 3 participants 16 messages Feb 24, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 refs: parse_hide_refs_config to use parse_config_key

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.

---

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(-)
refs.c +4 -3
index cd36b64ed9..c9e5f13630 100644
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;
 
 int parse_hide_refs_config(const char *var, const char *value, const char *section)
 {
+	const char *subsection, *key;
+	int subsection_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;
 		int len;
 
Signature · 1 line
2.12.0.rc1.16.ge4278d41a0.dirty
#1 lore raw
Quoted 10 lines · Stefan Beller

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.

Quoted 8 lines

---

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.

Quoted 6 lines
refs.c +0 -0
index cd36b64ed9..c9e5f13630 100644
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;
 

refs.c static struct string_list *hide_refs; · line 1041

Quoted 10 lines
refs.c +4 -3
 int parse_hide_refs_config(const char *var, const char *value, const char *section)
 {
+	const char *subsection, *key;
+	int subsection_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.

#2 lore raw

PATCH 1/1 refs: parse_hide_refs_config to use parse_config_key

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.

---
 refs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
refs.c +4 -3
index cd36b64ed9..21bc8c9101 100644
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;
 
 int parse_hide_refs_config(const char *var, const char *value, const char *section)
 {
+	const char *subsection, *key;
+	int subsection_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;
 		int len;
 
Signature · 1 line
2.12.0.rc1.16.ge4278d41a0.dirty
#3 lore raw
Quoted 9 lines · Stefan Beller

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. [...]

refs.c static struct string_list *hide_refs; · line 1041

Quoted 6 lines
refs.c +2 -3
 	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"))) {

Yeah, this one looks fine.

-Peff

#4 lore raw
In reply to Stefan Beller
Quoted 22 lines · Stefan Beller

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.

---
 refs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
refs.c +0 -0
index cd36b64ed9..21bc8c9101 100644
@@ -1034,10 +1034,11 @@ static struct string_list *hide_refs;
 
 int parse_hide_refs_config(const char *var, const char *value, const char *section)
 {

refs.c static struct string_list *hide_refs; · line 1043

Quoted 10 lines
refs.c +4 -3
+	const char *subsection, *key;
+	int subsection_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;
 		int len;

Thanks for noticing. "once both are merged" is a cryptic comment to leave when somebody knows which two topics make "both" ;-)

#5 lore raw
In reply to Jeff King
Quoted 10 lines · Jeff King

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.

Quoted 6 lines

-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(-)
#6 lore raw

PATCH 1/3 parse_config_key: use skip_prefix instead of starts_with

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()).

---
 config.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
config.c +3 -4
index c6b874a7b..1b08a75a7 100644
@@ -2536,11 +2536,10 @@ int parse_config_key(const char *var,
 		     const char **subsection, int *subsection_len,
 		     const char **key)
 {
-	int section_len = strlen(section);
 	const char *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;
 	}
 
Signature · 1 line
2.12.0.616.g5f622f3b1
#7 lore raw

PATCH 2/3 parse_config_key: allow matching single-level config

In reply to Jeff King

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.

---
 cache.h  | 5 ++++-
 config.c | 8 ++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
cache.h +4 -1
index 61fc86e6d..647a78f3f 100644
@@ -1819,8 +1819,11 @@ extern int git_config_include(const char *name, const char *value, void *data);
  *
  * (i.e., what gets handed to a config_fn_t). The caller provides the section;
  * we return -1 if it does not match, 0 otherwise. The subsection and key
- * out-parameters are filled by the function (and subsection is NULL if it is
+ * out-parameters are filled by the function (and *subsection is NULL if it is
  * missing).
+ *
+ * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
+ * there is no subsection at all.
  */
 extern int parse_config_key(const char *var,
 			    const char *section,
config.c +6 -2
index 1b08a75a7..13c8b21ea 100644
@@ -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;
 	}
Signature · 1 line
2.12.0.616.g5f622f3b1
#8 lore raw

Re: [PATCH 2/3] parse_config_key: allow matching single-level config

Quoted 11 lines · Jeff King

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

Perhaps "only" at the end of the title?

After grepping for call sites of this function, I think we can simplify quite a few instances of:

	if (parse_config_key(...) || !name)
		return ...;
#9 lore raw

Re: [PATCH 2/3] parse_config_key: allow matching single-level config

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Jeff King

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.

Quoted 7 lines
Quoted 5 lines

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

Perhaps "only" at the end of the title?

which still stands. My initial reaction was "eh, I didn't know it was an error to call the function for two-level names".

Quoted 5 lines

After grepping for call sites of this function, I think we can simplify quite a few instances of:

	if (parse_config_key(...) || !name)
		return ...;

This is false. Sorry for the noise.

#10 lore raw

Re: [PATCH 2/3] parse_config_key: allow matching single-level config

In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Jeff King

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.

Quoted 7 lines
Quoted 5 lines

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

Perhaps "only" at the end of the title?

Yeah, that would be an improvement.

Quoted 5 lines

After grepping for call sites of this function, I think we can simplify quite a few instances of:

	if (parse_config_key(...) || !name)
		return ...;

I think you figured this out from your other response, but no, those are the opposite case (it tricked me at first, too).

-Peff

#11 lore raw

PATCH 3/3 parse_hide_refs_config: tell parse_config_key we don't want a subsection

In reply to Jeff King

This lets us avoid declaring some otherwise useless variables.

---
 refs.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
refs.c +3 -4
index 21bc8c910..b9188908b 100644
@@ -1034,11 +1034,10 @@ static struct string_list *hide_refs;
 
 int parse_hide_refs_config(const char *var, const char *value, const char *section)
 {
-	const char *subsection, *key;
-	int subsection_len;
+	const char *key;
 	if (!strcmp("transfer.hiderefs", var) ||
-	    (!parse_config_key(var, section, &subsection, &subsection_len, &key)
-	    && !subsection && !strcmp(key, "hiderefs"))) {
+	    (!parse_config_key(var, section, NULL, NULL, &key) &&
+	     !strcmp(key, "hiderefs"))) {
 		char *ref;
 		int len;
 
Signature · 1 line
2.12.0.616.g5f622f3b1
#12 lore raw

Re: [PATCH 3/3] parse_hide_refs_config: tell parse_config_key we don't want a subsection

refs.c static struct string_list *hide_refs; · line 1040

Quoted 2 lines · Jeff King
refs.c +2 -0
+           (!parse_config_key(var, section, NULL, NULL, &key) &&
+            !strcmp(key, "hiderefs"))) {

Heh, see how fast my code gets replaced with even better code? ;) All three patches look good,

Thanks,
Stefan

#13 lore raw
In reply to Jeff King
Quoted 19 lines · Jeff King
Quoted 10 lines · Jeff King

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.

Quoted 6 lines

-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.

Quoted 6 lines

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

While you were doing that, I was grepping the call sites for parse_config_key() and made sure that all of them are OK when fed two level names. Most of them follow this pattern:

	if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
		return -1;

and ones that do not immediately check !name does either eventually do so or have separate codepaths for handlihng two- and three-level names.

#14 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 6 lines

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

While you were doing that, I was grepping the call sites for parse_config_key() and made sure that all of them are OK when fed two level names. Most of them follow this pattern:

Quoted 6 lines
	if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
		return -1;

and ones that do not immediately check !name does either eventually do so or have separate codepaths for handlihng two- and three-level names.

Yeah, I did that, too. :)

I don't think there are any other sites to convert. And I don't think we can make the "!name" case easier (because some call-sites do want to handle both types). And it's not like it gets much easier anyway (unlike the opposite case, you _do_ need to declare the extra variables.

-Peff

#15 lore raw
Quoted 20 lines · Jeff King
Quoted 11 lines · Junio C Hamano
Quoted 6 lines

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

While you were doing that, I was grepping the call sites for parse_config_key() and made sure that all of them are OK when fed two level names. Most of them follow this pattern:

Quoted 6 lines
	if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
		return -1;

and ones that do not immediately check !name does either eventually do so or have separate codepaths for handlihng two- and three-level names.

Quoted 6 lines

Yeah, I did that, too. :)

I don't think there are any other sites to convert. And I don't think we can make the "!name" case easier (because some call-sites do want to handle both types). And it's not like it gets much easier anyway (unlike the opposite case, you _do_ need to declare the extra variables.

Yeah, because the rejection of !name as an error is not the only reason these call sites have &name and &namelen---they want to use that subsection name after that if() statement rejects malformed input, so we cannot really lose them.

Thanks. All three looked good.