Skip to content

Commit 31f8983

Browse files
pks-tgitster
authored andcommitted
refs: drop unused params from the reflog iterator callback
The ref and reflog iterators share much of the same underlying code to iterate over the corresponding entries. This results in some weird code because the reflog iterator also exposes an object ID as well as a flag to the callback function. Neither of these fields do refer to the reflog though -- they refer to the corresponding ref with the same name. This is quite misleading. In practice at least the object ID cannot really be implemented in any other way as a reflog does not have a specific object ID in the first place. This is further stressed by the fact that none of the callbacks except for our test helper make use of these fields. Split up the infrastucture so that ref and reflog iterators use separate callback signatures. This allows us to drop the nonsensical fields from the reflog iterator. Note that internally, the backends still use the same shared infra to iterate over both types. As the backends should never end up being called directly anyway, this is not much of a problem and thus kept as-is for simplicity's sake. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e01d83 commit 31f8983

File tree

11 files changed

+65
-54
lines changed

11 files changed

+65
-54
lines changed

builtin/fsck.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,7 @@ static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid
509509
return 0;
510510
}
511511

512-
static int fsck_handle_reflog(const char *logname,
513-
const struct object_id *oid UNUSED,
514-
int flag UNUSED, void *cb_data)
512+
static int fsck_handle_reflog(const char *logname, void *cb_data)
515513
{
516514
struct strbuf refname = STRBUF_INIT;
517515

builtin/reflog.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ struct worktree_reflogs {
6060
struct string_list reflogs;
6161
};
6262

63-
static int collect_reflog(const char *ref, const struct object_id *oid UNUSED,
64-
int flags UNUSED, void *cb_data)
63+
static int collect_reflog(const char *ref, void *cb_data)
6564
{
6665
struct worktree_reflogs *cb = cb_data;
6766
struct worktree *worktree = cb->worktree;

refs.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,18 +2512,33 @@ int refs_verify_refname_available(struct ref_store *refs,
25122512
return ret;
25132513
}
25142514

2515-
int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2515+
struct do_for_each_reflog_help {
2516+
each_reflog_fn *fn;
2517+
void *cb_data;
2518+
};
2519+
2520+
static int do_for_each_reflog_helper(struct repository *r UNUSED,
2521+
const char *refname,
2522+
const struct object_id *oid UNUSED,
2523+
int flags,
2524+
void *cb_data)
2525+
{
2526+
struct do_for_each_reflog_help *hp = cb_data;
2527+
return hp->fn(refname, hp->cb_data);
2528+
}
2529+
2530+
int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data)
25162531
{
25172532
struct ref_iterator *iter;
2518-
struct do_for_each_ref_help hp = { fn, cb_data };
2533+
struct do_for_each_reflog_help hp = { fn, cb_data };
25192534

25202535
iter = refs->be->reflog_iterator_begin(refs);
25212536

25222537
return do_for_each_repo_ref_iterator(the_repository, iter,
2523-
do_for_each_ref_helper, &hp);
2538+
do_for_each_reflog_helper, &hp);
25242539
}
25252540

2526-
int for_each_reflog(each_ref_fn fn, void *cb_data)
2541+
int for_each_reflog(each_reflog_fn fn, void *cb_data)
25272542
{
25282543
return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data);
25292544
}

refs.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,19 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat
534534
/* youngest entry first */
535535
int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);
536536

537+
/*
538+
* The signature for the callback function for the {refs_,}for_each_reflog()
539+
* functions below. The memory pointed to by the refname argument is only
540+
* guaranteed to be valid for the duration of a single callback invocation.
541+
*/
542+
typedef int each_reflog_fn(const char *refname, void *cb_data);
543+
537544
/*
538545
* Calls the specified function for each reflog file until it returns nonzero,
539546
* and returns the value. Reflog file order is unspecified.
540547
*/
541-
int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data);
542-
int for_each_reflog(each_ref_fn fn, void *cb_data);
548+
int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
549+
int for_each_reflog(each_reflog_fn fn, void *cb_data);
543550

544551
#define REFNAME_ALLOW_ONELEVEL 1
545552
#define REFNAME_REFSPEC_PATTERN 2

refs/files-backend.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,10 +2115,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
21152115

21162116
struct files_reflog_iterator {
21172117
struct ref_iterator base;
2118-
21192118
struct ref_store *ref_store;
21202119
struct dir_iterator *dir_iterator;
2121-
struct object_id oid;
21222120
};
21232121

21242122
static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
@@ -2129,8 +2127,6 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
21292127
int ok;
21302128

21312129
while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2132-
int flags;
2133-
21342130
if (!S_ISREG(diter->st.st_mode))
21352131
continue;
21362132
if (diter->basename[0] == '.')
@@ -2140,14 +2136,12 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
21402136

21412137
if (!refs_resolve_ref_unsafe(iter->ref_store,
21422138
diter->relative_path, 0,
2143-
&iter->oid, &flags)) {
2139+
NULL, NULL)) {
21442140
error("bad ref for %s", diter->path.buf);
21452141
continue;
21462142
}
21472143

21482144
iter->base.refname = diter->relative_path;
2149-
iter->base.oid = &iter->oid;
2150-
iter->base.flags = flags;
21512145
return ITER_OK;
21522146
}
21532147

refs/reftable-backend.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,6 @@ struct reftable_reflog_iterator {
15941594
struct reftable_ref_store *refs;
15951595
struct reftable_iterator iter;
15961596
struct reftable_log_record log;
1597-
struct object_id oid;
15981597
char *last_name;
15991598
int err;
16001599
};
@@ -1605,8 +1604,6 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
16051604
(struct reftable_reflog_iterator *)ref_iterator;
16061605

