Skip to content

Commit d445aec

Browse files
committed
Merge branch 'ps/refs-for-each'
Code refactoring around refs-for-each-* API functions. * ps/refs-for-each: refs: replace `refs_for_each_fullref_in()` refs: replace `refs_for_each_namespaced_ref()` refs: replace `refs_for_each_glob_ref()` refs: replace `refs_for_each_glob_ref_in()` refs: replace `refs_for_each_rawref_in()` refs: replace `refs_for_each_rawref()` refs: replace `refs_for_each_ref_in()` refs: improve verification for-each-ref options refs: generalize `refs_for_each_fullref_in_prefixes()` refs: generalize `refs_for_each_namespaced_ref()` refs: speed up `refs_for_each_glob_ref_in()` refs: introduce `refs_for_each_ref_ext` refs: rename `each_ref_fn` refs: rename `do_for_each_ref_flags` refs: move `do_for_each_ref_flags` further up refs: move `refs_head_ref_namespaced()` refs: remove unused `refs_for_each_include_root_ref()`
2 parents 5c56c72 + 1dd4f1e commit d445aec

28 files changed

Lines changed: 457 additions & 361 deletions

bisect.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,12 @@ static int register_ref(const struct reference *ref, void *cb_data UNUSED)
473473

474474
static int read_bisect_refs(void)
475475
{
476-
return refs_for_each_ref_in(get_main_ref_store(the_repository),
477-
"refs/bisect/", register_ref, NULL);
476+
struct refs_for_each_ref_options opts = {
477+
.prefix = "refs/bisect/",
478+
.trim_prefix = strlen("refs/bisect/"),
479+
};
480+
return refs_for_each_ref_ext(get_main_ref_store(the_repository),
481+
register_ref, NULL, &opts);
478482
}
479483

480484
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
@@ -1186,13 +1190,15 @@ static int mark_for_removal(const struct reference *ref, void *cb_data)
11861190

11871191
int bisect_clean_state(void)
11881192
{
1193+
struct refs_for_each_ref_options opts = {
1194+
.prefix = "refs/bisect/",
1195+
};
11891196
int result = 0;
11901197

11911198
/* There may be some refs packed during bisection */
11921199
struct string_list refs_for_removal = STRING_LIST_INIT_DUP;
1193-
refs_for_each_fullref_in(get_main_ref_store(the_repository),
1194-
"refs/bisect/", NULL, mark_for_removal,
1195-
&refs_for_removal);
1200+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
1201+
mark_for_removal, &refs_for_removal, &opts);
11961202
string_list_append(&refs_for_removal, "BISECT_HEAD");
11971203
string_list_append(&refs_for_removal, "BISECT_EXPECTED_REV");
11981204
result = refs_delete_refs(get_main_ref_store(the_repository),

builtin/bisect.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,17 @@ static void bisect_status(struct bisect_state *state,
422422
{
423423
char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
424424
char *good_glob = xstrfmt("%s-*", terms->term_good);
425+
struct refs_for_each_ref_options opts = {
426+
.pattern = good_glob,
427+
.prefix = "refs/bisect/",
428+
.trim_prefix = strlen("refs/bisect/"),
429+
};
425430

426431
if (refs_ref_exists(get_main_ref_store(the_repository), bad_ref))
427432
state->nr_bad = 1;
428433

429-
refs_for_each_glob_ref_in(get_main_ref_store(the_repository), inc_nr,
430-
good_glob, "refs/bisect/",
431-
(void *) &state->nr_good);
434+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
435+
inc_nr, &state->nr_good, &opts);
432436

433437
free(good_glob);
434438
free(bad_ref);
@@ -562,6 +566,10 @@ static int add_bisect_ref(const struct reference *ref, void *cb)
562566

563567
static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
564568
{
569+
struct refs_for_each_ref_options opts = {
570+
.prefix = "refs/bisect/",
571+
.trim_prefix = strlen("refs/bisect/"),
572+
};
565573
int res = 0;
566574
struct add_bisect_ref_data cb = { revs };
567575
char *good = xstrfmt("%s-*", terms->term_good);
@@ -581,11 +589,16 @@ static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
581589
reset_revision_walk();
582590
repo_init_revisions(the_repository, revs, NULL);
583591
setup_revisions(0, NULL, revs, NULL);
584-
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
585-
add_bisect_ref, bad, "refs/bisect/", &cb);
592+
593+
opts.pattern = bad;
594+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
595+
add_bisect_ref, &cb, &opts);
596+
586597
cb.object_flags = UNINTERESTING;
587-
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
588-
add_bisect_ref, good, "refs/bisect/", &cb);
598+
opts.pattern = good;
599+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
600+
add_bisect_ref, &cb, &opts);
601+
589602
if (prepare_revision_walk(revs))
590603
res = error(_("revision walk setup failed"));
591604

