cgit

commit 181b6e789b79fc385e5ec75b2b83f01192d54051

Author: Lars Hjemli <hjemli@gmail.com>

Merge branch 'jp/defbranch'

 cgit.c | 15 ++++++++++++++-
 cgitrc.5.txt | 3 ++-
 shared.c | 1 -
 ui-repolist.c | 3 ++-


diff --git a/cgit.c b/cgit.c
index 7e3d349aa78a813e4547885aae46492220982c82..6a75f2706ff0c711e8c3f38ae20d3be867b858bd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -421,6 +421,17 @@ 		ref = xstrdup(ref);
 	return ref;
 }
 
+static char *guess_defbranch(const char *repo_path)
+{
+	const char *ref;
+	unsigned char sha1[20];
+
+	ref = resolve_ref("HEAD", sha1, 0, NULL);
+	if (!ref || prefixcmp(ref, "refs/heads/"))
+		return "master";
+	return xstrdup(ref + 11);
+}
+
 static int prepare_repo_cmd(struct cgit_context *ctx)
 {
 	char *tmp;
@@ -447,10 +458,12 @@ 		return 1;
 	}
 	ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
 
+	if (!ctx->repo->defbranch)
+		ctx->repo->defbranch = guess_defbranch(ctx->repo->path);
+
 	if (!ctx->qry.head) {
 		ctx->qry.nohead = 1;
 		ctx->qry.head = find_default_branch(ctx->repo);
-		ctx->repo->defbranch = ctx->qry.head;
 	}
 
 	if (!ctx->qry.head) {




diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index b4ad2acb5481e99fc592d400ea70c062f4bd4e75..fab0e0a60e5f27d9e15e0b00680906b7fec6e6f8 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -377,7 +377,8 @@
 repo.defbranch::
 	The name of the default branch for this repository. If no such branch
 	exists in the repository, the first branch name (when sorted) is used
-	as default instead. Default value: "master".
+	as default instead. Default value: branch pointed to by HEAD, or
+	"master" if there is no suitable HEAD.
 
 repo.desc::
 	The value to show as repository description. Default value: none.




diff --git a/shared.c b/shared.c
index cb52380916447eecb62b66957a1655dd6a6a0fd6..0a0e22e7255b0db8b9446c496fa85b58d46e24aa 100644
--- a/shared.c
+++ b/shared.c
@@ -55,7 +55,6 @@ 	ret->path = NULL;
 	ret->desc = "[no description]";
 	ret->owner = NULL;
 	ret->section = ctx.cfg.section;
-	ret->defbranch = "master";
 	ret->snapshots = ctx.cfg.snapshots;
 	ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
 	ret->enable_log_filecount = ctx.cfg.enable_log_filecount;




diff --git a/ui-repolist.c b/ui-repolist.c
index a09a6894442ca546b08b55f51f5ec2f3403b4d51..d946f32cd5354021c45637a0af24e1a7653dff92 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -45,7 +45,8 @@ 		r->mtime = *mtime;
 		return 1;
 	}
 
-	path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
+	path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
+		   repo->defbranch : "master");
 	if (stat(path, &s) == 0) {
 		*mtime = s.st_mtime;
 		r->mtime = *mtime;