Allow consult-grep
to use --include
#1017
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the upstream master, I noticed, given a
conult-grep
input of#gnu -- --include *md#
, the--incude *md
passed to grep by theconsult--grep-make-builder
has no effect in terms of filtering out files which do not match theinclude
.To me, using
--include
to select just the files I want operate on, is the more common use-case.I hypothesised that this is because the arguments are passed to the end of the flag list of grep, and to test this, I put them at the start, and the
--include *md
works as expected.I'm closely looking at this piece of documentation of
man grep
, which it seems to describe the behaviour.It must be the following rule which this PR makes use of:
If no --include or --exclude options match, a file is included unless the first such option is --include
It seems to be true, as something like
grep --exclude *nonexistent --include *md -r -e gnu .
doesn't use the--include
flaggrep --exclude *el --include *md -r -e gnu .
doesgrep --exclude *el --exclude *nonexistent --include *md -r -e gnu .
doesgrep --include *md --exclude *nonexistent -r -e gnu .
doesActually, now I realise, having an
--exclude
before an--include
, changes the meaning of the succeeding--include
entirely.--include
which is before an--exclude
acts as a "filter everything out, except for the --include"--include
after an--exclude
acts as "negate the --exclude with what is in the --include" (it does no filtering)So in the
consult-grep
interface, we pre-pend the user-defined grep flags with an--exclude
, and so the user is limited to the second meaning of the--include
.So either way we decide to insert the user-provided flags, we are compromising a little bit.
This PR contains a little hack to put the user-providing flags at the start .
Is it worth creating a more elegant solution for this?
Maybe there can be an additional
--
separator which adds to the end/start, or there can be a way to expose environment variables which the user can splice consult related flags.