grep: honor sparse-checkout on working tree searches
commit42d906bec48c95d66cb669ee41ce04330f74a98b
authorMatheus Tavares <matheus.bernardino@usp.br>
Tue, 9 Feb 2021 21:33:30 +0000 (9 18:33 -0300)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Feb 2021 07:10:51 +0000 (9 23:10 -0800)
tree8b2006eeeaaab7b380b94cae9be9cde2e7619277
parent773e25afc41b1b6533fa9ae2cd825d0b4a697fad
grep: honor sparse-checkout on working tree searches

On a sparse checked out repository, `git grep` (without --cached) ends
up searching the cache when an entry matches the search pathspec and has
the SKIP_WORKTREE bit set. This is confusing both because the sparse
paths are not expected to be in a working tree search (as they are not
checked out), and because the output mixes working tree and cache
results without distinguishing them. (Note that grep also resorts to the
cache on working tree searches that include --assume-unchanged paths.
But the whole point in that case is to assume that the contents of the
index entry and the file are the same. This does not apply to the case
of sparse paths, where the file isn't even expected to be present.)

Fix that by teaching grep to honor the sparse-checkout rules for working
tree searches. If the user wants to grep paths outside the current
sparse-checkout definition, they may either update the sparsity rules to
materialize the files, or use --cached to search all blobs registered in
the index.

Note: it might also be interesting to add a configuration option that
allow users to search paths that are present despite having the
SKIP_WORKTREE bit set, and/or to restrict searches in the index and past
revisions too. These ideas are left as future improvements to avoid
conflicting with other sparse-checkout topics currently in flight.

Suggested-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
Reviewed-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/grep.c
t/t7011-skip-worktree-reading.sh
t/t7817-grep-sparse-checkout.sh [new file with mode: 0755]