]> granicus.if.org Git - cgit/commitdiff
Guess the default branch based on current namespace
authorRichard Maw <richard.maw@gmail.com>
Sat, 25 Jun 2016 21:42:33 +0000 (22:42 +0100)
committerRichard Maw <richard.maw@gmail.com>
Wed, 13 Jul 2016 19:09:37 +0000 (20:09 +0100)
resolve_ref_unsafe() can't be told to be namespace aware,
so we need to prepend the namespace beforehand.

Additionally, we need to add the RESOLVE_REF_NO_RECURSE flag,
since otherwise if the commit that is pointed to exists in the root namespace,
it will opt to return that rather than the value in the namespace,
presumably preferring shorter ref names to longer ones.

Signed-off-by: Richard Maw <richard.maw@gmail.com>
cgit.c

diff --git a/cgit.c b/cgit.c
index eae2f3443e35825d9535762a1d381fc11b295e93..d9fe2b9df1641902610bb3df9a850a92b7e00123 100644 (file)
--- a/cgit.c
+++ b/cgit.c
@@ -474,11 +474,17 @@ static char *guess_defbranch(void)
 {
        const char *ref;
        unsigned char sha1[20];
-
-       ref = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
-       if (!ref || !starts_with(ref, "refs/heads/"))
+       char *namespaced_head = NULL;
+
+       if (get_git_namespace())
+               namespaced_head = mkpathdup("%sHEAD", get_git_namespace());
+       /* NOTE: RESOLVE_REF_NO_RECURSE is required to prevent it resolving HEAD
+          into a ref outside of the namespace. */
+       ref = resolve_ref_unsafe(namespaced_head ?: "HEAD", RESOLVE_REF_NO_RECURSE, sha1, NULL);
+       free(namespaced_head);
+       if (!ref || !starts_with(strip_namespace(ref), "refs/heads/"))
                return "master";
-       return xstrdup(ref + 11);
+       return xstrdup(strip_namespace(ref) + 11);
 }
 /* The caller must free filename and ref after calling this. */
 static inline void parse_readme(const char *readme, char **filename, char **ref, struct cgit_repo *repo)