Skip to content

Commit 958a6c0

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0346f24 commit 958a6c0

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

odb.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,23 @@ void odb_add_to_alternates_memory(struct object_database *odb,
329329
'\n', NULL, 0);
330330
}
331331

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)
333334
{
334335
struct odb_source *source;
335336

336337
/*
337338
* Make sure alternates are initialized, or else our entry may be
338339
* overwritten when they are.
339340
*/
340-
odb_prepare_alternates(the_repository->objects);
341+
odb_prepare_alternates(odb);
341342

342343
/*
343344
* Make a new primary odb and link the old primary ODB in as an
344345
* alternate
345346
*/
346347
source = xcalloc(1, sizeof(*source));
347-
source->odb = the_repository->objects;
348+
source->odb = odb;
348349
source->path = xstrdup(dir);
349350

350351
/*
@@ -353,8 +354,8 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
353354
*/
354355
source->disable_ref_updates = 1;
355356
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;
358359
return source->next;
359360
}
360361

@@ -366,19 +367,21 @@ static void free_object_directory(struct odb_source *source)
366367
free(source);
367368
}
368369

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)
370373
{
371-
struct odb_source *cur_alt = the_repository->objects->sources;
374+
struct odb_source *cur_source = odb->sources;
372375

373-
if (strcmp(old_path, cur_alt->path))
376+
if (strcmp(old_path, cur_source->path))
374377
BUG("expected %s as primary object store; found %s",
375-
old_path, cur_alt->path);
378+
old_path, cur_source->path);
376379

377-
if (cur_alt->next != restore_alt)
380+
if (cur_source->next != restore_source)
378381
BUG("we expect the old primary object store to be the first alternate");
379382

380-
the_repository->objects->sources = restore_alt;
381-
free_object_directory(cur_alt);
383+
odb->sources = restore_source;
384+
free_object_directory(cur_source);
382385
}
383386

384387
char *compute_alternate_path(const char *path, struct strbuf *err)

odb.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ struct odb_source {
7373
char *path;
7474
};
7575

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-
8776
struct packed_git;
8877
struct multi_pack_index;
8978
struct cached_object_entry;
@@ -187,6 +176,20 @@ void odb_clear(struct object_database *o);
187176
*/
188177
struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
189178

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 a previous backend replaced by `odb_set_temporary_primary_source()`.
188+
*/
189+
void odb_restore_primary_source(struct object_database *odb,
190+
struct odb_source *restore_source,
191+
const char *old_path);
192+
190193
/*
191194
* Iterate through all alternates of the database and execute the provided
192195
* callback function for each of them. Stop iterating once the callback

tmp-objdir.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int tmp_objdir_destroy(struct tmp_objdir *t)
4747
the_tmp_objdir = NULL;
4848

4949
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);
5151

5252
err = remove_dir_recursively(&t->path, 0);
5353

@@ -279,7 +279,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
279279
if (t->prev_source) {
280280
if (t->repo->objects->sources->will_destroy)
281281
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);
283283
t->prev_source = NULL;
284284
}
285285

@@ -311,7 +311,8 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
311311
{
312312
if (t->prev_source)
313313
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);
315316
t->will_destroy = will_destroy;
316317
}
317318

@@ -320,7 +321,8 @@ struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
320321
if (!the_tmp_objdir || !the_tmp_objdir->prev_source)
321322
return NULL;
322323

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);
324326
the_tmp_objdir->prev_source = NULL;
325327
return the_tmp_objdir;
326328
}

0 commit comments

Comments
 (0)