[PATCH v3 07/21] Add namespaced resolve ref helper

Richard Maw richard.maw at gmail.com
Mon Aug 1 23:35:39 CEST 2016


Unlike resolve_ref_unsafe, this does not return a substring or static buffer.
This is because the ref including the namespace can be longer.

Signed-off-by: Richard Maw <richard.maw at gmail.com>
---
 cgit.h   |  2 ++
 shared.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/cgit.h b/cgit.h
index 93de0ea..9129fd9 100644
--- a/cgit.h
+++ b/cgit.h
@@ -392,6 +392,8 @@ extern char *expand_macros(const char *txt);
 
 extern char *get_mimetype_for_filename(const char *filename);
 
+extern char *cgit_namespaced_resolve_ref(const char *name, int resolve_flags, unsigned char *sha1, int *flags);
+
 extern int cgit_get_sha1(const char *name, unsigned char *sha1);
 
 #endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index d82c07b..789fb5e 100644
--- a/shared.c
+++ b/shared.c
@@ -602,6 +602,26 @@ char *get_mimetype_for_filename(const char *filename)
 	return NULL;
 }
 
+char *cgit_namespaced_resolve_ref(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
+{
+	/* TODO: If libgit's resolve_ref_unsafe gains a flag for namespace support
+	         see if this function can be removed. */
+	const char *ref;
+	const char *namespace = get_git_namespace();
+	char *namespaced_ref = NULL;
+	char *ret = NULL;
+
+	if (namespace)
+		namespaced_ref = mkpathdup("%s%s", namespace, refname);
+
+	ref = resolve_ref_unsafe(namespaced_ref ?: refname, resolve_flags, sha1, flags);
+	if (ref)
+		ret = xstrdup(ref);
+
+	free(namespaced_ref);
+	return ret;
+}
+
 int cgit_get_sha1(const char *name, unsigned char *sha1)
 {
 	return get_sha1(name, sha1);
-- 
2.9.0



More information about the CGit mailing list