introduce got_ref_alloc_symref() and got_ref_get_symref_target()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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)