odb: return newly created in-memory sources

Callers have no trivial way to obtain the newly created object database
source when adding it to the in-memory list of alternates. While not yet
needed anywhere, a subsequent commit will want to obtain that pointer.

Refactor the function to return the source to make it easily accessible.

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-08-11 15:46:44 +02:00
committed by Junio C Hamano
parent 25c532f6e0
commit a59d44ff3f
2 changed files with 20 additions and 14 deletions

30
odb.c
View File

@@ -139,17 +139,16 @@ static void read_info_alternates(struct object_database *odb,
const char *relative_base, const char *relative_base,
int depth); int depth);
static int link_alt_odb_entry(struct object_database *odb, static struct odb_source *link_alt_odb_entry(struct object_database *odb,
const char *dir, const char *dir,
const char *relative_base, const char *relative_base,
int depth, int depth,
const char *normalized_objdir) const char *normalized_objdir)
{ {
struct odb_source *alternate; struct odb_source *alternate = NULL;
struct strbuf pathbuf = STRBUF_INIT; struct strbuf pathbuf = STRBUF_INIT;
struct strbuf tmp = STRBUF_INIT; struct strbuf tmp = STRBUF_INIT;
khiter_t pos; khiter_t pos;
int ret = -1;
if (!is_absolute_path(dir) && relative_base) { if (!is_absolute_path(dir) && relative_base) {
strbuf_realpath(&pathbuf, relative_base, 1); strbuf_realpath(&pathbuf, relative_base, 1);
@@ -189,11 +188,11 @@ static int link_alt_odb_entry(struct object_database *odb,
/* recursively add alternates */ /* recursively add alternates */
read_info_alternates(odb, alternate->path, depth + 1); read_info_alternates(odb, alternate->path, depth + 1);
ret = 0;
error: error:
strbuf_release(&tmp); strbuf_release(&tmp);
strbuf_release(&pathbuf); strbuf_release(&pathbuf);
return ret; return alternate;
} }
static const char *parse_alt_odb_entry(const char *string, static const char *parse_alt_odb_entry(const char *string,
@@ -315,16 +314,23 @@ void odb_add_to_alternates_file(struct object_database *odb,
free(alts); free(alts);
} }
void odb_add_to_alternates_memory(struct object_database *odb, struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
const char *dir) const char *dir)
{ {
struct odb_source *alternate;
char *objdir;
/* /*
* Make sure alternates are initialized, or else our entry may be * Make sure alternates are initialized, or else our entry may be
* overwritten when they are. * overwritten when they are.
*/ */
odb_prepare_alternates(odb); odb_prepare_alternates(odb);
link_alt_odb_entries(odb, dir, '\n', NULL, 0); objdir = real_pathdup(odb->sources->path, 1);
alternate = link_alt_odb_entry(odb, dir, NULL, 0, objdir);
free(objdir);
return alternate;
} }
struct odb_source *odb_set_temporary_primary_source(struct object_database *odb, struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,

4
odb.h
View File

@@ -268,8 +268,8 @@ void odb_add_to_alternates_file(struct object_database *odb,
* recursive alternates it points to), but do not modify the on-disk alternates * recursive alternates it points to), but do not modify the on-disk alternates
* file. * file.
*/ */
void odb_add_to_alternates_memory(struct object_database *odb, struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
const char *dir); const char *dir);
/* /*
* Read an object from the database. Returns the object data and assigns object * Read an object from the database. Returns the object data and assigns object