Skip to content

Commit 39a9ef8

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4cc1ec commit 39a9ef8

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

refs.c

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,19 +400,29 @@ struct for_each_ref_filter {
400400
void *cb_data;
401401
};
402402

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)
404405
{
405-
struct ref_store *refs = get_main_ref_store(the_repository);
406-
407406
if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
408407
oid, flags))
409408
return 0;
410409
return -1;
411410
}
412411

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+
413423
int read_ref(const char *refname, struct object_id *oid)
414424
{
415-
return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
425+
return refs_read_ref(get_main_ref_store(the_repository), refname, oid);
416426
}
417427

418428
int refs_ref_exists(struct ref_store *refs, const char *refname)
@@ -542,21 +552,27 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)
542552
return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data);
543553
}
544554

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)
546556
{
547557
struct strbuf buf = STRBUF_INIT;
548558
int ret = 0;
549559
struct object_id oid;
550560
int flag;
551561

552562
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))
554564
ret = fn(buf.buf, &oid, flag, cb_data);
555565
strbuf_release(&buf);
556566

557567
return ret;
558568
}
559569

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+
560576
void normalize_glob_ref(struct string_list_item *item, const char *prefix,
561577
const char *pattern)
562578
{
@@ -583,8 +599,8 @@ void normalize_glob_ref(struct string_list_item *item, const char *prefix,
583599
strbuf_release(&normalized_pattern);
584600
}
585601

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)
588604
{
589605
struct strbuf real_pattern = STRBUF_INIT;
590606
struct for_each_ref_filter filter;
@@ -607,15 +623,29 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
607623
filter.prefix = prefix;
608624
filter.fn = fn;
609625
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);
611627

612628
strbuf_release(&real_pattern);
613629
return ret;
614630
}
615631

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+
616645
int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
617646
{
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);
619649
}
620650

621651
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
17331763
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
17341764
}
17351765

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)
17381769
{
17391770
struct strbuf buf = STRBUF_INIT;
17401771
int ret;
17411772
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);
17441774
strbuf_release(&buf);
17451775
return ret;
17461776
}
17471777

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+
17481785
int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
17491786
{
17501787
return do_for_each_ref(refs, "", NULL, fn, 0,

refs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ char *refs_resolve_refdup(struct ref_store *refs,
8181
char *resolve_refdup(const char *refname, int resolve_flags,
8282
struct object_id *oid, int *flags);
8383

84+
int refs_read_ref_full(struct ref_store *refs, const char *refname,
85+
int resolve_flags, struct object_id *oid, int *flags);
8486
int read_ref_full(const char *refname, int resolve_flags,
8587
struct object_id *oid, int *flags);
88+
89+
int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
8690
int read_ref(const char *refname, struct object_id *oid);
8791

8892
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);
375379
int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
376380

377381
/* 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);
378384
int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
379385

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);
380388
int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
381389
const char *prefix, void *cb_data);
382390

391+
int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data);
383392
int head_ref_namespaced(each_ref_fn fn, void *cb_data);
393+
384394
/*
385395
* references matching any pattern in "exclude_patterns" are omitted from the
386396
* result set on a best-effort basis.
387397
*/
398+
int refs_for_each_namespaced_ref(struct ref_store *refs,
399+
const char **exclude_patterns,
400+
each_ref_fn fn, void *cb_data);
388401
int for_each_namespaced_ref(const char **exclude_patterns,
389402
each_ref_fn fn, void *cb_data);
390403

0 commit comments

Comments
 (0)