From 94392a7af3d07f79adab195acfb7c1b545f68be9 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Mon, 15 Aug 2016 05:53:47 -0700
Subject: [PATCH] Fix empty repository panic on send test webhook

---
 .bra.toml               |  2 +-
 .gopmfile               |  2 +-
 glide.lock              |  2 +-
 gogs.go                 |  2 +-
 models/action.go        |  5 -----
 models/issue.go         | 18 ++----------------
 routers/repo/repo.go    |  2 +-
 routers/repo/webhook.go | 30 +++++++++++++++++++++---------
 8 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/.bra.toml b/.bra.toml
index a25a22410f..ed0fbd2259 100644
--- a/.bra.toml
+++ b/.bra.toml
@@ -14,6 +14,6 @@ watch_exts = [".go"]
 ignore_files = [".+_test.go"]
 build_delay = 1500
 cmds = [
-	["make", "build-dev", "TAGS=sqlite"], # cert pam tidb
+	["make", "build-dev"], # TAGS=sqlite cert pam tidb
 	["./gogs", "web"]
 ]
\ No newline at end of file
diff --git a/.gopmfile b/.gopmfile
index 9d80bca4bb..7c2420931e 100644
--- a/.gopmfile
+++ b/.gopmfile
@@ -18,7 +18,7 @@ github.com/go-xorm/core = commit:5bf745d
 github.com/go-xorm/xorm = commit:c6c7056
 github.com/gogits/chardet = commit:2404f77
 github.com/gogits/cron = commit:7f3990a
-github.com/gogits/git-module = commit:313ce90
+github.com/gogits/git-module = commit:31d8d73
 github.com/gogits/go-gogs-client = commit:e363d3f
 github.com/issue9/identicon = commit:d36b545
 github.com/jaytaylor/html2text = commit:52d9b78
diff --git a/glide.lock b/glide.lock
index 520f8dad1c..9d720077f5 100644
--- a/glide.lock
+++ b/glide.lock
@@ -41,7 +41,7 @@ imports:
 - name: github.com/gogits/cron
   version: 7f3990acf1833faa5ebd0e86f0a4c72a4b5eba3c
 - name: github.com/gogits/git-module
-  version: 313ce907cc5a5801126c7dfa3a74d21cc3104329
+  version: 31d8d73910d6f9ccb94ba6c17bbd18b1d01492ee
 - name: github.com/gogits/go-gogs-client
   version: e363d3ff8f70d0fe813324eedf228684af41c29c
 - name: github.com/issue9/identicon
diff --git a/gogs.go b/gogs.go
index 8ed3184d64..bfa01013e4 100644
--- a/gogs.go
+++ b/gogs.go
@@ -27,7 +27,7 @@ func init() {
 func main() {
 	app := cli.NewApp()
 	app.Name = "Gogs"
-	app.Usage = "Go Git Service"
+	app.Usage = "Go Git Service: a painless self-hosted Git service"
 	app.Version = APP_VER
 	app.Commands = []cli.Command{
 		cmd.CmdWeb,
diff --git a/models/action.go b/models/action.go
index c727c05992..13085ace71 100644
--- a/models/action.go
+++ b/models/action.go
@@ -6,7 +6,6 @@ package models
 
 import (
 	"encoding/json"
-	"errors"
 	"fmt"
 	"path"
 	"regexp"
@@ -45,10 +44,6 @@ const (
 	ACTION_REOPEN_PULL_REQUEST                       // 15
 )
 
-var (
-	ErrNotImplemented = errors.New("Not implemented yet")
-)
-
 var (
 	// Same as Github. See https://help.github.com/articles/closing-issues-via-commit-messages
 	IssueCloseKeywords  = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
diff --git a/models/issue.go b/models/issue.go
index eb126325f0..541b70068d 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -25,9 +25,8 @@ import (
 )
 
 var (
-	ErrWrongIssueCounter   = errors.New("Invalid number of issues for this milestone")
-	ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
-	ErrMissingIssueNumber  = errors.New("No issue number specified")
+	ErrWrongIssueCounter  = errors.New("Invalid number of issues for this milestone")
+	ErrMissingIssueNumber = errors.New("No issue number specified")
 )
 
 // Issue represents an issue or pull request of repository.
@@ -834,19 +833,6 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 	return issues, sess.Find(&issues)
 }
 
-type IssueStatus int
-
-const (
-	IS_OPEN = iota + 1
-	IS_CLOSE
-)
-
-// GetIssueCountByPoster returns number of issues of repository by poster.
-func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
-	count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
-	return count
-}
-
 // .___                             ____ ___
 // |   | ______ ________ __   ____ |    |   \______ ___________
 // |   |/  ___//  ___/  |  \_/ __ \|    |   /  ___// __ \_  __ \
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index 3c1775dce4..74ee3c4ff8 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -130,7 +130,7 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
 		AutoInit:    form.AutoInit,
 	})
 	if err == nil {
-		log.Trace("Repository created[%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
+		log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
 		ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
 		return
 	}
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index 8f6ee3f06c..85878e7d1e 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -347,23 +347,35 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
 }
 
 func TestWebhook(ctx *context.Context) {
+	// Grab latest commit or fake one if it's empty repository.
+	commit := ctx.Repo.Commit
+	if commit == nil {
+		ghost := models.NewGhostUser()
+		commit = &git.Commit{
+			ID:            git.MustIDFromString(git.EMPTY_SHA),
+			Author:        ghost.NewGitSig(),
+			Committer:     ghost.NewGitSig(),
+			CommitMessage: "This is a fake commit",
+		}
+	}
+
 	apiUser := ctx.User.APIFormat()
 	p := &api.PushPayload{
 		Ref:    git.BRANCH_PREFIX + ctx.Repo.Repository.DefaultBranch,
-		Before: ctx.Repo.CommitID,
-		After:  ctx.Repo.CommitID,
+		Before: commit.ID.String(),
+		After:  commit.ID.String(),
 		Commits: []*api.PayloadCommit{
 			{
-				ID:      ctx.Repo.CommitID,
-				Message: ctx.Repo.Commit.Message(),
-				URL:     ctx.Repo.Repository.FullLink() + "/commit/" + ctx.Repo.CommitID,
+				ID:      commit.ID.String(),
+				Message: commit.Message(),
+				URL:     ctx.Repo.Repository.FullLink() + "/commit/" + commit.ID.String(),
 				Author: &api.PayloadUser{
-					Name:  ctx.Repo.Commit.Author.Name,
-					Email: ctx.Repo.Commit.Author.Email,
+					Name:  commit.Author.Name,
+					Email: commit.Author.Email,
 				},
 				Committer: &api.PayloadUser{
-					Name:  ctx.Repo.Commit.Committer.Name,
-					Email: ctx.Repo.Commit.Committer.Email,
+					Name:  commit.Committer.Name,
+					Email: commit.Committer.Email,
 				},
 			},
 		},