Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
798c661c c44185f6

+47 -28
+1 -1
builtin/count-objects.c
··· 159 159 printf("prune-packable: %lu\n", packed_loose); 160 160 printf("garbage: %lu\n", garbage); 161 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 163 strbuf_release(&loose_buf); 164 164 strbuf_release(&pack_buf); 165 165 strbuf_release(&garbage_buf);
+2 -1
builtin/receive-pack.c
··· 359 359 360 360 refs_for_each_fullref_in(get_main_ref_store(the_repository), "", 361 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); 363 364 364 365 oidset_clear(&seen); 365 366 strvec_clear(&excludes_vector);
+2 -1
builtin/submodule--helper.c
··· 1668 1668 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); 1669 1669 1670 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); 1672 1673 else if (!strcmp(sm_alternate, "no")) 1673 1674 ; /* do nothing */ 1674 1675 else
+1 -1
diagnose.c
··· 229 229 strbuf_reset(&buf); 230 230 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:"); 231 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 233 strvec_push(&archiver_args, buf.buf); 234 234 235 235 strbuf_reset(&buf);
+2 -1
fetch-pack.c
··· 115 115 size_t i; 116 116 117 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); 119 120 initialized = 1; 120 121 } 121 122
+19 -17
odb.c
··· 494 494 } 495 495 496 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 499 { 500 500 struct child_process cmd = CHILD_PROCESS_INIT; 501 501 struct strbuf line = STRBUF_INIT; ··· 517 517 break; 518 518 } 519 519 520 - cb(&oid, data); 520 + cb(&oid, payload); 521 521 } 522 522 523 523 fclose(fh); ··· 526 526 } 527 527 528 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 531 }; 532 532 533 533 static int refs_from_alternate_cb(struct odb_source *alternate, 534 - void *data) 534 + void *payload) 535 535 { 536 536 struct strbuf path = STRBUF_INIT; 537 537 size_t base_len; 538 - struct alternate_refs_data *cb = data; 538 + struct alternate_refs_data *cb = payload; 539 539 540 540 if (!strbuf_realpath(&path, alternate->path, 0)) 541 541 goto out; ··· 549 549 goto out; 550 550 strbuf_setlen(&path, base_len); 551 551 552 - read_alternate_refs(path.buf, cb->fn, cb->data); 552 + read_alternate_refs(path.buf, cb->fn, cb->payload); 553 553 554 554 out: 555 555 strbuf_release(&path); 556 556 return 0; 557 557 } 558 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) 560 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); 565 566 } 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) 568 570 { 569 571 struct odb_source *alternate; 570 572 int r = 0; 571 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); 575 577 if (r) 576 578 break; 577 579 }
+18 -5
odb.h
··· 73 73 char *path; 74 74 }; 75 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 - 81 76 /* 82 77 * Replace the current writable object directory with the specified temporary 83 78 * object directory; returns the former primary object directory. ··· 191 186 * be found. 192 187 */ 193 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); 194 207 195 208 /* 196 209 * Create a temporary file rooted in the primary alternate's directory, or die
+2 -1
revision.c
··· 1907 1907 struct add_alternate_refs_data data; 1908 1908 data.revs = revs; 1909 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); 1911 1912 } 1912 1913 1913 1914 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,