refs.c: migrate internal ref iteration to pass thru repository argument

In 60ce76d3581 (refs: add repository argument to for_each_replace_ref, 2018-04-11) and 0d296c57aec (refs: allow for_each_replace_ref to handle arbitrary repositories, 2018-04-11), for_each_replace_ref learned how to iterate over refs by a given arbitrary repository. New attempts in the object store conversion have shown that it is useful to have the repository handle available that the refs iteration is currently iterating over. To achieve this goal we will need to add a repository argument to each_ref_fn in refs.h. However as many callers rely on the signature such a patch would be too large. So convert the internals of the ref subsystem first to pass through a repository argument without exposing the change to the user. Assume the_repository for the passed through repository, although it is not used anywhere yet. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Aug 20, 2018 at 18:24 UTC 4a6067cda51b592871144eb050d4673db9ad5103
4 files changed +53 -7
refs.c
+37 -2
@@ -1386,17 +1386,50 @@ struct ref_iterator *refs_ref_iterator_begin(
1386 * non-zero value, stop the iteration and return that value;
1387 * otherwise, return 0.
1388 */
1389 +static int do_for_each_repo_ref(struct repository *r, const char *prefix,
1390 + each_repo_ref_fn fn, int trim, int flags,
1391 + void *cb_data)
1392 +{
1393 + struct ref_iterator *iter;
1394 + struct ref_store *refs = get_main_ref_store(r);
1395 +
1396 + if (!refs)
1397 + return 0;
1398 +
1399 + iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
1400 +
1401 + return do_for_each_repo_ref_iterator(r, iter, fn, cb_data);
1402 +}
1403 +
1404 +struct do_for_each_ref_help {
1405 + each_ref_fn *fn;
1406 + void *cb_data;
1407 +};
1408 +
1409 +static int do_for_each_ref_helper(struct repository *r,
1410 + const char *refname,
1411 + const struct object_id *oid,
1412 + int flags,
1413 + void *cb_data)
1414 +{
1415 + struct do_for_each_ref_help *hp = cb_data;
1416 +
1417 + return hp->fn(refname, oid, flags, hp->cb_data);
1418 +}
1419 +
1420 static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1421 each_ref_fn fn, int trim, int flags, void *cb_data)
1422 {
1423 struct ref_iterator *iter;
1424 + struct do_for_each_ref_help hp = { fn, cb_data };
1425
1426 if (!refs)
1427 return 0;
1428
1429 iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
1430
1399 - return do_for_each_ref_iterator(iter, fn, cb_data);
1431 + return do_for_each_repo_ref_iterator(the_repository, iter,
1432 + do_for_each_ref_helper, &hp);
1433 }
1434
1435 int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
@@ -2025,10 +2058,12 @@ cleanup:
2058 int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2059 {
2060 struct ref_iterator *iter;
2061 + struct do_for_each_ref_help hp = { fn, cb_data };
2062
2063 iter = refs->be->reflog_iterator_begin(refs);
2064
2031 - return do_for_each_ref_iterator(iter, fn, cb_data);
2065 + return do_for_each_repo_ref_iterator(the_repository, iter,
2066 + do_for_each_ref_helper, &hp);
2067 }
2068
2069 int for_each_reflog(each_ref_fn fn, void *cb_data)
refs.h
+10
@@ -274,6 +274,16 @@ struct ref_transaction;
274 typedef int each_ref_fn(const char *refname,
275 const struct object_id *oid, int flags, void *cb_data);
276
277 +/*
278 + * The same as each_ref_fn, but also with a repository argument that
279 + * contains the repository associated with the callback.
280 + */
281 +typedef int each_repo_ref_fn(struct repository *r,
282 + const char *refname,
283 + const struct object_id *oid,
284 + int flags,
285 + void *cb_data);
286 +
287 /*
288 * The following functions invoke the specified callback function for
289 * each reference indicated. If the function ever returns a nonzero
refs/iterator.c
+3 -3
@@ -407,15 +407,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
407
408 struct ref_iterator *current_ref_iter = NULL;
409
410 -int do_for_each_ref_iterator(struct ref_iterator *iter,
411 - each_ref_fn fn, void *cb_data)
410 +int do_for_each_repo_ref_iterator(struct repository *r, struct ref_iterator *iter,
411 + each_repo_ref_fn fn, void *cb_data)
412 {
413 int retval = 0, ok;
414 struct ref_iterator *old_ref_iter = current_ref_iter;
415
416 current_ref_iter = iter;
417 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
418 - retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
418 + retval = fn(r, iter->refname, iter->oid, iter->flags, cb_data);
419 if (retval) {
420 /*
421 * If ref_iterator_abort() returns ITER_ERROR,
refs/refs-internal.h
+3 -2
@@ -474,8 +474,9 @@ extern struct ref_iterator *current_ref_iter;
474 * adapter between the callback style of reference iteration and the
475 * iterator style.
476 */
477 -int do_for_each_ref_iterator(struct ref_iterator *iter,
478 - each_ref_fn fn, void *cb_data);
477 +int do_for_each_repo_ref_iterator(struct repository *r,
478 + struct ref_iterator *iter,
479 + each_repo_ref_fn fn, void *cb_data);
480
481 /*
482 * Only include per-worktree refs in a do_for_each_ref*() iteration.