public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "Aaron Paterson via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Aaron Paterson <apaterson@pm.me>,
	Aaron Paterson <apaterson@pm.me>
Subject: [PATCH 1/2] odb: add odb_source_files_try() for heterogeneous source iteration
Date: Mon, 16 Mar 2026 15:29:42 +0000	[thread overview]
Message-ID: <e6877b30d5dddce5720a15d724d7b381a5595915.1773674983.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2068.git.1773674983.gitgitgadget@gmail.com>

From: Aaron Paterson <apaterson@pm.me>

The odb_source vtable introduced in this release allows multiple
backend implementations via the odb_source_type enum. However,
source-chain iteration sites that need files-specific internals
(pack store, loose cache, MIDX) currently use
odb_source_files_downcast(), which calls BUG() for non-files
source types. This makes it impossible to add a non-files source
to the chain without crashing.

Add odb_source_files_try() as a companion to the existing
downcast function. It returns NULL for non-files sources instead
of aborting. This follows the pattern used elsewhere in git where
a "try" or "maybe" variant provides a fallback path while the
strict version retains its safety guarantee.

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.

A subsequent commit will convert the source-chain iteration sites
to use this new helper.

Signed-off-by: Aaron Paterson <apaterson@pm.me>
---
 odb/source-files.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/odb/source-files.h b/odb/source-files.h
index 23a3b4e04b..abd093b23f 100644
--- a/odb/source-files.h
+++ b/odb/source-files.h
@@ -32,4 +32,18 @@ static inline struct odb_source_files *odb_source_files_downcast(struct odb_sour
 	return container_of(source, struct odb_source_files, base);
 }
 
+/*
+ * Try to cast the given source to the files backend. Returns NULL if
+ * the source uses a different backend. Use this in loops that iterate
+ * over heterogeneous source chains (e.g. when alternates may include
+ * non-files backends). Use odb_source_files_downcast() when the source
+ * is known to be a files backend.
+ */
+static inline struct odb_source_files *odb_source_files_try(struct odb_source *source)
+{
+	if (source->type != ODB_SOURCE_FILES)
+		return NULL;
+	return container_of(source, struct odb_source_files, base);
+}
+
 #endif
-- 
gitgitgadget


  reply	other threads:[~2026-03-16 15:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-16 15:29 [PATCH 0/2] odb: add odb_source_files_try() for heterogeneous source iteration Aaron Paterson via GitGitGadget
2026-03-16 15:29 ` Aaron Paterson via GitGitGadget [this message]
2026-03-16 15:29 ` [PATCH 2/2] odb: use odb_source_files_try() in source-chain iterations Aaron Paterson via GitGitGadget
2026-03-19 10:05   ` Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e6877b30d5dddce5720a15d724d7b381a5595915.1773674983.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=apaterson@pm.me \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox