[PATCH v2 0/8] odb: write alternates at creation time
From: Patrick Steinhardt <hidden>
Date: 2026-08-31 10:02:18
Hi,
writing alternates into the object database currently happens via
`odb_source_write_alternate()`. But while that creates the ability to
create alternates at arbitrary points of a source's lifetime, we don't
use that functionality in the first place. Instead, we only ever write
alternates when creating a new repository.
This design is suboptimal due to a couple of reasons:
- It requires us to have a `write_alternates()` callback, which is
overblown as we never even write alternates to an object database
after it has been created.
- We're about to make alternates an implementation detail of the
object database's backend in a future patch series, so alternate
implementations may not even support them.
- The backend has more flexibility with how exactly alternates are
configured when it itself is in full control over their setup at the
time where it creates the object database itself.
This patch series thus refactors how we handle alternates so that we
don't write them ad-hoc anymore. Instead, the series introduces a new
option for `odb_source_create_on_disk()` that makes it handle those
alternates at creation time.
This is part of the bigger goal of moving handling of alternates into
the "files" backend.
This series is built on top of 2c3adbb2c4 (The 18th batch, 2026-08-24)
with ps/odb-eagerly-load-alternates at 0076dc9f81 (odb: drop
`alternates_db` field, 2026-08-17) merged into it.
Changes in v2:
- Use a lockfile to write "info/alternates" during creation time.
- Remove useless "strvec.h" include by reordering declarations a bit.
- Link to v1: https://patch.msgid.link/20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im
Thanks!
Patrick
---
Patrick Steinhardt (8):
builtin/clone: defer setup of the object database
builtin/clone: move around `setup_reference()`
builtin/clone: refactor handling of "--reference{,-if-able}"
builtin/clone: move setup of alternates for shared local clones
builtin/clone: move setup of alternates for non-shared local clones
odb/source: support writing alternates when creating the database
builtin/clone: write alternates via `odb_create_on_disk()`
odb/source: remove the ability to write alternates
builtin/clone.c | 108 +++++++++++++++++++++++--------------
odb.c | 9 ----
odb.h | 7 ---
odb/source-files.c | 130 ++++++++++++++++++++++++++-------------------
odb/source-inmemory.c | 7 ---
odb/source-loose.c | 7 ---
odb/source-packed.c | 7 ---
odb/source.h | 43 +++++----------
setup.c | 15 ++++--
setup.h | 15 ++++--
t/t5604-clone-reference.sh | 25 +++++++++
11 files changed, 203 insertions(+), 170 deletions(-)
Range-diff versus v1:
1: 85b01b6b1b = 1: ca4f283e20 builtin/clone: defer setup of the object database
2: 1e131d0a84 = 2: 4849b826ed builtin/clone: move around `setup_reference()`
3: a6d33bf077 = 3: 870b994a9d builtin/clone: refactor handling of "--reference{,-if-able}"
4: fd88a949f5 = 4: 6268e14ecb builtin/clone: move setup of alternates for shared local clones
5: e18f135daa = 5: c148c65508 builtin/clone: move setup of alternates for non-shared local clones
6: e0733d5d63 ! 6: 360b8e83db odb/source: support writing alternates when creating the database
@@ odb/source-files.c: static void odb_source_files_close(struct odb_source *source
+static int odb_source_files_create_on_disk(struct odb_source *source,
+ const struct odb_create_on_disk_options *opts)
{
++ struct lock_file alternates_lock = LOCK_INIT;
struct strbuf path = STRBUF_INIT;
+ struct strset seen = STRSET_INIT;
+ struct strbuf line = STRBUF_INIT;
-+ FILE *f = NULL;
+ int ret;
safe_create_dir(source->odb->repo, source->path, 1);
@@ odb/source-files.c: static int odb_source_files_create_on_disk(struct odb_source
safe_create_dir(source->odb->repo, path.buf, 1);
+ if (opts->alternates && opts->alternates->nr) {
++ FILE *alternates, *orig;
++
+ strbuf_reset(&path);
+ strbuf_addf(&path, "%s/info/alternates", source->path);
+
++ repo_hold_lock_file_for_update(source->odb->repo, &alternates_lock,
++ path.buf, LOCK_DIE_ON_ERROR);
++
++ alternates = fdopen_lock_file(&alternates_lock, "w");
++ if (!alternates) {
++ ret = error_errno(_("unable to fdopen alternates lockfile"));
++ goto out;
++ }
++
+ /*
+ * The alternates file may already exist, e.g. when it has been
+ * seeded from a template directory. Read any preexisting
+ * entries so that we don't end up writing duplicates.
+ */
-+ f = fopen(path.buf, "r");
-+ if (f) {
-+ while (strbuf_getline(&line, f) != EOF)
++ orig = fopen(path.buf, "r");
++ if (orig) {
++ while (strbuf_getline(&line, orig) != EOF) {
+ strset_add(&seen, line.buf);
++ fprintf(alternates, "%s\n", line.buf);
++ }
+
-+ if (ferror(f)) {
++ if (ferror(orig)) {
+ ret = error_errno(_("unable to read alternates file"));
++ fclose(orig);
+ goto out;
+ }
+
-+ fclose(f);
++ fclose(orig);
+ } else if (errno != ENOENT) {
+ ret = error_errno(_("unable to read alternates file"));
+ goto out;
+ }
+
-+ f = fopen(path.buf, "a");
-+ if (!f) {
-+ ret = error_errno(_("unable to open alternates file for writing"));
-+ goto out;
-+ }
-+
+ for (size_t i = 0; i < opts->alternates->nr; i++) {
+ const char *alternate = opts->alternates->v[i];
-+
+ if (!strset_add(&seen, alternate))
+ continue;
-+
-+ fprintf(f, "%s\n", alternate);
++ fprintf(alternates, "%s\n", alternate);
+ }
+
-+ if (ferror(f)) {
++ if (ferror(alternates)) {
+ ret = error_errno(_("unable to write alternates file"));
+ goto out;
+ }
+
-+ ret = fclose(f);
-+ f = NULL;
-+ if (ret) {
-+ ret = error_errno(_("unable to write alternates file"));
++ if (commit_lock_file(&alternates_lock)) {
++ ret = error_errno(_("unable to commit alternates file"));
+ goto out;
+ }
+ }
@@ odb/source-files.c: static int odb_source_files_create_on_disk(struct odb_source
+ ret = 0;
+
+out:
-+ if (f)
-+ fclose(f);
++ rollback_lock_file(&alternates_lock);
+ strbuf_release(&line);
strbuf_release(&path);
- return 0;
@@ odb/source-files.c: static int odb_source_files_create_on_disk(struct odb_source
static void odb_source_files_prepare(struct odb_source *source,
## odb/source.h ##
-@@
- #include "object.h"
- #include "odb.h"
- #include "odb/transaction.h"
-+#include "strvec.h"
-
- enum odb_source_type {
- /*
-@@ odb/source.h: enum odb_source_type {
- ODB_SOURCE_INMEMORY,
- };
+@@ odb/source.h: struct object_id;
+ struct odb_stream;
+ struct strvec;
+struct odb_create_on_disk_options {
+ /*
@@ odb/source.h: enum odb_source_type {
+};
+
/*
- * Convert between the enum and its name. Returns the equivalent of "unknown"
- * for unknown types.
+ * The source is the part of the object database that stores the actual
+ * objects. It thus encapsulates the logic to read and write the specific
@@ odb/source.h: struct odb_source {
* This callback may be NULL in case the source does not need any
* on-disk setup.
7: 24ae5658ea = 7: 2809430e28 builtin/clone: write alternates via `odb_create_on_disk()`
8: 3bda635fb8 ! 8: 2abcd3b53c odb/source: remove the ability to write alternates
@@ odb.h: int odb_mkstemp(struct object_database *odb,
* recursive alternates it points to), but do not modify the on-disk alternates
## odb/source-files.c ##
-@@
- #include "chdir-notify.h"
- #include "config.h"
- #include "gettext.h"
--#include "lockfile.h"
- #include "object-file.h"
- #include "odb.h"
- #include "odb/source.h"
@@ odb/source-files.c: static int odb_source_files_read_alternates(struct odb_source *source,
return 0;
}
---
base-commit: afa255aeb620346d56a2c01fb5ae9163513c56d7
change-id: 20260813-pks-odb-write-alternates-at-creation-time-64010deb94a0