Skip to content

Commit dae4a6a

Browse files
pks-tgitster
authored andcommitted
odb: introduce parent pointers
In subsequent commits we'll get rid of our use of `the_repository` in "odb.c" in favor of explicitly passing in a `struct object_database` or a `struct odb_source`. In some cases though we'll need access to the repository, for example to read a config value from it, but we don't have a way to access the repository owning a specific object database. Introduce parent pointers for `struct object_database` to its owning repository as well as for `struct odb_source` to its owning object database, which will allow us to adapt those use cases. Note that this change requires us to pass through the object database to `link_alt_odb_entry()` so that we can set up the parent pointers for any source there. The callchain is adapted to pass through the object database accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a7df3f commit dae4a6a

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

odb.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ static int alt_odb_usable(struct object_database *o,
135135
* of the object ID, an extra slash for the first level indirection, and
136136
* the terminating NUL.
137137
*/
138-
static void read_info_alternates(struct repository *r,
138+
static void read_info_alternates(struct object_database *odb,
139139
const char *relative_base,
140140
int depth);
141-
static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
142-
const char *relative_base, int depth, const char *normalized_objdir)
141+
142+
static int link_alt_odb_entry(struct object_database *odb,
143+
const struct strbuf *entry,
144+
const char *relative_base,
145+
int depth,
146+
const char *normalized_objdir)
143147
{
144148
struct odb_source *alternate;
145149
struct strbuf pathbuf = STRBUF_INIT;
@@ -167,22 +171,23 @@ static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
167171
while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
168172
strbuf_setlen(&pathbuf, pathbuf.len - 1);
169173

170-
if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos))
174+
if (!alt_odb_usable(odb, &pathbuf, normalized_objdir, &pos))
171175
goto error;
172176

173177
CALLOC_ARRAY(alternate, 1);
174-
/* pathbuf.buf is already in r->objects->source_by_path */
178+
alternate->odb = odb;
179+
/* pathbuf.buf is already in r->objects->alternate_by_path */
175180
alternate->path = strbuf_detach(&pathbuf, NULL);
176181

177182
/* add the alternate entry */
178-
*r->objects->sources_tail = alternate;
179-
r->objects->sources_tail = &(alternate->next);
183+
*odb->sources_tail = alternate;
184+
odb->sources_tail = &(alternate->next);
180185
alternate->next = NULL;
181-
assert(r->objects->source_by_path);
182-
kh_value(r->objects->source_by_path, pos) = alternate;
186+
assert(odb->source_by_path);
187+
kh_value(odb->source_by_path, pos) = alternate;
183188

184189
/* recursively add alternates */
185-
read_info_alternates(r, alternate->path, depth + 1);
190+
read_info_alternates(odb, alternate->path, depth + 1);
186191
ret = 0;
187192
error:
188193
strbuf_release(&tmp);
@@ -219,7 +224,7 @@ static const char *parse_alt_odb_entry(const char *string,
219224
return end;
220225
}
221226