@@ -1191,10 +1204,14 @@ static int verify_good(const struct bisect_terms *terms, const char *command)
11911204
char *good_glob = xstrfmt("%s-*", terms->term_good);
11921205
int no_checkout = refs_ref_exists(get_main_ref_store(the_repository),
11931206
"BISECT_HEAD");
1207+
struct refs_for_each_ref_options opts = {
1208+
.pattern = good_glob,
1209+
.prefix = "refs/bisect/",
1210+
.trim_prefix = strlen("refs/bisect/"),
1211+
};
11941212

1195-
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
1196-
get_first_good, good_glob, "refs/bisect/",
1197-
&good_rev);
1213+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
1214+
get_first_good, &good_rev, &opts);
11981215
free(good_glob);
11991216

12001217
if (refs_read_ref(get_main_ref_store(the_repository), no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))

builtin/describe.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ int cmd_describe(int argc,
641641
const char *prefix,
642642
struct repository *repo UNUSED )
643643
{
644+
struct refs_for_each_ref_options for_each_ref_opts = {
645+
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
646+
};
644647
int contains = 0;
645648
struct option options[] = {
646649
OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
@@ -738,8 +741,8 @@ int cmd_describe(int argc,
738741
}
739742

740743
hashmap_init(&names, commit_name_neq, NULL, 0);
741-
refs_for_each_rawref(get_main_ref_store(the_repository), get_name,
742-
NULL);
744+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
745+
get_name, NULL, &for_each_ref_opts);
743746
if (!hashmap_get_size(&names) && !always)
744747
die(_("No names found, cannot describe anything."));
745748

builtin/fetch.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,9 @@ static void add_negotiation_tips(struct git_transport_options *smart_options)
15411541

15421542
for (i = 0; i < negotiation_tip.nr; i++) {
15431543
const char *s = negotiation_tip.items[i].string;
1544+
struct refs_for_each_ref_options opts = {
1545+
.pattern = s,
1546+
};
15441547
int old_nr;
15451548
if (!has_glob_specials(s)) {
15461549
struct object_id oid;
@@ -1552,8 +1555,8 @@ static void add_negotiation_tips(struct git_transport_options *smart_options)
15521555
continue;
15531556
}
15541557
old_nr = oids->nr;
1555-
refs_for_each_glob_ref(get_main_ref_store(the_repository),
1556-
add_oid, s, oids);
1558+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
1559+
add_oid, oids, &opts);
15571560
if (old_nr == oids->nr)
15581561
warning("ignoring --negotiation-tip=%s because it does not match any refs",
15591562
s);

builtin/fsck.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,9 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
582582

583583
static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
584584
{
585+
struct refs_for_each_ref_options opts = {
586+
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
587+
};
585588
struct worktree **worktrees, **p;
586589
const char *head_points_at;
587590
struct object_id head_oid;
@@ -607,8 +610,8 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
607610
return;
608611
}
609612

610-
refs_for_each_rawref(get_main_ref_store(the_repository),
611-
snapshot_ref, snap);
613+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
614+
snapshot_ref, snap, &opts);
612615

