Skip to content

Commit 0346f24

Browse files
pks-tgitster
authored andcommitted
odb: get rid of the_repository in for_each() functions
There are a couple of iterator-style functions that execute a callback for each instance of a given set, all of which currently depend on `the_repository`. Refactor them to instead take an object database as parameter so that we can get rid of this dependency. Rename the functions accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 916a05f commit 0346f24

File tree

8 files changed

+47
-28
lines changed

8 files changed

+47
-28
lines changed

builtin/count-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int cmd_count_objects(int argc,
159159
printf("prune-packable: %lu\n", packed_loose);
160160
printf("garbage: %lu\n", garbage);
161161
printf("size-garbage: %s\n", garbage_buf.buf);
162-
foreach_alt_odb(print_alternate, NULL);
162+
odb_for_each_alternate(the_repository->objects, print_alternate, NULL);
163163
strbuf_release(&loose_buf);
164164
strbuf_release(&pack_buf);
165165
strbuf_release(&garbage_buf);

builtin/receive-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ static void write_head_info(void)
359359

360360
refs_for_each_fullref_in(get_main_ref_store(the_repository), "",
361361
exclude_patterns, show_ref_cb, &seen);
362-
for_each_alternate_ref(show_one_alternate_ref, &seen);
362+
odb_for_each_alternate_ref(the_repository->objects,
363+
show_one_alternate_ref, &seen);
363364

364365
oidset_clear(&seen);
365366
strvec_clear(&excludes_vector);

builtin/submodule--helper.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,8 @@ static void prepare_possible_alternates(const char *sm_name,
16681668
die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
16691669

16701670
if (!strcmp(sm_alternate, "superproject"))
1671-
foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1671+
odb_for_each_alternate(the_repository->objects,
1672+
add_possible_reference_from_superproject, &sas);
16721673
else if (!strcmp(sm_alternate, "no"))
16731674
; /* do nothing */
16741675
else

diagnose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ int create_diagnostics_archive(struct repository *r,
229229
strbuf_reset(&buf);
230230
strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
231231
dir_file_stats(r->objects->sources, &buf);
232-
foreach_alt_odb(dir_file_stats, &buf);
232+
odb_for_each_alternate(r->objects, dir_file_stats, &buf);
233233
strvec_push(&archiver_args, buf.buf);
234234

235235
strbuf_reset(&buf);

fetch-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
115115
size_t i;
116116

117117
if (!initialized) {
118-
for_each_alternate_ref(cache_one_alternate, &cache);
118+
odb_for_each_alternate_ref(the_repository->objects,
119+
cache_one_alternate, &cache);
119120
initialized = 1;
120121
}
121122

odb.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ static void fill_alternate_refs_command(struct child_process *cmd,
494494
}
495495

496496
static void read_alternate_refs(const char *path,
497-
alternate_ref_fn *cb,
498-
void *data)
497+
odb_for_each_alternate_ref_fn *cb,
498+
void *payload)
499499
{
500500
struct child_process cmd = CHILD_PROCESS_INIT;
501501
struct strbuf line = STRBUF_INIT;
@@ -517,7 +517,7 @@ static void read_alternate_refs(const char *path,
517517
break;
518518
}
519519

520-
cb(&oid, data);
520+
cb(&oid, payload);
521521
}
522522

523523
fclose(fh);
@@ -526,16 +526,16 @@ static void read_alternate_refs(const char *path,
526526
}
527527

528528
struct alternate_refs_data {
529-
alternate_ref_fn *fn;
530-
void *data;
529+
odb_for_each_alternate_ref_fn *fn;
530+
void *payload;
531531
};
532532

533533
static int refs_from_alternate_cb(struct odb_source *alternate,
534-
void *data)
534+
void *payload)
535535
{
536536
struct strbuf path = STRBUF_INIT;
537537
size_t base_len;
538-
struct alternate_refs_data *cb = data;
538+
struct alternate_refs_data *cb = payload;
539539

540540
if (!strbuf_realpath(&path, alternate->path, 0))
541541
goto out;
@@ -549,29 +549,31 @@ static int refs_from_alternate_cb(struct odb_source *alternate,
549549
goto out;
550550
strbuf_setlen(&path, base_len);
551551

552-
read_alternate_refs(path.buf, cb->fn, cb->data);
552+
read_alternate_refs(path.buf, cb->fn, cb->payload);
553553

554554
out:
555555
strbuf_release(&path);
556556
return 0;
557557
}
558558

559-
void for_each_alternate_ref(alternate_ref_fn fn, void *data)
559+
void odb_for_each_alternate_ref(struct object_database *odb,
560+
odb_for_each_alternate_ref_fn cb, void *payload)
560561
{
561-
struct alternate_refs_data cb;
562-
cb.fn = fn;
563-
cb.data = data;
564-
foreach_alt_odb(refs_from_alternate_cb, &cb);
562+
struct alternate_refs_data data;
563+
data.fn = cb;
564+
data.payload = payload;
565+
odb_for_each_alternate(odb, refs_from_alternate_cb, &data);
565566
}
566567

567-
int foreach_alt_odb(alt_odb_fn fn, void *cb)
568+
int odb_for_each_alternate(struct object_database *odb,
569+
odb_for_each_alternate_fn cb, void *payload)
568570
{
569571
struct odb_source *alternate;
570572
int r = 0;
571573

572-
odb_prepare_alternates(the_repository->objects);
573-
for (alternate = the_repository->objects->sources->next; alternate; alternate = alternate->next) {
574-
r = fn(alternate, cb);
574+
odb_prepare_alternates(odb);
575+
for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
576+
r = cb(alternate, payload);
575577
if (r)
576578
break;
577579
}

odb.h

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

76-
typedef int alt_odb_fn(struct odb_source *, void *);
77-
int foreach_alt_odb(alt_odb_fn, void*);
78-
typedef void alternate_ref_fn(const struct object_id *oid, void *);
79-
void for_each_alternate_ref(alternate_ref_fn, void *);
80-
8176
/*
8277
* Replace the current writable object directory with the specified temporary
8378
* object directory; returns the former primary object directory.
@@ -192,6 +187,24 @@ void odb_clear(struct object_database *o);
192187
*/
193188
struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
194189

190+
/*
191+
* Iterate through all alternates of the database and execute the provided
192+
* callback function for each of them. Stop iterating once the callback
193+
* function returns a non-zero value, in which case the value is bubbled up
194+
* from the callback.
195+
*/
196+
typedef int odb_for_each_alternate_fn(struct odb_source *, void *);
197+
int odb_for_each_alternate(struct object_database *odb,
198+
odb_for_each_alternate_fn cb, void *payload);
199+
200+
/*
201+
* Iterate through all alternates of the database and yield their respective
202+
* references.
203+
*/
204+
typedef void odb_for_each_alternate_ref_fn(const struct object_id *oid, void *);
205+
void odb_for_each_alternate_ref(struct object_database *odb,
206+
odb_for_each_alternate_ref_fn cb, void *payload);
207+
195208
/*
196209
* Create a temporary file rooted in the primary alternate's directory, or die
197210
* on failure. The filename is taken from "pattern", which should have the

revision.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,8 @@ static void add_alternate_refs_to_pending(struct rev_info *revs,
19071907
struct add_alternate_refs_data data;
19081908
data.revs = revs;
19091909
data.flags = flags;
1910-
for_each_alternate_ref(add_one_alternate_ref, &data);
1910+
odb_for_each_alternate_ref(the_repository->objects,
1911+
add_one_alternate_ref, &data);
19111912
}
19121913

19131914
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,

0 commit comments

Comments
 (0)