Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grep/sparse-integration-v1.3 #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ static pthread_cond_t cond_result;

static int skip_first_line;

static int grep_sparse = 0;

static void add_work(struct grep_opt *opt, struct grep_source *gs)
{
if (opt->binary != GREP_BINARY_TEXT)
Expand Down Expand Up @@ -517,15 +519,23 @@ static int grep_cache(struct grep_opt *opt,
strbuf_addstr(&name, repo->submodule_prefix);
}

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

if (repo_read_index(repo) < 0)
die(_("index file corrupt"));

/* TODO: audit for interaction with sparse-index. */
ensure_full_index(repo->index);
if (grep_sparse)
ensure_full_index(repo->index);

for (nr = 0; nr < repo->index->cache_nr; nr++) {
const struct cache_entry *ce = repo->index->cache[nr];

if (!cached && ce_skip_worktree(ce))
/*
* If ce is a SKIP_WORKTREE entry, look into it when both
* --sparse and --cached are given.
*/
if (!(grep_sparse && cached) && ce_skip_worktree(ce))
continue;

strbuf_setlen(&name, name_base_len);
Expand Down Expand Up @@ -963,6 +973,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
PARSE_OPT_NOCOMPLETE),
OPT_INTEGER('m', "max-count", &opt.max_count,
N_("maximum number of results per file")),
OPT_BOOL(0, "sparse", &grep_sparse,
N_("search sparse contents and expand sparse index")),
OPT_END()
};
grep_prefix = prefix;
Expand Down
17 changes: 17 additions & 0 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1853,4 +1853,21 @@ test_expect_success 'mv directory from out-of-cone to in-cone' '
grep -e "H deep/0/1" actual
'

test_expect_success 'grep expands index using --sparse' '
init_repos &&

# With --sparse and --cached, do not ignore sparse entries and
# expand the index.
test_all_match git grep --sparse --cached a
'

test_expect_success 'grep is not expanded' '
init_repos &&

ensure_not_expanded grep a &&
ensure_not_expanded grep a -- deep/* &&
# grep does not match anything per se, so ! is used
ensure_not_expanded ! grep a -- folder1/*
'

test_done
12 changes: 6 additions & 6 deletions t/t7817-grep-sparse-checkout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ test_expect_success 'grep searches unmerged file despite not matching sparsity p
test_cmp expect actual
'

test_expect_success 'grep --cached searches entries with the SKIP_WORKTREE bit' '
test_expect_success 'grep --cached and --sparse searches entries with the SKIP_WORKTREE bit' '
cat >expect <<-EOF &&
a:text
b:text
dir/c:text
EOF
git grep --cached "text" >actual &&
git grep --cached --sparse "text" >actual &&
test_cmp expect actual
'

Expand All @@ -143,7 +143,7 @@ test_expect_success 'grep --recurse-submodules honors sparse checkout in submodu
test_cmp expect actual
'

test_expect_success 'grep --recurse-submodules --cached searches entries with the SKIP_WORKTREE bit' '
test_expect_success 'grep --recurse-submodules --cached and --sparse searches entries with the SKIP_WORKTREE bit' '
cat >expect <<-EOF &&
a:text
b:text
Expand All @@ -152,7 +152,7 @@ test_expect_success 'grep --recurse-submodules --cached searches entries with th
sub/B/b:text
sub2/a:text
EOF
git grep --recurse-submodules --cached "text" >actual &&
git grep --recurse-submodules --cached --sparse "text" >actual &&
test_cmp expect actual
'

Expand All @@ -166,15 +166,15 @@ test_expect_success 'working tree grep does not search the index with CE_VALID a
test_cmp expect actual
'

test_expect_success 'grep --cached searches index entries with both CE_VALID and SKIP_WORKTREE' '
test_expect_success 'grep --cached and --sparse searches index entries with both CE_VALID and SKIP_WORKTREE' '
cat >expect <<-EOF &&
a:text
b:text
dir/c:text
EOF
test_when_finished "git update-index --no-assume-unchanged b" &&
git update-index --assume-unchanged b &&
git grep --cached text >actual &&
git grep --cached --sparse text >actual &&
test_cmp expect actual
'

Expand Down