git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: "Martin Ågren" <martin.agren@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] merge: use skip_prefix to parse config key
Date: Fri, 10 Apr 2020 11:58:27 -0400	[thread overview]
Message-ID: <20200410155827.GA71011@coredump.intra.peff.net> (raw)
In-Reply-To: <20200410151032.23198-1-martin.agren@gmail.com>

On Fri, Apr 10, 2020 at 05:10:32PM +0200, Martin Ågren wrote:

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

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

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

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

  reply	other threads:[~2020-04-10 15:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-10 15:10 Martin Ågren
2020-04-10 15:58 ` Jeff King [this message]
2020-04-10 16:44   ` Junio C Hamano
2020-04-10 16:56     ` Jeff King
2020-04-10 17:12       ` Junio C Hamano
2020-04-11  7:11       ` [PATCH v2] " Martin Ågren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200410155827.GA71011@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=martin.agren@gmail.com \
    --subject='Re: [PATCH] merge: use skip_prefix to parse config key' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).