From c85ff532e9154920fc2bc3f6753a805615b924dc Mon Sep 17 00:00:00 2001
From: Ethan Koenig <ethantkoenig@gmail.com>
Date: Sun, 28 May 2017 22:25:23 -0400
Subject: [PATCH] Commitless repos should be bare

---
 models/action.go        | 2 +-
 modules/context/repo.go | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/models/action.go b/models/action.go
index a09e42066..6ca92cf93 100644
--- a/models/action.go
+++ b/models/action.go
@@ -517,7 +517,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
 	}
 
 	// Change repository bare status and update last updated time.
-	repo.IsBare = false
+	repo.IsBare = repo.IsBare && opts.Commits.Len <= 0
 	if err = UpdateRepository(repo, false); err != nil {
 		return fmt.Errorf("UpdateRepository: %v", err)
 	}
diff --git a/modules/context/repo.go b/modules/context/repo.go
index a59dc7da2..3b159a7aa 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -392,6 +392,10 @@ func RepoRef() macaron.Handler {
 				if err != nil {
 					ctx.Handle(500, "GetBranches", err)
 					return
+				} else if len(brs) == 0 {
+					err = fmt.Errorf("No branches in non-bare repository %s",
+						ctx.Repo.GitRepo.Path)
+					ctx.Handle(500, "GetBranches", err)
 				}
 				refName = brs[0]
 			}