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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jul 1, 2025 at 14:22 UTC
798c661ce39f7d5297fa7ea8928ae464b6d5dd95
8 files changed
+47
-28
builtin/count-objects.c
+1
-1
@@ -159,7 +159,7 @@ int cmd_count_objects(int argc,
159
printf("prune-packable: %lu\n", packed_loose);
160
printf("garbage: %lu\n", garbage);
161
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);
163
strbuf_release(&loose_buf);
164
strbuf_release(&pack_buf);
165
strbuf_release(&garbage_buf);
builtin/receive-pack.c
+2
-1
@@ -359,7 +359,8 @@ static void write_head_info(void)
359
360
refs_for_each_fullref_in(get_main_ref_store(the_repository), "",
361
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);
364
365
oidset_clear(&seen);
366
strvec_clear(&excludes_vector);
builtin/submodule--helper.c
+2
-1
@@ -1668,7 +1668,8 @@ static void prepare_possible_alternates(const char *sm_name,
1668
die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1669
1670
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);
1673
else if (!strcmp(sm_alternate, "no"))
1674
; /* do nothing */
1675
else
diagnose.c
+1
-1
@@ -229,7 +229,7 @@ int create_diagnostics_archive(struct repository *r,
229
strbuf_reset(&buf);
230
strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
231
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);
233
strvec_push(&archiver_args, buf.buf);
234
235
strbuf_reset(&buf);
fetch-pack.c
+2
-1
@@ -115,7 +115,8 @@ static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
115
size_t i;
116
117
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);
120
initialized = 1;
121
}
122
odb.c
+19
-17
@@ -494,8 +494,8 @@ static void fill_alternate_refs_command(struct child_process *cmd,
494
}
495
496
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)
499
{
500
struct child_process cmd = CHILD_PROCESS_INIT;
501
struct strbuf line = STRBUF_INIT;
@@ -517,7 +517,7 @@ static void read_alternate_refs(const char *path,
517
break;
518
}
519
520
- cb(&oid, data);
520
+ cb(&oid, payload);
521
}
522
523
fclose(fh);
@@ -526,16 +526,16 @@ static void read_alternate_refs(const char *path,
526
}
527
528
struct alternate_refs_data {
529
- alternate_ref_fn *fn;
530
- void *data;
529
+ odb_for_each_alternate_ref_fn *fn;
530
+ void *payload;
531
};
532
533
static int refs_from_alternate_cb(struct odb_source *alternate,
534
- void *data)
534
+ void *payload)
535
{
536
struct strbuf path = STRBUF_INIT;
537
size_t base_len;
538
- struct alternate_refs_data *cb = data;
538
+ struct alternate_refs_data *cb = payload;
539
540
if (!strbuf_realpath(&path, alternate->path, 0))
541
goto out;
@@ -549,29 +549,31 @@ static int refs_from_alternate_cb(struct odb_source *alternate,
549
goto out;
550
strbuf_setlen(&path, base_len);
551
552
- read_alternate_refs(path.buf, cb->fn, cb->data);
552
+ read_alternate_refs(path.buf, cb->fn, cb->payload);
553
554
out:
555
strbuf_release(&path);
556
return 0;
557
}
558
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)
561
{
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);
566
}
567
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)
570
{
571
struct odb_source *alternate;
572
int r = 0;
573
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);
577
if (r)
578
break;
579
}
odb.h
+18
-5
@@ -73,11 +73,6 @@ struct odb_source {
73
char *path;
74
};
75
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
-
76
/*
77
* Replace the current writable object directory with the specified temporary
78
* object directory; returns the former primary object directory.
@@ -192,6 +187,24 @@ void odb_clear(struct object_database *o);
187
*/
188
struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
189
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
+
208
/*
209
* Create a temporary file rooted in the primary alternate's directory, or die
210
* on failure. The filename is taken from "pattern", which should have the
revision.c
+2
-1
@@ -1907,7 +1907,8 @@ static void add_alternate_refs_to_pending(struct rev_info *revs,
1907
struct add_alternate_refs_data data;
1908
data.revs = revs;
1909
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);
1912
}
1913
1914
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,