builtin/clone: write alternates via `odb_create_on_disk()`

When creating a repository with alternates we first initialize the
object database and then write alternates to it in a separate step. This
is unfortunate due to a couple of reasons:

  - It requires us to have a `write_alternates()` callback, which is
    unfortunate 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 having this
    callback is suboptimal there.

  - 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.

We have thus introduced the ability to write alternates right at
creation time in the preceding commits, and we have unified setup of
alternates into a single location. All that's left to do for us now is
to wire up alternates as an option for the database creation.

Do so.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
next
Patrick Steinhardt 2026-09-10 17:09:18 +02:00 committed by Junio C Hamano
parent d13e56771d
commit a9c1a15deb
4 changed files with 11 additions and 9 deletions

View File

@ -1364,11 +1364,8 @@ int cmd_clone(int argc,
if (option_local > 0 && !is_local)
warning(_("--local is ignored"));

create_object_database(the_repository);
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]);
create_object_database(the_repository, &alternates);

transport = transport_get(remote, path ? path : remote->url.v[0]);
transport_set_verbosity(transport, option_verbosity, option_progress);

View File

@ -251,7 +251,7 @@ int cmd_init_db(int argc,
template_dir, hash_algo, ref_storage_format,
init_shared_repository, &reinit);
create_reference_database(the_repository, initial_branch, quiet);
create_object_database(the_repository);
create_object_database(the_repository, NULL);

if (!quiet) {
int len = strlen(git_dir);

View File

@ -2647,9 +2647,12 @@ static int create_default_files(struct repository *repo,
return reinit;
}

void create_object_database(struct repository *repo)
void create_object_database(struct repository *repo,
const struct strvec *alternates)
{
struct odb_create_on_disk_options opts = { 0 };
struct odb_create_on_disk_options opts = {
.alternates = alternates,
};

/*
* Create the "objects" directory in the common directory. This is done

View File

@ -291,9 +291,11 @@ void create_reference_database(struct repository *repo, const char *initial_bran

/*
* Create the object database for the repository. The repository must have
* already been configured properly before calling this function.
* already been configured properly before calling this function. When set,
* `alternates` is the list of alternates that should be written into the
* object database.
*/
void create_object_database(struct repository *repo);
void create_object_database(struct repository *repo, const struct strvec *alternates);

/*
* NOTE NOTE NOTE!!