Commit aaf883177f27944f273eed3ea7cbe43075bb2741

Stefan Sperling 2019-07-10T18:08:23

introduce got_ref_alloc_symref() and got_ref_get_symref_target()

diff --git a/include/got_reference.h b/include/got_reference.h
index 019df06..15b4fba 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -43,12 +43,22 @@ const struct got_error *got_ref_open(struct got_reference **,
 const struct got_error *got_ref_alloc(struct got_reference **, const char *,
     struct got_object_id *);
 
+/*
+ * Allocate a new symbolic reference which points at a given reference.
+ * The caller must dispose of it with got_ref_close().
+ */
+const struct got_error *got_ref_alloc_symref(struct got_reference **,
+    const char *, struct got_reference *);
+
 /* Dispose of a reference. */
 void got_ref_close(struct got_reference *);
 
 /* Get the name of the reference. */
 const char *got_ref_get_name(struct got_reference *);
 
+/* Get the name of the reference which a symoblic reference points at. */
+const char *got_ref_get_symref_target(struct got_reference *);
+
 /*
  * Create a duplicate copy of a reference.
  * The caller must dispose of this copy with got_ref_close().
diff --git a/lib/reference.c b/lib/reference.c
index e572d68..6e1854a 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -296,6 +296,16 @@ got_ref_alloc(struct got_reference **ref, const char *name,
 	return alloc_ref(ref, name, id, 0);
 }
 
+const struct got_error *
+got_ref_alloc_symref(struct got_reference **ref, const char *name,
+	struct got_reference *target_ref)
+{
+	if (!is_valid_ref_name(name))
+		return got_error(GOT_ERR_BAD_REF_NAME);
+
+	return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
+}
+
 static const struct got_error *
 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
     const char *line)
@@ -599,6 +609,15 @@ got_ref_get_name(struct got_reference *ref)
 	return ref->ref.ref.name;
 }
 
+const char *
+got_ref_get_symref_target(struct got_reference *ref)
+{
+	if (ref->flags & GOT_REF_IS_SYMBOLIC)
+		return ref->ref.symref.ref;
+
+	return NULL;
+}
+
 static const struct got_error *
 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
     struct got_reference *ref, struct got_repository *repo)