refs: introduce missing functions that accept a `struct ref_store`

While most of the functions in "refs.h" have a variant that accepts a `struct ref_store`, some don't. Callers of these functions are thus forced to implicitly rely on `the_repository` to figure out the ref store that is to be used. Introduce those missing functions to address this shortcoming. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 7, 2024 at 09:11 UTC 39a9ef8fc4e7dbcb078369f9c2560ebc69bbbb1d
2 files changed +64 -14
refs.c
+51 -14
@@ -400,19 +400,29 @@ struct for_each_ref_filter {
400 void *cb_data;
401 };
402
403 -int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
403 +int refs_read_ref_full(struct ref_store *refs, const char *refname,
404 + int resolve_flags, struct object_id *oid, int *flags)
405 {
405 - struct ref_store *refs = get_main_ref_store(the_repository);
406 -
406 if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
407 oid, flags))
408 return 0;
409 return -1;
410 }
411
412 +int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
413 +{
414 + return refs_read_ref_full(get_main_ref_store(the_repository), refname,
415 + resolve_flags, oid, flags);
416 +}
417 +
418 +int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid)
419 +{
420 + return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL);
421 +}
422 +
423 int read_ref(const char *refname, struct object_id *oid)
424 {
415 - return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
425 + return refs_read_ref(get_main_ref_store(the_repository), refname, oid);
426 }
427
428 int refs_ref_exists(struct ref_store *refs, const char *refname)
@@ -542,7 +552,7 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)
552 return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data);
553 }
554
545 -int head_ref_namespaced(each_ref_fn fn, void *cb_data)
555 +int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data)
556 {
557 struct strbuf buf = STRBUF_INIT;
558 int ret = 0;
@@ -550,13 +560,19 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
560 int flag;
561
562 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
553 - if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag))
563 + if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag))
564 ret = fn(buf.buf, &oid, flag, cb_data);
565 strbuf_release(&buf);
566
567 return ret;
568 }
569
570 +int head_ref_namespaced(each_ref_fn fn, void *cb_data)
571 +{
572 + return refs_head_ref_namespaced(get_main_ref_store(the_repository),
573 + fn, cb_data);
574 +}
575 +
576 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
577 const char *pattern)
578 {
@@ -583,8 +599,8 @@ void normalize_glob_ref(struct string_list_item *item, const char *prefix,
599 strbuf_release(&normalized_pattern);
600 }
601
586 -int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
587 - const char *prefix, void *cb_data)
602 +int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
603 + const char *pattern, const char *prefix, void *cb_data)
604 {
605 struct strbuf real_pattern = STRBUF_INIT;
606 struct for_each_ref_filter filter;
@@ -607,15 +623,29 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
623 filter.prefix = prefix;
624 filter.fn = fn;
625 filter.cb_data = cb_data;
610 - ret = for_each_ref(for_each_filter_refs, &filter);
626 + ret = refs_for_each_ref(refs, for_each_filter_refs, &filter);
627
628 strbuf_release(&real_pattern);
629 return ret;
630 }
631
632 +int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
633 + const char *prefix, void *cb_data)
634 +{
635 + return refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
636 + fn, pattern, prefix, cb_data);
637 +}
638 +
639 +int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
640 + const char *pattern, void *cb_data)
641 +{
642 + return refs_for_each_glob_ref_in(refs, fn, pattern, NULL, cb_data);
643 +}
644 +
645 int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
646 {
618 - return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
647 + return refs_for_each_glob_ref(get_main_ref_store(the_repository),
648 + fn, pattern, cb_data);
649 }
650
651 const char *prettify_refname(const char *name)
@@ -1733,18 +1763,25 @@ int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_dat
1763 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1764 }
1765
1736 -int for_each_namespaced_ref(const char **exclude_patterns,
1737 - each_ref_fn fn, void *cb_data)
1766 +int refs_for_each_namespaced_ref(struct ref_store *refs,
1767 + const char **exclude_patterns,
1768 + each_ref_fn fn, void *cb_data)
1769 {
1770 struct strbuf buf = STRBUF_INIT;
1771 int ret;
1772 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1742 - ret = do_for_each_ref(get_main_ref_store(the_repository),
1743 - buf.buf, exclude_patterns, fn, 0, 0, cb_data);
1773 + ret = do_for_each_ref(refs, buf.buf, exclude_patterns, fn, 0, 0, cb_data);
1774 strbuf_release(&buf);
1775 return ret;
1776 }
1777
1778 +int for_each_namespaced_ref(const char **exclude_patterns,
1779 + each_ref_fn fn, void *cb_data)
1780 +{
1781 + return refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
1782 + exclude_patterns, fn, cb_data);
1783 +}
1784 +
1785 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1786 {
1787 return do_for_each_ref(refs, "", NULL, fn, 0,
refs.h
+13
@@ -81,8 +81,12 @@ char *refs_resolve_refdup(struct ref_store *refs,
81 char *resolve_refdup(const char *refname, int resolve_flags,
82 struct object_id *oid, int *flags);
83
84 +int refs_read_ref_full(struct ref_store *refs, const char *refname,
85 + int resolve_flags, struct object_id *oid, int *flags);
86 int read_ref_full(const char *refname, int resolve_flags,
87 struct object_id *oid, int *flags);
88 +
89 +int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
90 int read_ref(const char *refname, struct object_id *oid);
91
92 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
@@ -375,16 +379,25 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data);
379 int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
380
381 /* iterates all refs that match the specified glob pattern. */
382 +int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
383 + const char *pattern, void *cb_data);
384 int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
385
386 +int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
387 + const char *pattern, const char *prefix, void *cb_data);
388 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
389 const char *prefix, void *cb_data);
390
391 +int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data);
392 int head_ref_namespaced(each_ref_fn fn, void *cb_data);
393 +
394 /*
395 * references matching any pattern in "exclude_patterns" are omitted from the
396 * result set on a best-effort basis.
397 */
398 +int refs_for_each_namespaced_ref(struct ref_store *refs,
399 + const char **exclude_patterns,
400 + each_ref_fn fn, void *cb_data);
401 int for_each_namespaced_ref(const char **exclude_patterns,
402 each_ref_fn fn, void *cb_data);
403