613616
worktrees = get_worktrees();
614617
for (p = worktrees; *p; p++) {

builtin/receive-pack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,22 +343,22 @@ static void show_one_alternate_ref(const struct object_id *oid,
343343

344344
static void write_head_info(void)
345345
{
346+
struct refs_for_each_ref_options opts = { 0 };
346347
static struct oidset seen = OIDSET_INIT;
347348
struct strvec excludes_vector = STRVEC_INIT;
348-
const char **exclude_patterns;
349349

350350
/*
351351
* We need access to the reference names both with and without their
352352
* namespace and thus cannot use `refs_for_each_namespaced_ref()`. We
353353
* thus have to adapt exclude patterns to carry the namespace prefix
354354
* ourselves.
355355
*/
356-
exclude_patterns = get_namespaced_exclude_patterns(
356+
opts.exclude_patterns = get_namespaced_exclude_patterns(
357357
hidden_refs_to_excludes(&hidden_refs),
358358
get_git_namespace(), &excludes_vector);
359359

360-
refs_for_each_fullref_in(get_main_ref_store(the_repository), "",
361-
exclude_patterns, show_ref_cb, &seen);
360+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
361+
show_ref_cb, &seen, &opts);
362362
odb_for_each_alternate_ref(the_repository->objects,
363363
show_one_alternate_ref, &seen);
364364

builtin/remote.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ static int mv(int argc, const char **argv, const char *prefix,
912912
old_remote_context.buf);
913913

914914
if (refspecs_need_update) {
915+
struct refs_for_each_ref_options opts = {
916+
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
917+
};
915918
rename.transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
916919
0, &err);
917920
if (!rename.transaction)
@@ -923,9 +926,10 @@ static int mv(int argc, const char **argv, const char *prefix,
923926

924927
strbuf_reset(&buf);
925928
strbuf_addf(&buf, "refs/remotes/%s/", rename.old_name);
929+
opts.prefix = buf.buf;
926930

927-
result = refs_for_each_rawref_in(get_main_ref_store(the_repository), buf.buf,
928-
rename_one_ref, &rename);
931+
result = refs_for_each_ref_ext(get_main_ref_store(the_repository),
932+
rename_one_ref, &rename, &opts);
929933
if (result < 0)
930934
die(_("queueing remote ref renames failed: %s"), rename.err->buf);
931935

builtin/rev-parse.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,22 @@ static int opt_with_value(const char *arg, const char *opt, const char **value)
613613

614614
static void handle_ref_opt(const char *pattern, const char *prefix)
615615
{
616-
if (pattern)
617-
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
618-
show_reference, pattern, prefix,
619-
NULL);
620-
else
621-
refs_for_each_ref_in(get_main_ref_store(the_repository),
622-
prefix, show_reference, NULL);
616+
if (pattern) {
617+
struct refs_for_each_ref_options opts = {
618+
.pattern = pattern,
619+
.prefix = prefix,
620+
.trim_prefix = prefix ? strlen(prefix) : 0,
621+
};
622+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
623+
show_reference, NULL, &opts);
624+
} else {
625+
struct refs_for_each_ref_options opts = {
626+
.prefix = prefix,
627+
.trim_prefix = strlen(prefix),
628+
};
629+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
630+
show_reference, NULL, &opts);
631+
}
623632
clear_ref_exclusions(&ref_excludes);
624633
}
625634

@@ -931,14 +940,13 @@ int cmd_rev_parse(int argc,
931940
continue;
932941
}
933942
if (!strcmp(arg, "--bisect")) {
934-
refs_for_each_fullref_in(get_main_ref_store(the_repository),
935-
"refs/bisect/bad",
936-
NULL, show_reference,
937-
NULL);
938-
refs_for_each_fullref_in(get_main_ref_store(the_repository),
939-
"refs/bisect/good",
940-
NULL, anti_reference,
941-
NULL);
943+
struct refs_for_each_ref_options opts = { 0 };
944+
opts.prefix = "refs/bisect/bad";
945+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
946+
show_reference, NULL, &opts);
947+
opts.prefix = "refs/bisect/good";
948+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
949+
anti_reference, NULL, &opts);
942950
continue;
943951
}
944952
if (opt_with_value(arg, "--branches", &arg)) {

builtin/show-ref.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,19 @@ static int cmd_show_ref__patterns(const struct patterns_options *opts,
215215
refs_head_ref(get_main_ref_store(the_repository), show_ref,
216216
&show_ref_data);
217217
if (opts->branches_only || opts->tags_only) {
218-
if (opts->branches_only)
219-
refs_for_each_fullref_in(get_main_ref_store(the_repository),
220-
"refs/heads/", NULL,
221-
show_ref, &show_ref_data);
222-
if (opts->tags_only)
223-
refs_for_each_fullref_in(get_main_ref_store(the_repository),
224-
"refs/tags/", NULL, show_ref,
225-
&show_ref_data);
218+
struct refs_for_each_ref_options for_each_ref_opts = { 0 };
219+
220+
if (opts->branches_only) {
221+
for_each_ref_opts.prefix = "refs/heads/";
222+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
223+
show_ref, &show_ref_data, &for_each_ref_opts);
224+
}
225+
226+
if (opts->tags_only) {
227+
for_each_ref_opts.prefix = "refs/tags/";
228+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
229+
show_ref, &show_ref_data, &for_each_ref_opts);
230+
}
226231
} else {
227232
refs_for_each_ref(get_main_ref_store(the_repository),
228233
show_ref, &show_ref_data);

fetch-pack.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,14 @@ static int next_flush(int stateless_rpc, int count)
293293
static void mark_tips(struct fetch_negotiator *negotiator,
294294
const struct oid_array *negotiation_tips)
295295
{
296+
struct refs_for_each_ref_options opts = {
297+
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
298+
};
296299
int i;
297300

298301
if (!negotiation_tips) {
299-
refs_for_each_rawref(get_main_ref_store(the_repository),
300-
rev_list_insert_ref_oid, negotiator);
302+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
303+
rev_list_insert_ref_oid, negotiator, &opts);
301304
return;
302305
}
303306

@@ -793,8 +796,12 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
793796
*/
794797
trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
795798
if (!args->deepen) {
796-
refs_for_each_rawref(get_main_ref_store(the_repository),
797-
mark_complete_oid, NULL);
799+
struct refs_for_each_ref_options opts = {
800+
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
801+
};
802+
803+
refs_for_each_ref_ext(get_main_ref_store(the_repository),
804+
mark_complete_oid, NULL, &opts);
798805
for_each_cached_alternate(NULL, mark_alternate_complete);
799806
if (cutoff)
800807
mark_recent_complete_commits(args, cutoff);

0 commit comments

Comments
 (0)