Skip to contentSkip to conversation

merge: use skip_prefix to parse config key

PATCH Superseded Martin Ågren 3 participants 6 messages Apr 10, 2020 – Apr 11, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 merge: use skip_prefix to parse config key

Instead of using starts_with(), the magic number 7, strlen() and a fair number of additions to verify the three parts of the config key "branch.<branch>.mergeoptions", use skip_prefix() to jump through them more explicitly.

We need to introduce a new variable for this (we certainly can't modify k just because we see "branch."!). With skip_prefix() we often use quite bland names like p or str. Let's do the same. If and when this function needs to do more prefix-skipping, we'll have a generic variable ready for this.

Adjust the indentation while we're here.

---

worktree.c also matches "strcmp.*strlen" and could see a similar patch. I'm working on a longer series for that file and this fell out of that work. This feels independent enough that I'm posting it on its own.

 builtin/merge.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
builtin/merge.c +3 -3
index d127d2225f..bde5f14f05 100644
@@ -597,10 +597,10 @@ static void parse_branch_merge_options(char *bmo)
 static int git_merge_config(const char *k, const char *v, void *cb)
 {
 	int status;
+	const char *str;
 
-	if (branch && starts_with(k, "branch.") &&
-		starts_with(k + 7, branch) &&
-		!strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
+	if (branch && skip_prefix(k, "branch.", &str) &&
+	    skip_prefix(str, branch, &str) && !strcmp(str, ".mergeoptions")) {
 		free(branch_mergeoptions);
 		branch_mergeoptions = xstrdup(v);
 		return 0;
Signature · 1 line
2.26.0
#1 lore raw
Quoted 4 lines · Martin Ågren

Instead of using starts_with(), the magic number 7, strlen() and a fair number of additions to verify the three parts of the config key "branch.<branch>.mergeoptions", use skip_prefix() to jump through them more explicitly.

The conversion looks correct to me and is certainly an improvement.

Quoted 5 lines

We need to introduce a new variable for this (we certainly can't modify k just because we see "branch."!). With skip_prefix() we often use quite bland names like p or str. Let's do the same. If and when this function needs to do more prefix-skipping, we'll have a generic variable ready for this.

I was about to comment on this in the patch, but your explanation preempted me. :) The logic here makes sense.

Quoted 8 lines
builtin/merge.c +0 -0
index d127d2225f..bde5f14f05 100644
@@ -597,10 +597,10 @@ static void parse_branch_merge_options(char *bmo)
 static int git_merge_config(const char *k, const char *v, void *cb)
 {
 	int status;

builtin/merge.c static void parse_branch_merge_options(char *bmo) · line 606

Quoted 10 lines
builtin/merge.c +3 -3
+	const char *str;
 
-	if (branch && starts_with(k, "branch.") &&
-		starts_with(k + 7, branch) &&
-		!strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
+	if (branch && skip_prefix(k, "branch.", &str) &&
+	    skip_prefix(str, branch, &str) && !strcmp(str, ".mergeoptions")) {
 		free(branch_mergeoptions);
 		branch_mergeoptions = xstrdup(v);
 		return 0;

In general, parsing subsections accurately involves looking from both the start and the end of the string, pulling out the section and key and leaving the rest in the middle. But I think we can get away with this left-to-right parsing because we're only interested in matching a _specific_ subsection name, and a specific key. So there are no cases it will handle incorrectly.

The more general form would be:

const char *subsection, *key;
int subsection_len;

  if (!parse_config_key("branch", &subsection, &subsection_len, &key) &&
      subsection_len == strlen(branch) && !strncmp(subsection, branch) &&
      !strcmp(key, "mergeoptions"))
         ...

but that's a bit more awkward (it would be less so if we had a helper function for comparing a NUL-terminated string against a ptr/len pair).

-Peff

#2 lore raw
Quoted 6 lines · Jeff King

In general, parsing subsections accurately involves looking from both the start and the end of the string, pulling out the section and key and leaving the rest in the middle. But I think we can get away with this left-to-right parsing because we're only interested in matching a _specific_ subsection name, and a specific key. So there are no cases it will handle incorrectly.

In other words, if k were "branch.A.B.mergeoptions", it can only be the 'branch.*.mergeoptions' variable attached to branch "A.B", but when checking for branch=="A", the first two skip_prefix() would pass and the only thing that protects us from misparsing is that "B.mergeoptions" is not what we are looking for.

Quoted 5 lines

The more general form would be:

const char *subsection, *key;
int subsection_len;

Quoted 7 lines
  if (!parse_config_key("branch", &subsection, &subsection_len, &key) &&
      subsection_len == strlen(branch) && !strncmp(subsection, branch) &&
      !strcmp(key, "mergeoptions"))
         ...

but that's a bit more awkward (it would be less so if we had a helper function for comparing a NUL-terminated string against a ptr/len pair).

Yes, but even with such a helper, i.e.

	if (branch &&
	    !parse_config_key("branch", &sub, &sublen, &key) &&
	    !spanstrcmp(sub, sublen, branch) &&
	    !strcmp(key, "mergeoptions"))

what Martin wrote, especially if it is reflowed to match the above, i.e.

	if (branch &&
	    skip_prefix(key, "branch.", &sub) &&
	    skip_prefix(sub, branch, &sub) &&
	    !strcmp(sub, ".mergeoptions")

I find it just as, if not more, easy to read.

Where the parse_config_key() helper shines, I think, is when we do not have the middle level to compare against, and in that case, we must work only from the given key, scanning from both ends for dot.

Thanks.

#3 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Jeff King

In general, parsing subsections accurately involves looking from both the start and the end of the string, pulling out the section and key and leaving the rest in the middle. But I think we can get away with this left-to-right parsing because we're only interested in matching a _specific_ subsection name, and a specific key. So there are no cases it will handle incorrectly.

Quoted 5 lines

In other words, if k were "branch.A.B.mergeoptions", it can only be the 'branch.*.mergeoptions' variable attached to branch "A.B", but when checking for branch=="A", the first two skip_prefix() would pass and the only thing that protects us from misparsing is that "B.mergeoptions" is not what we are looking for.

Yes, exactly.

Quoted 7 lines
Quoted 1 line

The more general form would be:

[...]
Yes, but even with such a helper, i.e.
[...]
what Martin wrote, especially if it is reflowed to match the above, i.e. [...]
I find it just as, if not more, easy to read.

Yeah, sorry if I was unclear; I think the left-to-right is perfectly fine for this case.

Quoted 3 lines

Where the parse_config_key() helper shines, I think, is when we do not have the middle level to compare against, and in that case, we must work only from the given key, scanning from both ends for dot.

Agreed.

Another option for known-value cases like this is to do it outside of the callback handler, like:

  char *key = xstrfmt("branch.%s.mergeoptions");
  const char *value;
  if (!git_config_get_string_const(key, &value))
     ...
  free(key);

The allocation is a bit awkward, though we could hide that with a clever helper.

Shifting from "iterate over and store config keys" to "look up config keys on demand" is a much bigger change, though. I would only do it if it made the rest of the code flow easier.

-Peff

#4 lore raw
Quoted 3 lines · Jeff King

Shifting from "iterate over and store config keys" to "look up config keys on demand" is a much bigger change, though. I would only do it if it made the rest of the code flow easier.

Yeah, absolutely.

Thanks.

#5 lore raw

PATCH v2 1/1 merge: use skip_prefix to parse config key

In reply to Jeff King
Quoted 21 lines · Jeff King
Quoted 7 lines · Junio C Hamano

Jeff King <[email protected]> writes:

In other words, if k were "branch.A.B.mergeoptions", it can only be the 'branch.*.mergeoptions' variable attached to branch "A.B", but when checking for branch=="A", the first two skip_prefix() would pass and the only thing that protects us from misparsing is that "B.mergeoptions" is not what we are looking for.

Yes, exactly.

Quoted 7 lines
Quoted 1 line

The more general form would be:

[...]
Yes, but even with such a helper, i.e.
[...]
what Martin wrote, especially if it is reflowed to match the above, i.e. [...]
I find it just as, if not more, easy to read.

Quoted 2 lines

Yeah, sorry if I was unclear; I think the left-to-right is perfectly fine for this case.

Thanks both for an interesting discussion. Junio left a remark about "[easy to read] especially if it is reflowed" which I recognized from my own thinking yesterday. Then I went for fewer lines in what arguably looks a bit crammed. Since I was not the only one to find readability in the larger number of lines, here it is as v2.

Junio, if you go "meh, what I've already picked up will do just as fine", feel free to act on that, i.e., to not act on this v2.

Quoted 10 lines

Another option for known-value cases like this is to do it outside of the callback handler, like:

  char *key = xstrfmt("branch.%s.mergeoptions");
  const char *value;
  if (!git_config_get_string_const(key, &value))
     ...
  free(key);

The allocation is a bit awkward [...]

I very briefly thought about such an approach here, but still in the callback handler, where the allocation would be quite a bit more awkward, since we'd do it over and over.

Something I considered a bit more seriously was a helper that takes three strings ("branch", branch and "mergeoptions") and does the check. But as you mention in this thread, it's a bit special that we're looking for a specific subsection (that is not a compile-time constant). So it felt like the kind of "nice-to-have" helper that you'd end up using just once or twice. (That said, I didn't look to see if I could find other instances.)

Martin

-- >8 --
Instead of using starts_with(), the magic number 7, strlen() and a fair number of additions to verify the three parts of the config key "branch.<branch>.mergeoptions", use skip_prefix() to jump through them more explicitly.

We need to introduce a new variable for this (we certainly can't modify k just because we see "branch."!). With skip_prefix() we often use quite bland names like p or str. Let's do the same. If and when this function needs to do more prefix-skipping, we'll have a generic variable ready for this.

---
 builtin/merge.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
builtin/merge.c +5 -3
index d127d2225f..df83ba2a80 100644
@@ -597,10 +597,12 @@ static void parse_branch_merge_options(char *bmo)
 static int git_merge_config(const char *k, const char *v, void *cb)
 {
 	int status;
+	const char *str;
 
-	if (branch && starts_with(k, "branch.") &&
-		starts_with(k + 7, branch) &&
-		!strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
+	if (branch &&
+	    skip_prefix(k, "branch.", &str) &&
+	    skip_prefix(str, branch, &str) &&
+	    !strcmp(str, ".mergeoptions")) {
 		free(branch_mergeoptions);
 		branch_mergeoptions = xstrdup(v);
 		return 0;
Signature · 1 line
2.26.0