Skip to content

Commit 8378c9d

Browse files
pks-tgitster
authored andcommitted
refs: convert iteration over replace refs to accept ref store
The function `for_each_replace_ref()` is a bit of an oddball across the refs interfaces as it accepts a pointer to the repository instead of a pointer to the ref store. The only reason for us to accept a repository is so that we can eventually pass it back to the callback function that the caller has provided. This is somewhat arbitrary though, as callers that need the repository can instead make it accessible via the callback payload. Refactor the function to instead accept the ref store and adjust callers accordingly. This allows us to get rid of some of the boilerplate that we had to carry to pass along the repository and brings us in line with the other functions that iterate through refs. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc7fb4f commit 8378c9d

File tree

6 files changed

+28
-81
lines changed

6 files changed

+28
-81
lines changed

builtin/replace.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ enum replace_format {
4343
};
4444

4545
struct show_data {
46+
struct repository *repo;
4647
const char *pattern;
4748
enum replace_format format;
4849
};
4950

50-
static int show_reference(struct repository *r, const char *refname,
51+
static int show_reference(const char *refname,
5152
const struct object_id *oid,
5253
int flag UNUSED, void *cb_data)
5354
{
@@ -62,11 +63,11 @@ static int show_reference(struct repository *r, const char *refname,
6263
struct object_id object;
6364
enum object_type obj_type, repl_type;
6465

65-
if (repo_get_oid(r, refname, &object))
66+
if (repo_get_oid(data->repo, refname, &object))
6667
return error(_("failed to resolve '%s' as a valid ref"), refname);
6768

68-
obj_type = oid_object_info(r, &object, NULL);
69-
repl_type = oid_object_info(r, oid, NULL);
69+
obj_type = oid_object_info(data->repo, &object, NULL);
70+
repl_type = oid_object_info(data->repo, oid, NULL);
7071

7172
printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
7273
oid_to_hex(oid), type_name(repl_type));
@@ -80,6 +81,7 @@ static int list_replace_refs(const char *pattern, const char *format)
8081
{
8182
struct show_data data;
8283

84+
data.repo = the_repository;
8385
if (!pattern)
8486
pattern = "*";
8587
data.pattern = pattern;
@@ -99,7 +101,8 @@ static int list_replace_refs(const char *pattern, const char *format)
99101
"valid formats are 'short', 'medium' and 'long'"),
100102
format);
101103

102-
for_each_replace_ref(the_repository, show_reference, (void *)&data);
104+
refs_for_each_replace_ref(get_main_ref_store(the_repository),
105+
show_reference, (void *)&data);
103106

104107
return 0;
105108
}

refs.c

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,62 +1576,20 @@ struct ref_iterator *refs_ref_iterator_begin(
15761576
return iter;
15771577
}
15781578

1579-
/*
1580-
* Call fn for each reference in the specified submodule for which the
1581-
* refname begins with prefix. If trim is non-zero, then trim that
1582-
* many characters off the beginning of each refname before passing
1583-
* the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1584-
* include broken references in the iteration. If fn ever returns a
1585-
* non-zero value, stop the iteration and return that value;
1586-
* otherwise, return 0.
1587-
*/
1588-
static int do_for_each_repo_ref(struct repository *r, const char *prefix,
1589-
each_repo_ref_fn fn, int trim, int flags,
1590-
void *cb_data)
1591-
{
1592-
struct ref_iterator *iter;
1593-
struct ref_store *refs = get_main_ref_store(r);
1594-
1595-
if (!refs)
1596-
return 0;
1597-
1598-
iter = refs_ref_iterator_begin(refs, prefix, NULL, trim, flags);
1599-
1600-
return do_for_each_repo_ref_iterator(r, iter, fn, cb_data);
1601-
}
1602-
1603-
struct do_for_each_ref_help {
1604-
each_ref_fn *fn;
1605-
void *cb_data;
1606-
};
1607-
1608-
static int do_for_each_ref_helper(struct repository *r UNUSED,
1609-
const char *refname,
1610-
const struct object_id *oid,
1611-
int flags,
1612-
void *cb_data)
1613-
{
1614-
struct do_for_each_ref_help *hp = cb_data;
1615-
1616-
return hp->fn(refname, oid, flags, hp->cb_data);
1617-
}
1618-
16191579
static int do_for_each_ref(struct ref_store *refs, const char *prefix,
16201580
const char **exclude_patterns,
16211581
each_ref_fn fn, int trim,
16221582
enum do_for_each_ref_flags flags, void *cb_data)
16231583
{
16241584
struct ref_iterator *iter;
1625-
struct do_for_each_ref_help hp = { fn, cb_data };
16261585

16271586
if (!refs)
16281587
return 0;
16291588

16301589
iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim,
16311590
flags);
16321591

1633-
return do_for_each_repo_ref_iterator(the_repository, iter,
1634-
do_for_each_ref_helper, &hp);
1592+
return do_for_each_ref_iterator(iter, fn, cb_data);
16351593
}
16361594

16371595
int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
@@ -1652,12 +1610,12 @@ int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
16521610
return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data);
16531611
}
16541612

