Skip to content

Commit 916a05f

Browse files
pks-tgitster
authored andcommitted
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 parameter and adjusting all callers. The functions are renamed accordingly. Note that right now the situation is still somewhat weird because we end up using the path provided by the object store's repository anyway. This will be adapted over time though so that we instead store the path to the primary object directory in the object database itself. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a1d811 commit 916a05f

File tree

14 files changed

+83
-64
lines changed

14 files changed

+83
-64
lines changed

builtin/clone.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static int add_one_reference(struct string_list_item *item, void *cb_data)
171171
} else {
172172
struct strbuf sb = STRBUF_INIT;
173173
strbuf_addf(&sb, "%s/objects", ref_git);
174-
add_to_alternates_file(sb.buf);
174+
odb_add_to_alternates_file(the_repository->objects, sb.buf);
175175
strbuf_release(&sb);
176176
}
177177

@@ -212,12 +212,14 @@ static void copy_alternates(struct strbuf *src, const char *src_repo)
212212
if (!line.len || line.buf[0] == '#')
213213
continue;
214214
if (is_absolute_path(line.buf)) {
215-
add_to_alternates_file(line.buf);
215+
odb_add_to_alternates_file(the_repository->objects,
216+
line.buf);
216217
continue;
217218
}
218219
abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
219220
if (!normalize_path_copy(abs_path, abs_path))
220-
add_to_alternates_file(abs_path);
221+
odb_add_to_alternates_file(the_repository->objects,
222+
abs_path);
221223
else
222224
warning("skipping invalid relative alternate: %s/%s",
223225
src_repo, line.buf);
@@ -352,7 +354,7 @@ static void clone_local(const char *src_repo, const char *dest_repo)
352354
struct strbuf alt = STRBUF_INIT;
353355
get_common_dir(&alt, src_repo);
354356
strbuf_addstr(&alt, "/objects");
355-
add_to_alternates_file(alt.buf);
357+
odb_add_to_alternates_file(the_repository->objects, alt.buf);
356358
strbuf_release(&alt);
357359
} else {
358360
struct strbuf src = STRBUF_INIT;

builtin/fsck.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ int cmd_fsck(int argc,
997997
for_each_packed_object(the_repository,
998998
mark_packed_for_connectivity, NULL, 0);
999999
} else {
1000-
prepare_alt_odb(the_repository);
1000+
odb_prepare_alternates(the_repository->objects);
10011001
for (source = the_repository->objects->sources; source; source = source->next)
10021002
fsck_object_dir(source->path);
10031003

@@ -1108,7 +1108,7 @@ int cmd_fsck(int argc,
11081108
if (the_repository->settings.core_commit_graph) {
11091109
struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
11101110

1111-
prepare_alt_odb(the_repository);
1111+
odb_prepare_alternates(the_repository->objects);
11121112
for (source = the_repository->objects->sources; source; source = source->next) {
11131113
child_process_init(&commit_graph_verify);
11141114
commit_graph_verify.git_cmd = 1;
@@ -1126,7 +1126,7 @@ int cmd_fsck(int argc,
11261126
if (the_repository->settings.core_multi_pack_index) {
11271127
struct child_process midx_verify = CHILD_PROCESS_INIT;
11281128

1129-
prepare_alt_odb(the_repository);
1129+
odb_prepare_alternates(the_repository->objects);
11301130
for (source = the_repository->objects->sources; source; source = source->next) {
11311131
child_process_init(&midx_verify);
11321132
midx_verify.git_cmd = 1;

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ static int grep_submodule(struct grep_opt *opt,
462462

463463
/*
464464
* NEEDSWORK: repo_read_gitmodules() might call
465-
* add_to_alternates_memory() via config_from_gitmodules(). This
465+
* odb_add_to_alternates_memory() via config_from_gitmodules(). This
466466
* operation causes a race condition with concurrent object readings
467467
* performed by the worker threads. That's why we need obj_read_lock()
468468
* here. It should be removed once it's no longer necessary to add the

builtin/repack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,8 @@ int cmd_repack(int argc,
12561256
if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
12571257
die(_(incremental_bitmap_conflict_error));
12581258

1259-
if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1259+
if (write_bitmaps && po_args.local &&
1260+
odb_has_alternates(the_repository->objects)) {
12601261
/*
12611262
* When asked to do a local repack, but we have
12621263
* packfiles that are inherited from an alternate, then

commit-graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
649649
count = st->st_size / (the_hash_algo->hexsz + 1);
650650
CALLOC_ARRAY(oids, count);
651651

652-
prepare_alt_odb(r);
652+
odb_prepare_alternates(r->objects);
653653

654654
for (i = 0; i < count; i++) {
655655
struct odb_source *source;
@@ -778,7 +778,7 @@ static int prepare_commit_graph(struct repository *r)
778778
if (!commit_graph_compatible(r))
779779
return 0;
780780

781-
prepare_alt_odb(r);
781+
odb_prepare_alternates(r->objects);
782782
for (source = r->objects->sources;
783783
!r->objects->commit_graph && source;
784784
source = source->next)

loose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int repo_read_loose_object_map(struct repository *repo)
112112
if (!should_use_loose_object_map(repo))
113113
return 0;
114114

115-
prepare_alt_odb(repo);
115+
odb_prepare_alternates(repo->objects);
116116

117117
for (source = repo->objects->sources; source; source = source->next) {
118118
if (load_one_loose_object_map(repo, source) < 0) {

object-file.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
106106
{
107107
struct odb_source *source;
108108

109-
prepare_alt_odb(the_repository);
109+
odb_prepare_alternates(the_repository->objects);
110110
for (source = the_repository->objects->sources->next; source; source = source->next) {
111111
if (check_and_freshen_odb(source, oid, freshen))
112112
return 1;
@@ -205,7 +205,7 @@ static int stat_loose_object(struct repository *r, const struct object_id *oid,
205205
struct odb_source *source;
206206
static struct strbuf buf = STRBUF_INIT;
207207

208-
prepare_alt_odb(r);
208+
odb_prepare_alternates(r->objects);
209209
for (source = r->objects->sources; source; source = source->next) {
210210
*path = odb_loose_path(source, &buf, oid);
211211
if (!lstat(*path, st))
@@ -227,7 +227,7 @@ static int open_loose_object(struct repository *r,
227227
int most_interesting_errno = ENOENT;
228228
static struct strbuf buf = STRBUF_INIT;
229229

230-
prepare_alt_odb(r);
230+
odb_prepare_alternates(r->objects);
231231
for (source = r->objects->sources; source; source = source->next) {
232232
*path = odb_loose_path(source, &buf, oid);
233233
fd = git_open(*path);
@@ -246,7 +246,7 @@ static int quick_has_loose(struct repository *r,
246246
{
247247
struct odb_source *source;
248248

249-
prepare_alt_odb(r);
249+
odb_prepare_alternates(r->objects);
250250
for (source = r->objects->sources; source; source = source->next) {
251251
if (oidtree_contains(odb_loose_cache(source, oid), oid))
252252
return 1;
@@ -1439,7 +1439,7 @@ int for_each_loose_object(each_loose_object_fn cb, void *data,
14391439
{
14401440
struct odb_source *source;
14411441

1442-
prepare_alt_odb(the_repository);
1442+
odb_prepare_alternates(the_repository->objects);
14431443
for (source = the_repository->objects->sources; source; source = source->next) {
14441444
int r = for_each_loose_file_in_objdir(source->path, cb, NULL,
14451445
NULL, data);

object-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static int init_object_disambiguation(struct repository *r,
376376
ds->hex_pfx[len] = '\0';
377377
ds->repo = r;
378378
ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
379-
prepare_alt_odb(r);
379+
odb_prepare_alternates(r->objects);
380380
return 0;
381381
}
382382

odb.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,11 @@ static void read_info_alternates(struct object_database *odb,
272272
free(path);
273273
}
274274

275-
void add_to_alternates_file(const char *reference)
275+
void odb_add_to_alternates_file(struct object_database *odb,
276+
const char *reference)
276277
{
277278
struct lock_file lock = LOCK_INIT;
278-
char *alts = repo_git_path(the_repository, "objects/info/alternates");
279+
char *alts = repo_git_path(odb->repo, "objects/info/alternates");
279280
FILE *in, *out;
280281
int found = 0;
281282

@@ -308,22 +309,23 @@ void add_to_alternates_file(const char *reference)
308309
fprintf_or_die(out, "%s\n", reference);
309310
if (commit_lock_file(&lock))
310311
die_errno(_("unable to move new alternates file into place"));
311-
if (the_repository->objects->loaded_alternates)
312-
link_alt_odb_entries(the_repository->objects, reference,
312+
if (odb->loaded_alternates)
313+
link_alt_odb_entries(odb, reference,
313314
'\n', NULL, 0);
314315
}
315316
free(alts);
316317
}
317318

318-
void add_to_alternates_memory(const char *reference)
319+
void odb_add_to_alternates_memory(struct object_database *odb,
320+
const char *reference)
319321
{
320322
/*
321323
* Make sure alternates are initialized, or else our entry may be
322324
* overwritten when they are.
323325
*/
324-
prepare_alt_odb(the_repository);
326+
odb_prepare_alternates(odb);
325327

326-
link_alt_odb_entries(the_repository->objects, reference,
328+
link_alt_odb_entries(odb, reference,
327329
'\n', NULL, 0);
328330
}
329331

@@ -335,7 +337,7 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
335337
* Make sure alternates are initialized, or else our entry may be
336338
* overwritten when they are.
337339
*/
338-
prepare_alt_odb(the_repository);
340+
odb_prepare_alternates(the_repository->objects);
339341

340342
/*
341343
* 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)
379381
free_object_directory(cur_alt);
380382
}
381383

382-
/*
383-
* Compute the exact path an alternate is at and returns it. In case of
384-
* error NULL is returned and the human readable error is added to `err`
385-
* `path` may be relative and should point to $GIT_DIR.
386-
* `err` must not be null.
387-
*/
388384
char *compute_alternate_path(const char *path, struct strbuf *err)
389385
{
390386
char *ref_git = NULL;
@@ -455,7 +451,7 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
455451
char *obj_dir_real = real_pathdup(obj_dir, 1);
456452
struct strbuf odb_path_real = STRBUF_INIT;
457453

458-
prepare_alt_odb(odb->repo);
454+
odb_prepare_alternates(odb);
459455
for (source = odb->sources; source; source = source->next) {
460456
strbuf_realpath(&odb_path_real, source->path, 1);
461457
if (!strcmp(obj_dir_real, odb_path_real.buf))
@@ -573,7 +569,7 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
573569
struct odb_source *alternate;
574570
int r = 0;
575571

576-
prepare_alt_odb(the_repository);
572+
odb_prepare_alternates(the_repository->objects);
577573
for (alternate = the_repository->objects->sources->next; alternate; alternate = alternate->next) {
578574
r = fn(alternate, cb);
579575
if (r)
@@ -582,21 +578,21 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
582578
return r;
583579
}
584580

585-
void prepare_alt_odb(struct repository *r)
581+
void odb_prepare_alternates(struct object_database *odb)
586582
{
587-
if (r->objects->loaded_alternates)
583+
if (odb->loaded_alternates)
588584
return;
589585

590-
link_alt_odb_entries(r->objects, r->objects->alternate_db, PATH_SEP, NULL, 0);
586+
link_alt_odb_entries(odb, odb->alternate_db, PATH_SEP, NULL, 0);
591587

592-
read_info_alternates(r->objects, r->objects->sources->path, 0);
593-
r->objects->loaded_alternates = 1;
588+
read_info_alternates(odb, odb->sources->path, 0);
589+
odb->loaded_alternates = 1;
594590
}
595591

596-
int has_alt_odb(struct repository *r)
592+
int odb_has_alternates(struct object_database *odb)
597593
{
598-
prepare_alt_odb(r);
599-
return !!r->objects->sources->next;
594+
odb_prepare_alternates(odb);
595+
return !!odb->sources->next;
600596
}
601597

602598
int obj_read_use_lock = 0;

odb.h

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ struct oidtree;
1313
struct strbuf;
1414
struct repository;
1515

16+
/*
17+
* Compute the exact path an alternate is at and returns it. In case of
18+
* error NULL is returned and the human readable error is added to `err`
19+
* `path` may be relative and should point to $GIT_DIR.
20+
* `err` must not be null.
21+
*/
22+
char *compute_alternate_path(const char *path, struct strbuf *err);
23+
1624
/*
1725
* The source is the part of the object database that stores the actual
1826
* objects. It thus encapsulates the logic to read and write the specific
@@ -65,27 +73,11 @@ struct odb_source {
6573
char *path;
6674
};
6775

68-
void prepare_alt_odb(struct repository *r);
69-
int has_alt_odb(struct repository *r);
70-
char *compute_alternate_path(const char *path, struct strbuf *err);
7176
typedef int alt_odb_fn(struct odb_source *, void *);
7277
int foreach_alt_odb(alt_odb_fn, void*);
7378
typedef void alternate_ref_fn(const struct object_id *oid, void *);
7479
void for_each_alternate_ref(alternate_ref_fn, void *);
7580

76-
/*
77-
* Add the directory to the on-disk alternates file; the new entry will also
78-
* take effect in the current process.
79-
*/
80-
void add_to_alternates_file(const char *dir);
81-
82-
/*
83-
* Add the directory to the in-memory list of alternates (along with any
84-
* recursive alternates it points to), but do not modify the on-disk alternates
85-
* file.
86-
*/
87-
void add_to_alternates_memory(const char *dir);
88-
8981
/*
9082
* Replace the current writable object directory with the specified temporary
9183
* object directory; returns the former primary object directory.
@@ -124,7 +116,7 @@ struct object_database {
124116
/*
125117
* A list of alternate object directories loaded from the environment;
126118
* this should not generally need to be accessed directly, but will
127-
* populate the "sources" list when prepare_alt_odb() is run.
119+
* populate the "sources" list when odb_prepare_alternates() is run.
128120
*/
129121
char *alternate_db;
130122

@@ -209,6 +201,33 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
209201
int odb_mkstemp(struct object_database *odb,
210202
struct strbuf *temp_filename, const char *pattern);
211203

204+
/*
205+
* Prepare alternate object sources for the given database by reading
206+
* "objects/info/alternates" and opening the respective sources.
207+
*/
208+
void odb_prepare_alternates(struct object_database *odb);
209+
210+
/*
211+
* Check whether the object database has any alternates. The primary object
212+
* source does not count as alternate.
213+
*/
214+
int odb_has_alternates(struct object_database *odb);
215+
216+
/*
217+
* Add the directory to the on-disk alternates file; the new entry will also
218+
* take effect in the current process.
219+
*/
220+
void odb_add_to_alternates_file(struct object_database *odb,
221+
const char *dir);
222+
223+
/*
224+
* Add the directory to the in-memory list of alternate sources (along with any
225+
* recursive alternates it points to), but do not modify the on-disk alternates
226+
* file.
227+
*/
228+
void odb_add_to_alternates_memory(struct object_database *odb,
229+
const char *dir);
230+
212231
void *repo_read_object_file(struct repository *r,
213232
const struct object_id *oid,
214233
enum object_type *type,

0 commit comments

Comments
 (0)