git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/3] builtin/grep: read sparse directory trees via passed-in repository

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 3, 2026, 12:43 UTC
Message-ID
<20261103-pks-grep-recurse-submodule-v2-2-b00e3b3dc865@pks.im>
In-Reply-To
<20261103-pks-grep-recurse-submodule-v2-0-b00e3b3dc865@pks.im>

When iterating through the index in `grep_cache()` we may encounter sparse directory entries. In that case we read the tree object they point to so that we can recurse into it via `grep_tree()`. We do so via `the_repository` though, even though `grep_cache()` may be called for a submodule via `grep_submodule()`.

This issue is similar to the one fixed in the preceding commit, where `grep_tree()` didn't use the correct repository. But in this case here the issue cannot be triggered as we call `repo_read_gitmodules()` on the submodule repository, and that function will cause us to read its index. But because we only ever set `command_requires_full_index` for the top-level repository we will convert the submodule's index into a full index, and consequently we will never see sparse directory entries. This is also documented as such in a NEEDSWORK comment in `grep_submodule()`.

Regardless of that the code is still wrong, and it may very easily turn into a bug once we start to handle sparse indices for submodules. Adapt it to use the correct repository to preempt such a future bug.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 22fb365eeac..cb518d8155e 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -560,7 +560,7 @@ static int grep_cache(struct grep_opt *opt,
 			void *data;
 			size_t size;
 
-			data = odb_read_object(the_repository->objects, &ce->oid,
+			data = odb_read_object(repo->objects, &ce->oid,
 					       &type, &size);
 			if (!data)
 				die(_("unable to read tree %s"), oid_to_hex(&ce->oid));
-- 
2.56.0.500.g5d06dc02cd.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 10 in “builtin/grep: fix reading objects from wrong repository”
  1. 0/3 builtin/grep: fix reading objects from wrong repositoryPatrick Steinhardt, Oct 30, 2026
  2. 1/3 builtin/grep: read submodule trees via their repositoryPatrick Steinhardt, Oct 30, 2026
  3. Toon ClaesOct 30, 2026
  4. Patrick SteinhardtNov 3, 2026
  5. 2/3 builtin/grep: read sparse directory trees via passed-in repositoryPatrick Steinhardt, Oct 30, 2026
  6. 3/3 builtin/grep: read ".gitmodules" via passed-in repositoryPatrick Steinhardt, Oct 30, 2026
  7. 0/3 builtin/grep: fix reading objects from wrong repositoryPatrick Steinhardt, Nov 3, 2026
  8. 1/3 builtin/grep: read submodule trees via their repositoryPatrick Steinhardt, Nov 3, 2026
  9. 2/3 builtin/grep: read sparse directory trees via passed-in repositoryPatrick Steinhardt, Nov 3, 2026
  10. 3/3 builtin/grep: read ".gitmodules" via passed-in repositoryPatrick Steinhardt, Nov 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.