1655-
int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
1613+
int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
16561614
{
16571615
const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
1658-
return do_for_each_repo_ref(r, git_replace_ref_base, fn,
1659-
strlen(git_replace_ref_base),
1660-
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1616+
return do_for_each_ref(refs, git_replace_ref_base, NULL, fn,
1617+
strlen(git_replace_ref_base),
1618+
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
16611619
}
16621620

16631621
int refs_for_each_namespaced_ref(struct ref_store *refs,
@@ -2425,8 +2383,7 @@ struct do_for_each_reflog_help {
24252383
void *cb_data;
24262384
};
24272385

2428-
static int do_for_each_reflog_helper(struct repository *r UNUSED,
2429-
const char *refname,
2386+
static int do_for_each_reflog_helper(const char *refname,
24302387
const struct object_id *oid UNUSED,
24312388
int flags,
24322389
void *cb_data)
@@ -2442,8 +2399,7 @@ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_dat
24422399

24432400
iter = refs->be->reflog_iterator_begin(refs);
24442401

2445-
return do_for_each_repo_ref_iterator(the_repository, iter,
2446-
do_for_each_reflog_helper, &hp);
2402+
return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp);
24472403
}
24482404

24492405
int refs_for_each_reflog_ent_reverse(struct ref_store *refs,

refs.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,6 @@ struct ref_transaction;
298298
typedef int each_ref_fn(const char *refname,
299299
const struct object_id *oid, int flags, void *cb_data);
300300

301-
/*
302-
* The same as each_ref_fn, but also with a repository argument that
303-
* contains the repository associated with the callback.
304-
*/
305-
typedef int each_repo_ref_fn(struct repository *r,
306-
const char *refname,
307-
const struct object_id *oid,
308-
int flags,
309-
void *cb_data);
310-
311301
/*
312302
* The following functions invoke the specified callback function for
313303
* each reference indicated. If the function ever returns a nonzero
@@ -329,6 +319,8 @@ int refs_for_each_branch_ref(struct ref_store *refs,
329319
each_ref_fn fn, void *cb_data);
330320
int refs_for_each_remote_ref(struct ref_store *refs,
331321
each_ref_fn fn, void *cb_data);
322+
int refs_for_each_replace_ref(struct ref_store *refs,
323+
each_ref_fn fn, void *cb_data);
332324

333325
/*
334326
* references matching any pattern in "exclude_patterns" are omitted from the
@@ -353,11 +345,6 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
353345
const char **exclude_patterns,
354346
each_ref_fn fn, void *cb_data);
355347

356-
/**
357-
* iterate refs from the respective area.
358-
*/
359-
int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
360-
361348
/* iterates all refs that match the specified glob pattern. */
362349
int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
363350
const char *pattern, void *cb_data);

refs/iterator.c

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

441441
struct ref_iterator *current_ref_iter = NULL;
442442

443-
int do_for_each_repo_ref_iterator(struct repository *r, struct ref_iterator *iter,
444-
each_repo_ref_fn fn, void *cb_data)
443+
int do_for_each_ref_iterator(struct ref_iterator *iter,
444+
each_ref_fn fn, void *cb_data)
445445
{
446446
int retval = 0, ok;
447447
struct ref_iterator *old_ref_iter = current_ref_iter;
448448

449449
current_ref_iter = iter;
450450
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
451-
retval = fn(r, iter->refname, iter->oid, iter->flags, cb_data);
451+
retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
452452
if (retval) {
453453
/*
454454
* If ref_iterator_abort() returns ITER_ERROR,

refs/refs-internal.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ extern struct ref_iterator *current_ref_iter;
503503
* adapter between the callback style of reference iteration and the
504504
* iterator style.
505505
*/
506-
int do_for_each_repo_ref_iterator(struct repository *r,
507-
struct ref_iterator *iter,
508-
each_repo_ref_fn fn, void *cb_data);
506+
int do_for_each_ref_iterator(struct ref_iterator *iter,
507+
each_ref_fn fn, void *cb_data);
509508

510509
struct ref_store;
511510

replace-object.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include "repository.h"
99
#include "commit.h"
1010

11-
static int register_replace_ref(struct repository *r,
12-
const char *refname,
11+
static int register_replace_ref(const char *refname,
1312
const struct object_id *oid,
1413
int flag UNUSED,
15-
void *cb_data UNUSED)
14+
void *cb_data)
1615
{
16+
struct repository *r = cb_data;
17+
1718
/* Get sha1 from refname */
1819
const char *slash = strrchr(refname, '/');
1920
const char *hash = slash ? slash + 1 : refname;
@@ -50,7 +51,8 @@ void prepare_replace_object(struct repository *r)
5051
xmalloc(sizeof(*r->objects->replace_map));
5152
oidmap_init(r->objects->replace_map, 0);
5253

53-
for_each_replace_ref(r, register_replace_ref, NULL);
54+
refs_for_each_replace_ref(get_main_ref_store(r),
55+
register_replace_ref, r);
5456
r->objects->replace_map_initialized = 1;
5557

5658
pthread_mutex_unlock(&r->objects->replace_mutex);

0 commit comments

Comments
 (0)