Subject: [PATCH 0/2] odb: add odb_source_files_try() for heterogeneous source iteration

From: "Aaron Paterson via GitGitGadget" <[email-masked]>
Date: Mon, 16 Mar 2026 15:29:41 +0000

The odb_source vtable introduced in this release allows multiple backend
implementations via the odb_source_type enum. However, 22 callsites iterate
the source chain and downcast every source to odb_source_files to access
files-specific internals (pack store, loose cache, MIDX). This prevents
adding any non-files source to the chain without hitting a BUG() assertion.

This two-patch series adds odb_source_files_try() as a companion to the
existing downcast function. It returns NULL for non-files sources instead of
aborting. The 22 source-chain iteration sites are then converted to use this
new helper, skipping non-files sources gracefully.

The existing odb_source_files_downcast() is unchanged and continues to BUG()
on type mismatch, protecting call sites that should only ever receive a
files source (primary source access, vtable callbacks).

This follows the pattern established by the refs subsystem, where
eliminating backend downcasts was a prerequisite for adding the reftable
backend.

Patch 1/2: Add odb_source_files_try() helper (14 lines, 1 file)

Patch 2/2: Convert 22 iteration sites across 11 files + unit test

Known limitation: repo_approximate_object_count() and has_object_pack() will
not account for objects in non-files sources. These are display/optimization
functions. A follow-up series can add vtable callbacks to address this.

Aaron Paterson (2):
  odb: add odb_source_files_try() for heterogeneous source iteration
  odb: use odb_source_files_try() in source-chain iterations

 Makefile                    |  1 +
 builtin/cat-file.c          |  9 ++++++---
 builtin/fast-import.c       |  8 ++++++--
 builtin/grep.c              |  4 +++-
 builtin/pack-objects.c      | 15 +++++++++++----
 commit-graph.c              |  4 +++-
 loose.c                     | 12 +++++++++---
 midx.c                      |  8 ++++++--
 object-file.c               | 20 ++++++++++++++------
 object-name.c               |  8 +++++---
 odb/source-files.h          | 14 ++++++++++++++
 packfile.c                  | 23 +++++++++++++++++------
 packfile.h                  | 14 ++++++++++----
 t/meson.build               |  1 +
 t/unit-tests/u-odb-source.c | 25 +++++++++++++++++++++++++
 15 files changed, 131 insertions(+), 35 deletions(-)
 create mode 100644 t/unit-tests/u-odb-source.c

base-commit: dc6ecd5354dca88d51b6d6562777fc8fc10d77e1
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2068%2FMayCXC%2Fps%2Fodb-source-type-guards-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2068/MayCXC/ps/odb-source-type-guards-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2068
-- 
gitgitgadget