Skip to content

Commit 3071797

Browse files
committed
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
2 parents 5fa0f52 + 6a289d4 commit 3071797

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

builtin/grep.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
403403
static int grep_submodule(struct grep_opt *opt,
404404
const struct pathspec *pathspec,
405405
const struct object_id *oid,
406-
const char *filename, const char *path)
406+
const char *filename, const char *path, int cached)
407407
{
408408
struct repository subrepo;
409409
struct repository *superproject = opt->repo;
@@ -475,7 +475,7 @@ static int grep_submodule(struct grep_opt *opt,
475475
strbuf_release(&base);
476476
free(data);
477477
} else {
478-
hit = grep_cache(&subopt, pathspec, 1);
478+
hit = grep_cache(&subopt, pathspec, cached);
479479
}
480480

481481
repo_clear(&subrepo);
@@ -523,7 +523,8 @@ static int grep_cache(struct grep_opt *opt,
523523
}
524524
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
525525
submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
526-
hit |= grep_submodule(opt, pathspec, NULL, ce->name, ce->name);
526+
hit |= grep_submodule(opt, pathspec, NULL, ce->name,
527+
ce->name, cached);
527528
} else {
528529
continue;
529530
}
@@ -598,7 +599,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
598599
free(data);
599600
} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
600601
hit |= grep_submodule(opt, pathspec, &entry.oid,
601-
base->buf, base->buf + tn_len);
602+
base->buf, base->buf + tn_len,
603+
1); /* ignored */
602604
}
603605

604606
strbuf_setlen(base, old_baselen);

t/t7814-grep-recurse-submodules.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,25 @@ test_expect_success 'grep --recurse-submodules with submodules without .gitmodul
408408
test_cmp expect actual
409409
'
410410

411+
reset_and_clean () {
412+
git reset --hard &&
413+
git clean -fd &&
414+
git submodule foreach --recursive 'git reset --hard' &&
415+
git submodule foreach --recursive 'git clean -fd'
416+
}
417+
418+
test_expect_success 'grep --recurse-submodules without --cached considers worktree modifications' '
419+
reset_and_clean &&
420+
echo "A modified line in submodule" >>submodule/a &&
421+
echo "submodule/a:A modified line in submodule" >expect &&
422+
git grep --recurse-submodules "A modified line in submodule" >actual &&
423+
test_cmp expect actual
424+
'
425+
426+
test_expect_success 'grep --recurse-submodules with --cached ignores worktree modifications' '
427+
reset_and_clean &&
428+
echo "A modified line in submodule" >>submodule/a &&
429+
test_must_fail git grep --recurse-submodules --cached "A modified line in submodule" >actual 2>&1 &&
430+
test_must_be_empty actual
431+
'
411432
test_done

0 commit comments

Comments
 (0)