odb: get rid of the_repository when handling alternates

The functions to manage alternates all depend on `the_repository`.
Refactor them to accept an object database as a parameter and adjust all
callers. The functions are renamed accordingly.

Note that right now the situation is still somewhat weird because we end
up using the object store path provided by the object store's repository
anyway. Consequently, we could have instead passed in a pointer to the
repository instead of passing in the pointer to the object store. This
will be addressed in subsequent commits though, where we will start to
use the path owned by the object store itself.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2025-07-01 14:22:20 +02:00
committed by Junio C Hamano
parent 1b1679c688
commit c44185f6c1
14 changed files with 83 additions and 64 deletions

44
odb.c
View File

@@ -272,10 +272,11 @@ static void read_info_alternates(struct object_database *odb,
free(path);
}
void add_to_alternates_file(const char *reference)
void odb_add_to_alternates_file(struct object_database *odb,
const char *reference)
{
struct lock_file lock = LOCK_INIT;
char *alts = repo_git_path(the_repository, "objects/info/alternates");
char *alts = repo_git_path(odb->repo, "objects/info/alternates");
FILE *in, *out;
int found = 0;
@@ -308,22 +309,23 @@ void add_to_alternates_file(const char *reference)
fprintf_or_die(out, "%s\n", reference);
if (commit_lock_file(&lock))
die_errno(_("unable to move new alternates file into place"));
if (the_repository->objects->loaded_alternates)
link_alt_odb_entries(the_repository->objects, reference,
if (odb->loaded_alternates)
link_alt_odb_entries(odb, reference,
'\n', NULL, 0);
}
free(alts);
}
void add_to_alternates_memory(const char *reference)
void odb_add_to_alternates_memory(struct object_database *odb,
const char *reference)
{
/*
* Make sure alternates are initialized, or else our entry may be
* overwritten when they are.
*/
prepare_alt_odb(the_repository);
odb_prepare_alternates(odb);
link_alt_odb_entries(the_repository->objects, reference,
link_alt_odb_entries(odb, reference,
'\n', NULL, 0);
}
@@ -335,7 +337,7 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
* Make sure alternates are initialized, or else our entry may be
* overwritten when they are.
*/
prepare_alt_odb(the_repository);
odb_prepare_alternates(the_repository->objects);
/*
* Make a new primary odb and link the old primary ODB in as an
@@ -379,12 +381,6 @@ void restore_primary_odb(struct odb_source *restore_alt, const char *old_path)
free_object_directory(cur_alt);
}
/*
* Compute the exact path an alternate is at and returns it. In case of
* error NULL is returned and the human readable error is added to `err`
* `path` may be relative and should point to $GIT_DIR.
* `err` must not be null.
*/
char *compute_alternate_path(const char *path, struct strbuf *err)
{
char *ref_git = NULL;
@@ -455,7 +451,7 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
char *obj_dir_real = real_pathdup(obj_dir, 1);
struct strbuf odb_path_real = STRBUF_INIT;
prepare_alt_odb(odb->repo);
odb_prepare_alternates(odb);
for (source = odb->sources; source; source = source->next) {
strbuf_realpath(&odb_path_real, source->path, 1);
if (!strcmp(obj_dir_real, odb_path_real.buf))
@@ -573,7 +569,7 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
struct odb_source *alternate;
int r = 0;
prepare_alt_odb(the_repository);
odb_prepare_alternates(the_repository->objects);
for (alternate = the_repository->objects->sources->next; alternate; alternate = alternate->next) {
r = fn(alternate, cb);
if (r)
@@ -582,21 +578,21 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
return r;
}
void prepare_alt_odb(struct repository *r)
void odb_prepare_alternates(struct object_database *odb)
{
if (r->objects->loaded_alternates)
if (odb->loaded_alternates)
return;
link_alt_odb_entries(r->objects, r->objects->alternate_db, PATH_SEP, NULL, 0);
link_alt_odb_entries(odb, odb->alternate_db, PATH_SEP, NULL, 0);
read_info_alternates(r->objects, r->objects->sources->path, 0);
r->objects->loaded_alternates = 1;
read_info_alternates(odb, odb->sources->path, 0);
odb->loaded_alternates = 1;
}
int has_alt_odb(struct repository *r)
int odb_has_alternates(struct object_database *odb)
{
prepare_alt_odb(r);
return !!r->objects->sources->next;
odb_prepare_alternates(odb);
return !!odb->sources->next;
}
int obj_read_use_lock = 0;