Merge branch 'mt/grep-submodules-working-tree'

"git grep --recurse-submodules" that looks at the working tree
files looked at the contents in the index in submodules, instead of
files in the working tree.

* mt/grep-submodules-working-tree:
  grep: fix worktree case in submodules
This commit is contained in:
Junio C Hamano 2019-08-22 12:34:10 -07:00
commit 307179732d
2 changed files with 27 additions and 4 deletions

View file

@ -403,7 +403,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
static int grep_submodule(struct grep_opt *opt,
const struct pathspec *pathspec,
const struct object_id *oid,
const char *filename, const char *path)
const char *filename, const char *path, int cached)
{
struct repository subrepo;
struct repository *superproject = opt->repo;
@ -475,7 +475,7 @@ static int grep_submodule(struct grep_opt *opt,
strbuf_release(&base);
free(data);
} else {
hit = grep_cache(&subopt, pathspec, 1);
hit = grep_cache(&subopt, pathspec, cached);
}
repo_clear(&subrepo);
@ -523,7 +523,8 @@ static int grep_cache(struct grep_opt *opt,
}
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
hit |= grep_submodule(opt, pathspec, NULL, ce->name, ce->name);
hit |= grep_submodule(opt, pathspec, NULL, ce->name,
ce->name, cached);
} else {
continue;
}
@ -598,7 +599,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
free(data);
} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
hit |= grep_submodule(opt, pathspec, &entry.oid,
base->buf, base->buf + tn_len);
base->buf, base->buf + tn_len,
1); /* ignored */
}
strbuf_setlen(base, old_baselen);

View file

@ -408,4 +408,25 @@ test_expect_success 'grep --recurse-submodules with submodules without .gitmodul
test_cmp expect actual
'
reset_and_clean () {
git reset --hard &&
git clean -fd &&
git submodule foreach --recursive 'git reset --hard' &&
git submodule foreach --recursive 'git clean -fd'
}
test_expect_success 'grep --recurse-submodules without --cached considers worktree modifications' '
reset_and_clean &&
echo "A modified line in submodule" >>submodule/a &&
echo "submodule/a:A modified line in submodule" >expect &&
git grep --recurse-submodules "A modified line in submodule" >actual &&
test_cmp expect actual
'
test_expect_success 'grep --recurse-submodules with --cached ignores worktree modifications' '
reset_and_clean &&
echo "A modified line in submodule" >>submodule/a &&
test_must_fail git grep --recurse-submodules --cached "A modified line in submodule" >actual 2>&1 &&
test_must_be_empty actual
'
test_done