All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jacob Keller <jacob.e.keller@intel.com>
Cc: <git@vger.kernel.org>,  Jacob Keller <jacob.keller@gmail.com>
Subject: Re: [PATCH v3 3/4] pathspec: add flag to indicate operation without repository
Date: Tue, 20 May 2025 08:13:35 -0700	[thread overview]
Message-ID: <xmqqwmabjoww.fsf@gitster.g> (raw)
In-Reply-To: <20250520000125.2162144-4-jacob.e.keller@intel.com> (Jacob Keller's message of "Mon, 19 May 2025 17:01:24 -0700")

Jacob Keller <jacob.e.keller@intel.com> writes:

> From: Jacob Keller <jacob.keller@gmail.com>
>
> A following change will add support for pathspecs to the git diff
> --no-index command. This mode of git diff does not load any repository.
>
> Add a new PATHSPEC_NO_REPOSITORY flag indicating that we're parsing
> pathspecs without a repository.
>
> Both PATHSPEC_ATTR and PATHSPEC_FROMTOP require a repository to
> function. Thus, verify that both of these are set in magic_mask to
> ensure they won't be accepted when PATHSPEC_NO_REPOSITORY is set.
>
> Check PATHSPEC_NO_REPOSITORY when warning about paths outside the
> directory tree. When the flag is set, do not look for a git repository
> when generating the warning message.
>
> Finally, add a BUG in match_pathspec_item if the istate is NULL but the
> pathspec has PATHSPEC_ATTR set. Callers which support PATHSPEC_ATTR
> should always pass a valid istate, and callers which don't pass a valid
> istate should have set PATHSPEC_ATTR in the magic_mask field to disable
> support for attribute-based pathspecs.

All very sensible considerations.

> diff --git a/dir.c b/dir.c
> index 2f2b654b0252..45aac0bfacab 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -396,9 +396,12 @@ static int match_pathspec_item(struct index_state *istate,
>  	    strncmp(item->match, name - prefix, item->prefix))
>  		return 0;
>  
> -	if (item->attr_match_nr &&
> -	    !match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
> -		return 0;
> +	if (item->attr_match_nr) {
> +		if (!istate)
> +			BUG("magic PATHSPEC_ATTR requires an index");
> +		if (!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
> +			return 0;
> +	}

It is a bit curious why we do not check PATHSPEC_NO_REPOSITORY here,
but it is OK, because it is a BUG for istate to be NULL when we have
a repository anyway.

> diff --git a/pathspec.c b/pathspec.c
> index 2b4e434bc0aa..a3ddd701c740 100644
> --- a/pathspec.c
> +++ b/pathspec.c
> @@ -492,7 +492,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
>  		if (!match) {
>  			const char *hint_path;
>  
> -			if (!have_git_dir())
> +			if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
>  				die(_("'%s' is outside the directory tree"),
>  				    copyfrom);
>  			hint_path = repo_get_work_tree(the_repository);

This is a part of generating an error message.  We die early to
avoid having to call get-work-tree when we know we are not even in
any working tree, which makes sense.

> @@ -614,6 +614,10 @@ void parse_pathspec(struct pathspec *pathspec,
>  	    (flags & PATHSPEC_PREFER_FULL))
>  		BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
>  
> +	if ((flags & PATHSPEC_NO_REPOSITORY) &&
> +	    (~magic_mask & (PATHSPEC_ATTR | PATHSPEC_FROMTOP)))
> +		BUG("PATHSPEC_NO_REPOSITORY is incompatible with PATHSPEC_ATTR and PATHSPEC_FROMTOP");

Hmph, I am not sure if this change is correct.  The magic_mask
parameter is passed by a caller to say "even if parsr_pathspec()
parses a pathspec using a certain set of features properly, the
caller is not prepared to handle the parsed result".  If magic_mask
lacks PATHSPEC_ATTR, that does not necessarily mean that the given
pathspec contains any pathspec items that do use the attr magic.  It
merely says that the caller is not prepared to handle a pathspec
item that uses the attr magic feature.

If we are going to add a call to parse_pathspec() in a code path
that is specific to diff-no-index, isn't it sufficient to pass
PATHSPEC_ATTR and PATHSPEC_FROMTOP as magic_mask without this
change?



  reply	other threads:[~2025-05-20 15:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-20  0:01 [PATCH v3 0/4] diff: add pathspec support to --no-index Jacob Keller
2025-05-20  0:01 ` [PATCH v3 1/4] prefix_path: support prefixes not ending in trailing slash Jacob Keller
2025-05-20 14:35   ` Junio C Hamano
2025-05-20 22:34     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 2/4] pathspec: expose match_pathspec_with_flags Jacob Keller
2025-05-20 14:39   ` Junio C Hamano
2025-05-20 22:38     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 3/4] pathspec: add flag to indicate operation without repository Jacob Keller
2025-05-20 15:13   ` Junio C Hamano [this message]
2025-05-20 22:42     ` Jacob Keller
2025-05-21 23:05     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 4/4] diff --no-index: support limiting by pathspec Jacob Keller
2025-05-20 16:30   ` Junio C Hamano
2025-05-20 22:45     ` Jacob Keller
2025-05-20 22:47     ` Jacob Keller

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=xmqqwmabjoww.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.