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>

Patrick Steinhardt committed Aug 11, 2025 at 15:46 UTC a59d44ff3f0f308f9577b05c858c063d2466b061
2 files changed +20 -14
odb.c
+18 -12
@@ -139,17 +139,16 @@ static void read_info_alternates(struct object_database *odb,
139 const char *relative_base,
140 int depth);
141
142 -static int link_alt_odb_entry(struct object_database *odb,
143 - const char *dir,
144 - const char *relative_base,
145 - int depth,
146 - const char *normalized_objdir)
142 +static struct odb_source *link_alt_odb_entry(struct object_database *odb,
143 + const char *dir,
144 + const char *relative_base,
145 + int depth,
146 + const char *normalized_objdir)
147 {
148 - struct odb_source *alternate;
148 + struct odb_source *alternate = NULL;
149 struct strbuf pathbuf = STRBUF_INIT;
150 struct strbuf tmp = STRBUF_INIT;
151 khiter_t pos;
152 - int ret = -1;
152
153 if (!is_absolute_path(dir) && relative_base) {
154 strbuf_realpath(&pathbuf, relative_base, 1);
@@ -189,11 +188,11 @@ static int link_alt_odb_entry(struct object_database *odb,
188
189 /* recursively add alternates */
190 read_info_alternates(odb, alternate->path, depth + 1);
192 - ret = 0;
191 +
192 error:
193 strbuf_release(&tmp);
194 strbuf_release(&pathbuf);
196 - return ret;
195 + return alternate;
196 }
197
198 static const char *parse_alt_odb_entry(const char *string,
@@ -315,16 +314,23 @@ void odb_add_to_alternates_file(struct object_database *odb,
314 free(alts);
315 }
316
318 -void odb_add_to_alternates_memory(struct object_database *odb,
319 - const char *dir)
317 +struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
318 + const char *dir)
319 {
320 + struct odb_source *alternate;
321 + char *objdir;
322 +
323 /*
324 * Make sure alternates are initialized, or else our entry may be
325 * overwritten when they are.
326 */
327 odb_prepare_alternates(odb);
328
327 - link_alt_odb_entries(odb, dir, '\n', NULL, 0);
329 + objdir = real_pathdup(odb->sources->path, 1);
330 + alternate = link_alt_odb_entry(odb, dir, NULL, 0, objdir);
331 +
332 + free(objdir);
333 + return alternate;
334 }
335
336 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
odb.h
+2 -2
@@ -268,8 +268,8 @@ void odb_add_to_alternates_file(struct object_database *odb,
268 * recursive alternates it points to), but do not modify the on-disk alternates
269 * file.
270 */
271 -void odb_add_to_alternates_memory(struct object_database *odb,
272 - const char *dir);
271 +struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
272 + const char *dir);
273
274 /*
275 * Read an object from the database. Returns the object data and assigns object