git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] merge: use skip_prefix to parse config key

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Apr 10, 2020, 15:10 UTC
Message-ID
<20200410151032.23198-1-martin.agren@gmail.com>

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.
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 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(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index d127d2225f..bde5f14f05 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -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;
-- 
2.26.0
Next: Jeff King
Message 1 of 6 in “merge: use skip_prefix to parse config key”
  1. merge: use skip_prefix to parse config keyMartin Ågren, Apr 10, 2020
  2. Jeff KingApr 10, 2020
  3. Junio C HamanoApr 10, 2020
  4. Jeff KingApr 10, 2020
  5. Junio C HamanoApr 10, 2020
  6. merge: use skip_prefix to parse config keyMartin Ågren, Apr 11, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.