222-
static void link_alt_odb_entries(struct repository *r, const char *alt,
227+
static void link_alt_odb_entries(struct object_database *odb, const char *alt,
223228
int sep, const char *relative_base, int depth)
224229
{
225230
struct strbuf objdirbuf = STRBUF_INIT;
@@ -234,20 +239,20 @@ static void link_alt_odb_entries(struct repository *r, const char *alt,
234239
return;
235240
}
236241

237-
strbuf_realpath(&objdirbuf, r->objects->sources->path, 1);
242+
strbuf_realpath(&objdirbuf, odb->sources->path, 1);
238243

239244
while (*alt) {
240245
alt = parse_alt_odb_entry(alt, sep, &entry);
241246
if (!entry.len)
242247
continue;
243-
link_alt_odb_entry(r, &entry,
248+
link_alt_odb_entry(odb, &entry,
244249
relative_base, depth, objdirbuf.buf);
245250
}
246251
strbuf_release(&entry);
247252
strbuf_release(&objdirbuf);
248253
}
249254

250-
static void read_info_alternates(struct repository *r,
255+
static void read_info_alternates(struct object_database *odb,
251256
const char *relative_base,
252257
int depth)
253258
{
@@ -261,7 +266,7 @@ static void read_info_alternates(struct repository *r,
261266
return;
262267
}
263268

264-
link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
269+
link_alt_odb_entries(odb, buf.buf, '\n', relative_base, depth);
265270
strbuf_release(&buf);
266271
free(path);
267272
}
@@ -303,7 +308,7 @@ void add_to_alternates_file(const char *reference)
303308
if (commit_lock_file(&lock))
304309
die_errno(_("unable to move new alternates file into place"));
305310
if (the_repository->objects->loaded_alternates)
306-
link_alt_odb_entries(the_repository, reference,
311+
link_alt_odb_entries(the_repository->objects, reference,
307312
'\n', NULL, 0);
308313
}
309314
free(alts);
@@ -317,7 +322,7 @@ void add_to_alternates_memory(const char *reference)
317322
*/
318323
prepare_alt_odb(the_repository);
319324

320-
link_alt_odb_entries(the_repository, reference,
325+
link_alt_odb_entries(the_repository->objects, reference,
321326
'\n', NULL, 0);
322327
}
323328

@@ -336,6 +341,7 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
336341
* alternate
337342
*/
338343
source = xcalloc(1, sizeof(*source));
344+
source->odb = the_repository->objects;
339345
source->path = xstrdup(dir);
340346

341347
/*
@@ -580,9 +586,9 @@ void prepare_alt_odb(struct repository *r)
580586
if (r->objects->loaded_alternates)
581587
return;
582588

583-
link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
589+
link_alt_odb_entries(r->objects, r->objects->alternate_db, PATH_SEP, NULL, 0);
584590

585-
read_info_alternates(r, r->objects->sources->path, 0);
591+
read_info_alternates(r->objects, r->objects->sources->path, 0);
586592
r->objects->loaded_alternates = 1;
587593
}
588594

@@ -950,11 +956,12 @@ void assert_oid_type(const struct object_id *oid, enum object_type expect)
950956
type_name(expect));
951957
}
952958

953-
struct object_database *odb_new(void)
959+
struct object_database *odb_new(struct repository *repo)
954960
{
955961
struct object_database *o = xmalloc(sizeof(*o));
956962

957963
memset(o, 0, sizeof(*o));
964+
o->repo = repo;
958965
INIT_LIST_HEAD(&o->packed_git_mru);
959966
hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
960967
pthread_mutex_init(&o->replace_mutex, NULL);

odb.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct repository;
2828
struct odb_source {
2929
struct odb_source *next;
3030

31+
/* Object database that owns this object source. */
32+
struct object_database *odb;
33+
3134
/*
3235
* Used to store the results of readdir(3) calls when we are OK
3336
* sacrificing accuracy due to races for speed. That includes
@@ -105,6 +108,9 @@ struct cached_object_entry;
105108
* configured via alternates.
106109
*/
107110
struct object_database {
111+
/* Repository that owns this database. */
112+
struct repository *repo;
113+
108114
/*
109115
* Set of all object directories; the main directory is first (and
110116
* cannot be NULL after initialization). Subsequent directories are
@@ -186,7 +192,7 @@ struct object_database {
186192
unsigned packed_git_initialized : 1;
187193
};
188194

189-
struct object_database *odb_new(void);
195+
struct object_database *odb_new(struct repository *repo);
190196
void odb_clear(struct object_database *o);
191197

192198
/*

repository.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void set_default_hash_algo(struct repository *repo)
5252

5353
void initialize_repository(struct repository *repo)
5454
{
55-
repo->objects = odb_new();
55+
repo->objects = odb_new(repo);
5656
repo->remote_state = remote_state_new();
5757
repo->parsed_objects = parsed_object_pool_new(repo);
5858
ALLOC_ARRAY(repo->index, 1);
@@ -167,6 +167,7 @@ void repo_set_gitdir(struct repository *repo,
167167

168168
if (!repo->objects->sources) {
169169
CALLOC_ARRAY(repo->objects->sources, 1);
170+
repo->objects->sources->odb = repo->objects;
170171
repo->objects->sources_tail = &repo->objects->sources->next;
171172
}
172173
expand_base_dir(&repo->objects->sources->path, o->object_dir,

0 commit comments

Comments
 (0)