git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Jacob Keller <jacob.e.keller@intel.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	 Jacob Keller <jacob.keller@gmail.com>
Subject: Re: [PATCH v4 3/3] diff --no-index: support limiting by pathspec
Date: Tue, 23 Sep 2025 16:57:18 +0200 (CEST)	[thread overview]
Message-ID: <c75ec5f9-407a-6555-d4fb-bb629d54ec61@gmx.de> (raw)
In-Reply-To: <20250521232917.2333291-4-jacob.e.keller@intel.com>

Hi Jacob,

I know this is about a patch that you contributed four months ago, and
the usual feedback required sweeping changes, including this one that was
introduced in v4:

On Wed, 21 May 2025, Jacob Keller wrote:

> diff --git a/diff-no-index.c b/diff-no-index.c
> index 9739b2b268b9..4aeeb98cfa8f 100644
> --- a/diff-no-index.c
> +++ b/diff-no-index.c
> @@ -15,20 +15,45 @@
>  #include "gettext.h"
>  #include "revision.h"
>  #include "parse-options.h"
> +#include "pathspec.h"
>  #include "string-list.h"
>  #include "dir.h"
>  
> -static int read_directory_contents(const char *path, struct string_list *list)
> +static int read_directory_contents(const char *path, struct string_list *list,
> +				   const struct pathspec *pathspec,
> +				   int skip)
>  {
> +	struct strbuf match = STRBUF_INIT;
> +	int len;
>  	DIR *dir;
>  	struct dirent *e;
>  
>  	if (!(dir = opendir(path)))
>  		return error("Could not open directory %s", path);
>  
> -	while ((e = readdir_skip_dot_and_dotdot(dir)))
> -		string_list_insert(list, e->d_name);
> +	if (pathspec) {
> +		strbuf_addstr(&match, path);
> +		strbuf_complete(&match, '/');
> +		strbuf_remove(&match, 0, skip);

Okay, so here the `read_directory_contents()` function learns to
optionally skip `skip` bytes from the `path` variable, after potentially
appending a `/`.

>  [...]
> @@ -337,7 +369,23 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
>  		paths[i] = p;
>  	}
>  
> -	fixup_paths(paths, &replacement);
> +	if (fixup_paths(paths, &replacement)) {
> +		parse_pathspec(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_ATTR,
> +			       PATHSPEC_PREFER_FULL | PATHSPEC_NO_REPOSITORY,
> +			       NULL, &argv[2]);
> +		if (pathspec.nr)
> +			ps = &pathspec;
> +
> +		skip1 = strlen(paths[0]);
> +		skip1 += paths[0][skip1] == '/' ? 0 : 1;

Since `skip1` is defined as the length of `path[0]`, I would expect
`paths[0][skip1]` to always evaluate to NUL, and therefore the `== '/'`
condition to always evaluate to `false`. Did I miss anything?

> +		skip2 = strlen(paths[1]);
> +		skip2 += paths[1][skip2] == '/' ? 0 : 1;

Same here, `paths[1][skip2]` should always return `NUL`.

This has ramifications where `skip1` and `skip2` are each one larger than
the length of `paths[0]` and `paths[1]`, respectively, and hence the code
in `read_directory_contents()` will now try to remove one more than the
length of the path, after potentially appending a slash.

But what if there is already a slash? The answer is:

  $ git diff --no-index -- /tmp/ /tmp/ ':!'
  fatal: `pos + len' is too far after the end of the buffer

This has been reported (with Windows paths, don't let that distract) in
https://github.com/git-for-windows/git/issues/5836.

I _think_ that what the patch should have done instead was:

	if (skip1 > 0 && paths[0][skip1 - 1] == '/')
		skip1--;

and likewise

	if (skip2 > 0 && paths[1][skip2 - 1] == '/')
		skip2--;

Focusing on the lines' correctness (which I don't think was the primary
concern in the review of your patch), that would be what I would suggest.

However, this makes me wonder whether the logic itself is sound? It is not
immediately obvious to me why the `paths[0]` and `paths[1]` values aren't
matched against the pathspec yet their entirety is seemingly skipped in
`read_directory_contents()`?

Ciao,
Johannes

  parent reply	other threads:[~2025-09-23 14:57 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-21 23:29 [PATCH v4 0/3] diff: add pathspec support to --no-index Jacob Keller
2025-05-21 23:29 ` [PATCH v4 1/3] pathspec: add match_leading_pathspec variant Jacob Keller
2025-05-21 23:29 ` [PATCH v4 2/3] pathspec: add flag to indicate operation without repository Jacob Keller
2025-05-21 23:29 ` [PATCH v4 3/3] diff --no-index: support limiting by pathspec Jacob Keller
2025-06-04  2:37   ` Ben Knoble
2025-06-04 17:22     ` Jacob Keller
2025-06-04 18:27     ` Jacob Keller
2025-06-04 20:19       ` Junio C Hamano
2025-06-04 21:05         ` Jacob Keller
2025-06-04 21:36           ` D. Ben Knoble
2025-06-04 23:22             ` Junio C Hamano
2025-09-23 14:57   ` Johannes Schindelin [this message]
2025-09-23 22:48     ` Jacob Keller
2025-09-24 11:19       ` Johannes Schindelin
2025-09-24 18:19         ` Jacob Keller
2025-09-24 18:23           ` Jacob Keller
2025-05-22 21:37 ` [PATCH v4 0/3] diff: add pathspec support to --no-index Junio C Hamano
2025-05-22 21:50   ` Jacob Keller
2025-05-22 22:04     ` Junio C Hamano
2025-06-03 21:12 ` Junio C Hamano
2025-06-04  2:32   ` Ben Knoble
2025-06-05 15:34   ` Phillip Wood

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

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

  git send-email \
    --in-reply-to=c75ec5f9-407a-6555-d4fb-bb629d54ec61@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jacob.e.keller@intel.com \
    --cc=jacob.keller@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).