16071606
while (!iter->err) {
1608-
int flags;
1609-
16101607
iter->err = reftable_iterator_next_log(&iter->iter, &iter->log);
16111608
if (iter->err)
16121609
break;
@@ -1620,16 +1617,14 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
16201617
continue;
16211618

16221619
if (!refs_resolve_ref_unsafe(&iter->refs->base, iter->log.refname,
1623-
0, &iter->oid, &flags)) {
1620+
0, NULL, NULL)) {
16241621
error(_("bad ref for %s"), iter->log.refname);
16251622
continue;
16261623
}
16271624

16281625
free(iter->last_name);
16291626
iter->last_name = xstrdup(iter->log.refname);
16301627
iter->base.refname = iter->log.refname;
1631-
iter->base.oid = &iter->oid;
1632-
iter->base.flags = flags;
16331628

16341629
break;
16351630
}
@@ -1682,7 +1677,6 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl
16821677
iter = xcalloc(1, sizeof(*iter));
16831678
base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable);
16841679
iter->refs = refs;
1685-
iter->base.oid = &iter->oid;
16861680

16871681
ret = refs->err;
16881682
if (ret)

revision.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,9 +1686,7 @@ static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
16861686
return 0;
16871687
}
16881688

1689-
static int handle_one_reflog(const char *refname_in_wt,
1690-
const struct object_id *oid UNUSED,
1691-
int flag UNUSED, void *cb_data)
1689+
static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
16921690
{
16931691
struct all_refs_cb *cb = cb_data;
16941692
struct strbuf refname = STRBUF_INIT;

t/helper/test-ref-store.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,21 @@ static int cmd_verify_ref(struct ref_store *refs, const char **argv)
221221
return ret;
222222
}
223223

224+
static int each_reflog(const char *refname, void *cb_data UNUSED)
225+
{
226+
printf("%s\n", refname);
227+
return 0;
228+
}
229+
224230
static int cmd_for_each_reflog(struct ref_store *refs,
225231
const char **argv UNUSED)
226232
{
227-
return refs_for_each_reflog(refs, each_ref, NULL);
233+
return refs_for_each_reflog(refs, each_reflog, NULL);
228234
}
229235

230-
static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
231-
const char *committer, timestamp_t timestamp,
232-
int tz, const char *msg, void *cb_data UNUSED)
236+
static int each_reflog_ent(struct object_id *old_oid, struct object_id *new_oid,
237+
const char *committer, timestamp_t timestamp,
238+
int tz, const char *msg, void *cb_data UNUSED)
233239
{
234240
printf("%s %s %s %" PRItime " %+05d%s%s", oid_to_hex(old_oid),
235241
oid_to_hex(new_oid), committer, timestamp, tz,
@@ -241,14 +247,14 @@ static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
241247
{
242248
const char *refname = notnull(*argv++, "refname");
243249

244-
return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
250+
return refs_for_each_reflog_ent(refs, refname, each_reflog_ent, refs);
245251
}
246252

247253
static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
248254
{
249255
const char *refname = notnull(*argv++, "refname");
250256

251-
return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
257+
return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog_ent, refs);
252258
}
253259

254260
static int cmd_reflog_exists(struct ref_store *refs, const char **argv)

t/t0600-reffiles-backend.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,23 @@ test_expect_success 'for_each_reflog()' '
287287
mkdir -p .git/worktrees/wt/logs/refs/bisect &&
288288
echo $ZERO_OID > .git/worktrees/wt/logs/refs/bisect/wt-random &&
289289
290-
$RWT for-each-reflog | cut -d" " -f 2- >actual &&
290+
$RWT for-each-reflog >actual &&
291291
cat >expected <<-\EOF &&
292-
HEAD 0x1
293-
PSEUDO-WT 0x0
294-
refs/bisect/wt-random 0x0
295-
refs/heads/main 0x0
296-
refs/heads/wt-main 0x0
292+
HEAD
293+
PSEUDO-WT
294+
refs/bisect/wt-random
295+
refs/heads/main
296+
refs/heads/wt-main
297297
EOF
298298
test_cmp expected actual &&
299299
300-
$RMAIN for-each-reflog | cut -d" " -f 2- >actual &&
300+
$RMAIN for-each-reflog >actual &&
301301
cat >expected <<-\EOF &&
302-
HEAD 0x1
303-
PSEUDO-MAIN 0x0
304-
refs/bisect/random 0x0
305-
refs/heads/main 0x0
306-
refs/heads/wt-main 0x0
302+
HEAD
303+
PSEUDO-MAIN
304+
refs/bisect/random
305+
refs/heads/main
306+
refs/heads/wt-main
307307
EOF
308308
test_cmp expected actual
309309
'

t/t1405-main-ref-store.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ test_expect_success 'verify_ref(new-main)' '
7474
'
7575

7676
test_expect_success 'for_each_reflog()' '
77-
$RUN for-each-reflog | cut -d" " -f 2- >actual &&
77+
$RUN for-each-reflog >actual &&
7878
cat >expected <<-\EOF &&
79-
HEAD 0x1
80-
refs/heads/main 0x0
81-
refs/heads/new-main 0x0
79+
HEAD
80+
refs/heads/main
81+
refs/heads/new-main
8282
EOF
8383
test_cmp expected actual
8484
'

0 commit comments

Comments
 (0)