From: Patrick Steinhardt Date: Thu, 10 Sep 2026 15:09:10 GMT Subject: [PATCH v5 0/9] odb: write alternates at creation time Message-ID: <20260910-pks-odb-write-alternates-at-creation-time-v5-0-8d10c4238edc@pks.im> In-Reply-To: <20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im> 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 v5: - Rename `add_one_alternate` and `add_one_alternate_data` to `collect_one_alternate` and `collect_alternates_data` to clarify their intent a bit. - Drop extra parameter in `collect_alternates()`. - Clarify why we compute `commondir` even though it's unused. - Link to v4: https://patch.msgid.link/20260909-pks-odb-write-alternates-at-creation-time-v4-0-d8a78ffc32e4@pks.im Changes in v4: - Add documentation for the different functions that play a role in creating repositories. - Link to v3: https://patch.msgid.link/20260907-pks-odb-write-alternates-at-creation-time-v3-0-735d0b5b3e00@pks.im Changes in v3: - Refactor `init_db()` to not create the reference and object database at all anymore. Instead, it's now called `create_repository()` and it is responsible for creating the initial repository skeleton, only. This allows us to get rid of the flags and overall makes the logic more straight-forward by moving the command-specific logic into the respective commands. - A couple of typo fixes. - Link to v2: https://patch.msgid.link/20260831-pks-odb-write-alternates-at-creation-time-v2-0-aecd2382ba1c@pks.im 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 (9): setup: split up concerns of `init_db()` 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 | 111 +++++++++++++++++++++++--------------- builtin/init-db.c | 32 ++++++++--- 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 | 61 ++++++++------------- setup.h | 47 +++++++++++----- t/t5604-clone-reference.sh | 25 +++++++++ 12 files changed, 264 insertions(+), 222 deletions(-) Range-diff versus v4: 1: 30a1faa807 = 1: e5d789fdfa setup: split up concerns of `init_db()` 2: 7a646ebe92 ! 2: 94a19390b4 builtin/clone: defer setup of the object database @@ Commit message namely alternates. When initializing the object database we do not yet know whether we'll need alternates or not because this depends on the repository we're about to clone from. If it is a local repository and - the user has passed "--refernce{,-if-able}", then we will end up writing - alternates into the object database. + the user has passed "--reference{,-if-able}", then we will end up + writing alternates into the object database. The ugly part though is that we cannot determine where the repository is getting cloned from before it has been initialized. While we of course 3: af77d0e9e3 = 3: e9ccb5b1bd builtin/clone: move around `setup_reference()` 4: 2ca937bce1 ! 4: 30c4933fb0 builtin/clone: refactor handling of "--reference{,-if-able}" @@ Commit message the alternates into it one by one. Prepare for this refactoring by collecting the alternates into a strvec. - Rename the function to `collect_alternates()` to clarify its scope. + Rename the function to `collect_alternates()` to clarify its scope, as + it does not set up the references itself anymore. Signed-off-by: Patrick Steinhardt @@ builtin/clone.c: static char *get_repo_path(const char *repo, int *is_bundle) } -static int add_one_reference(struct string_list_item *item, void *cb_data) -+struct add_one_alternate_data { ++struct collect_alternates_data { + struct strvec *alternates; -+ int required; ++ bool required; +}; + -+static int add_one_alternate(struct string_list_item *item, void *cb_data) ++static int collect_one_alternate(struct string_list_item *item, void *cb_data) { -+ struct add_one_alternate_data *data = cb_data; ++ struct collect_alternates_data *data = cb_data; struct strbuf err = STRBUF_INIT; - int *required = cb_data; char *ref_git = compute_alternate_path(item->string, &err); @@ builtin/clone.c: static void copy_alternates(struct strbuf *src, const char *src - for_each_string_list(&option_optional_reference, - add_one_reference, &required); + if (option_required_reference.nr || option_optional_reference.nr) { -+ struct add_one_alternate_data data = { ++ struct collect_alternates_data data = { + .alternates = alternates, -+ .required = 1, ++ .required = true, + }; + + for_each_string_list(&option_required_reference, -+ add_one_alternate, &data); -+ data.required = 0; ++ collect_one_alternate, &data); ++ data.required = false; + for_each_string_list(&option_optional_reference, -+ add_one_alternate, &data); ++ collect_one_alternate, &data); + } } 5: 4ba60fc24b ! 5: 5141c179e2 builtin/clone: move setup of alternates for shared local clones @@ Commit message Note that the logic for cloning a local repository with "--no-shared" is not yet part of `collect_alternates()`. This will be handled in the next - commit. + commit, but means that at this step, we may compute `commondir` without + it being used. It will become used in the next step though. Signed-off-by: Patrick Steinhardt @@ builtin/clone.c: static void copy_alternates(struct strbuf *src, const char *src -static void collect_alternates(struct strvec *alternates) +static void collect_alternates(struct strvec *alternates, -+ const char *src_repo, bool is_local) ++ const char *local_source_repo) { if (option_required_reference.nr || option_optional_reference.nr) { - struct add_one_alternate_data data = { + struct collect_alternates_data data = { @@ builtin/clone.c: static void collect_alternates(struct strvec *alternates) for_each_string_list(&option_optional_reference, - add_one_alternate, &data); + collect_one_alternate, &data); } + -+ if (is_local) { ++ if (local_source_repo) { + struct strbuf commondir = STRBUF_INIT; + -+ get_common_dir(&commondir, src_repo); ++ get_common_dir(&commondir, local_source_repo); + if (option_shared) + strvec_pushf(alternates, "%s/objects", commondir.buf); + @@ builtin/clone.c: int cmd_clone(int argc, create_object_database(the_repository); - collect_alternates(&alternates); -+ collect_alternates(&alternates, path, is_local); ++ collect_alternates(&alternates, is_local ? path : NULL); for (size_t i = 0; i < alternates.nr; i++) odb_add_to_alternates_file(the_repository->objects, alternates.v[i]); 6: 4f3c8fb936 ! 6: b0ed987d1c builtin/clone: move setup of alternates for non-shared local clones @@ Commit message Signed-off-by: Patrick Steinhardt ## builtin/clone.c ## -@@ builtin/clone.c: static int add_one_alternate(struct string_list_item *item, void *cb_data) +@@ builtin/clone.c: static int collect_one_alternate(struct string_list_item *item, void *cb_data) return 0; } @@ builtin/clone.c: static void copy_alternates(struct strbuf *src, const char *src static void collect_alternates(struct strvec *alternates, @@ builtin/clone.c: static void collect_alternates(struct strvec *alternates, - get_common_dir(&commondir, src_repo); + get_common_dir(&commondir, local_source_repo); if (option_shared) strvec_pushf(alternates, "%s/objects", commondir.buf); + else 7: 47548ac464 = 7: 4cc70ca1dd odb/source: support writing alternates when creating the database 8: 4e3be623dd ! 8: 2cea0312bd builtin/clone: write alternates via `odb_create_on_disk()` @@ builtin/clone.c: int cmd_clone(int argc, warning(_("--local is ignored")); - create_object_database(the_repository); - collect_alternates(&alternates, path, is_local); + collect_alternates(&alternates, is_local ? path : NULL); - - for (size_t i = 0; i < alternates.nr; i++) - odb_add_to_alternates_file(the_repository->objects, alternates.v[i]); 9: 7207b7e2ed = 9: e1f8f728a6 odb/source: remove the ability to write alternates --- base-commit: afa255aeb620346d56a2c01fb5ae9163513c56d7 change-id: 20260813-pks-odb-write-alternates-at-creation-time-64010deb94a0