odb: get rid of `the_repository` when handling the primary source

The functions `set_temporary_primary_odb()` and `restore_primary_odb()` are responsible for managing a temporary primary source for the database. Both of these functions implicitly rely on `the_repository`. Refactor them to instead take an explicit object database parameter as argument and adjust callers. Rename the functions accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 1, 2025 at 14:22 UTC 7eafd4472d7c273e10a408da6662ca9d4b9800fd
3 files changed +36 -27
odb.c
+15 -12
@@ -329,7 +329,8 @@ void odb_add_to_alternates_memory(struct object_database *odb,
329 '\n', NULL, 0);
330 }
331
332 -struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
332 +struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
333 + const char *dir, int will_destroy)
334 {
335 struct odb_source *source;
336
@@ -337,14 +338,14 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
338 * Make sure alternates are initialized, or else our entry may be
339 * overwritten when they are.
340 */
340 - odb_prepare_alternates(the_repository->objects);
341 + odb_prepare_alternates(odb);
342
343 /*
344 * Make a new primary odb and link the old primary ODB in as an
345 * alternate
346 */
347 source = xcalloc(1, sizeof(*source));
347 - source->odb = the_repository->objects;
348 + source->odb = odb;
349 source->path = xstrdup(dir);
350
351 /*
@@ -353,8 +354,8 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
354 */
355 source->disable_ref_updates = 1;
356 source->will_destroy = will_destroy;
356 - source->next = the_repository->objects->sources;
357 - the_repository->objects->sources = source;
357 + source->next = odb->sources;
358 + odb->sources = source;
359 return source->next;
360 }
361
@@ -366,19 +367,21 @@ static void free_object_directory(struct odb_source *source)
367 free(source);
368 }
369
369 -void restore_primary_odb(struct odb_source *restore_alt, const char *old_path)
370 +void odb_restore_primary_source(struct object_database *odb,
371 + struct odb_source *restore_source,
372 + const char *old_path)
373 {
371 - struct odb_source *cur_alt = the_repository->objects->sources;
374 + struct odb_source *cur_source = odb->sources;
375
373 - if (strcmp(old_path, cur_alt->path))
376 + if (strcmp(old_path, cur_source->path))
377 BUG("expected %s as primary object store; found %s",
375 - old_path, cur_alt->path);
378 + old_path, cur_source->path);
379
377 - if (cur_alt->next != restore_alt)
380 + if (cur_source->next != restore_source)
381 BUG("we expect the old primary object store to be the first alternate");
382
380 - the_repository->objects->sources = restore_alt;
381 - free_object_directory(cur_alt);
383 + odb->sources = restore_source;
384 + free_object_directory(cur_source);
385 }
386
387 char *compute_alternate_path(const char *path, struct strbuf *err)
odb.h
+15 -11
@@ -73,17 +73,6 @@ struct odb_source {
73 char *path;
74 };
75
76 -/*
77 - * Replace the current writable object directory with the specified temporary
78 - * object directory; returns the former primary object directory.
79 - */
80 -struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy);
81 -
82 -/*
83 - * Restore a previous ODB replaced by set_temporary_main_odb.
84 - */
85 -void restore_primary_odb(struct odb_source *restore_alternate, const char *old_path);
86 -
76 struct packed_git;
77 struct multi_pack_index;
78 struct cached_object_entry;
@@ -187,6 +176,21 @@ void odb_clear(struct object_database *o);
176 */
177 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
178
179 +/*
180 + * Replace the current writable object directory with the specified temporary
181 + * object directory; returns the former primary source.
182 + */
183 +struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
184 + const char *dir, int will_destroy);
185 +
186 +/*
187 + * Restore the primary source that was previously replaced by
188 + * `odb_set_temporary_primary_source()`.
189 + */
190 +void odb_restore_primary_source(struct object_database *odb,
191 + struct odb_source *restore_source,
192 + const char *old_path);
193 +
194 /*
195 * Iterate through all alternates of the database and execute the provided
196 * callback function for each of them. Stop iterating once the callback
tmp-objdir.c
+6 -4
@@ -47,7 +47,7 @@ int tmp_objdir_destroy(struct tmp_objdir *t)
47 the_tmp_objdir = NULL;
48
49 if (t->prev_source)
50 - restore_primary_odb(t->prev_source, t->path.buf);
50 + odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
51
52 err = remove_dir_recursively(&t->path, 0);
53
@@ -279,7 +279,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
279 if (t->prev_source) {
280 if (t->repo->objects->sources->will_destroy)
281 BUG("migrating an ODB that was marked for destruction");
282 - restore_primary_odb(t->prev_source, t->path.buf);
282 + odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
283 t->prev_source = NULL;
284 }
285
@@ -311,7 +311,8 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
311 {
312 if (t->prev_source)
313 BUG("the primary object database is already replaced");
314 - t->prev_source = set_temporary_primary_odb(t->path.buf, will_destroy);
314 + t->prev_source = odb_set_temporary_primary_source(t->repo->objects,
315 + t->path.buf, will_destroy);
316 t->will_destroy = will_destroy;
317 }
318
@@ -320,7 +321,8 @@ struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
321 if (!the_tmp_objdir || !the_tmp_objdir->prev_source)
322 return NULL;
323
323 - restore_primary_odb(the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
324 + odb_restore_primary_source(the_tmp_objdir->repo->objects,
325 + the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
326 the_tmp_objdir->prev_source = NULL;
327 return the_tmp_objdir;
328 }