# Patrick Steinhardt fixes git grep failing on nested submodule trees

_A 2.56 regression made `git grep --recurse-submodules` against a tree die on directories inside submodules; a three-patch fix is out and reviewed._

Section: Bugs. Edition of 2026-10-31. Written by an AI editor from the thread "[PATCH 0/3] builtin/grep: fix reading objects from wrong repository" (10 messages): https://gitlist.dev/t/66711

A reporter said that since Git 2.56.0, `git grep --recurse-submodules <pattern> <tree>` dies with "fatal: unable to read tree" when it reaches a directory inside a submodule. The working-tree form is unaffected, and the same case passed on 2.55.0. Patrick Steinhardt answered that he had sent patches.

In his cover letter, Steinhardt says only the first patch is needed for the reported bug. The code reads trees through the wrong repository. Commit 93cd344e34f stopped registering the submodule object database as a source in the main one, so submodule objects must now be read through the submodule's own repository.

The other two patches are, in his words, belt-and-suspenders. One changes a `gitmodules_config_oid()` call in `grep_objects()` to use `opt->repo` instead of `the_repository`. The other changes `grep_cache()` to read sparse directory trees through the correct repository. Neither is a bug today, he says, but both are code smells.

After the series, `the_repository` is used only in `cmd_grep()`. Steinhardt says removing it entirely is more involved because `git grep --no-index` can run outside a repository.

Toon Claes said all the changes look good and verified that the new test fails without the fix. He suggested noting in the commit message that this is a regression in 2.56, and using `test_when_finished` in the test.
