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

Re: [PATCH] merge: use skip_prefix to parse config key

From
Jeff King <peff@peff.net>
Date
Apr 10, 2020, 16:56 UTC
Message-ID
<20200410165644.GA79836@coredump.intra.peff.net>
In-Reply-To
<xmqqo8rzjnnb.fsf@gitster.c.googlers.com>
On Fri, Apr 10, 2020 at 09:44:56AM -0700, Junio C Hamano wrote:
Show 14 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > 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.
Yes, exactly.
Show 7 quoted lines
> > 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.

> 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
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 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.