Skip to content

Commit 4a6067c

Browse files
stefanbellergitster
authored andcommitted
refs.c: migrate internal ref iteration to pass thru repository argument
In 60ce76d (refs: add repository argument to for_each_replace_ref, 2018-04-11) and 0d296c5 (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>
1 parent 1689c22 commit 4a6067c

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

refs.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,17 +1386,50 @@ struct ref_iterator *refs_ref_iterator_begin(
13861386
* non-zero value, stop the iteration and return that value;
13871387
* otherwise, return 0.
13881388
*/
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+
13891420
static int do_for_each_ref(struct ref_store *refs, const char *prefix,
13901421
each_ref_fn fn, int trim, int flags, void *cb_data)
13911422
{
13921423
struct ref_iterator *iter;
1424+
struct do_for_each_ref_help hp = { fn, cb_data };
13931425

13941426
if (!refs)
13951427
return 0;
13961428

13971429
iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
13981430

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);
14001433
}
14011434

14021435
int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
@@ -2025,10 +2058,12 @@ int refs_verify_refname_available(struct ref_store *refs,
20252058
int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
20262059
{
20272060
struct ref_iterator *iter;
2061+
struct do_for_each_ref_help hp = { fn, cb_data };
20282062

20292063
iter = refs->be->reflog_iterator_begin(refs);
20302064

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);
20322067
}
20332068

20342069
int for_each_reflog(each_ref_fn fn, void *cb_data)

refs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ struct ref_transaction;
274274
typedef int each_ref_fn(const char *refname,
275275
const struct object_id *oid, int flags, void *cb_data);
276276

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+
277287
/*
278288
* The following functions invoke the specified callback function for
279289
* each reference indicated. If the function ever returns a nonzero

refs/iterator.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
407407

408408
struct ref_iterator *current_ref_iter = NULL;
409409

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)
412412
{
413413
int retval = 0, ok;
414414
struct ref_iterator *old_ref_iter = current_ref_iter;
415415

416416
current_ref_iter = iter;
417417
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);
419419
if (retval) {
420420
/*
421421
* If ref_iterator_abort() returns ITER_ERROR,

refs/refs-internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,9 @@ extern struct ref_iterator *current_ref_iter;
474474
* adapter between the callback style of reference iteration and the
475475
* iterator style.
476476
*/
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);
479480

480481
/*
481482
* Only include per-worktree refs in a do_for_each_ref*() iteration.

0 commit comments

Comments
 (0)