diff mbox series

[1/2] refs: introduce the `refs_single_ref` function

Message ID 20231221170715.110565-2-karthik.188@gmail.com (mailing list archive)
State New, archived
Headers show
Series Initial changes to support printing all refs | expand

Commit Message

Karthik Nayak Dec. 21, 2023, 5:07 p.m. UTC
We currently provide the functionality to iterate over refs and call a
specific callback. This functionality currently supports iterating over
the entire "refs/*" space or directly the "HEAD" ref. This leaves
operational refs "ORIG_HEAD", "REBASE_HEAD" and so forth behind.

In the following commit, we introduce a mechanism to process all the
operational refs outside the "refs/*" space. To do this, we require a
function similar `refs_head_ref`, which can process a single specified
reference. This commit introduces `refs_single_ref` to fill in that gap.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 refs.c | 12 +++++++++---
 refs.h |  2 ++
 2 files changed, 11 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/refs.c b/refs.c
index 59fea0d44b..cebc5458d3 100644
--- a/refs.c
+++ b/refs.c
@@ -1549,18 +1549,24 @@  const char *find_descendant_ref(const char *dirname,
 	return NULL;
 }
 
-int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
+int refs_single_ref(struct ref_store *refs, const char *refname,
+					each_ref_fn fn, void *cb_data)
 {
 	struct object_id oid;
 	int flag;
 
-	if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
+	if (refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
 				    &oid, &flag))
-		return fn("HEAD", &oid, flag, cb_data);
+		return fn(refname, &oid, flag, cb_data);
 
 	return 0;
 }
 
+int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
+{
+	return refs_single_ref(refs, "HEAD", fn, cb_data);
+}
+
 int head_ref(each_ref_fn fn, void *cb_data)
 {
 	return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data);
diff --git a/refs.h b/refs.h
index 4816e46846..e147f13a85 100644
--- a/refs.h
+++ b/refs.h
@@ -324,6 +324,8 @@  typedef int each_repo_ref_fn(struct repository *r,
  * modifies the reference also returns a nonzero value to immediately
  * stop the iteration. Returned references are sorted.
  */
+int refs_single_ref(struct ref_store *refs, const char *refname,
+					each_ref_fn fn, void *cb_data);
 int refs_head_ref(struct ref_store *refs,
 		  each_ref_fn fn, void *cb_data);
 int refs_for_each_ref(struct ref_store *refs,