From 0e9bc2d4108ae32611e4fe65af493913c20d0279 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Sun, 6 Mar 2016 23:57:46 -0500
Subject: [PATCH] Fix pull request availability check

---
 README.md                         |  2 +-
 gogs.go                           |  2 +-
 modules/middleware/context.go     | 13 ++++++-
 modules/middleware/repo.go        | 59 +++++++++++++++++--------------
 routers/repo/issue.go             | 16 +++++++--
 routers/repo/pull.go              |  2 +-
 templates/.VERSION                |  2 +-
 templates/repo/home.tmpl          |  4 +--
 templates/repo/issue/list.tmpl    |  2 +-
 templates/repo/issue/view.tmpl    |  2 +-
 templates/repo/pulls/commits.tmpl |  2 +-
 templates/repo/pulls/compare.tmpl |  4 +--
 templates/repo/pulls/files.tmpl   |  2 +-
 13 files changed, 69 insertions(+), 43 deletions(-)

diff --git a/README.md b/README.md
index 290c9893a8..9597da4ce3 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
 
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
 
-##### Current version: 0.9.0
+##### Current version: 0.9.1
 
 | Web | UI  | Preview  |
 |:-------------:|:-------:|:-------:|
diff --git a/gogs.go b/gogs.go
index ff0dac92fc..1a18d4a55b 100644
--- a/gogs.go
+++ b/gogs.go
@@ -17,7 +17,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.9.0.0306"
+const APP_VER = "0.9.1.0306"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index cee5d10032..d2b7de4a27 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -27,6 +27,13 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
+type PullRequestContext struct {
+	BaseRepo *models.Repository
+	Allowed  bool
+	SameRepo bool
+	HeadInfo string // [<user>:]<branch>
+}
+
 type RepoContext struct {
 	AccessMode   models.AccessMode
 	IsWatching   bool
@@ -46,6 +53,8 @@ type RepoContext struct {
 	CloneLink    models.CloneLink
 	CommitsCount int64
 	Mirror       *models.Mirror
+
+	PullRequest *PullRequestContext
 }
 
 // Context represents context of a request.
@@ -211,7 +220,9 @@ func Contexter() macaron.Handler {
 			csrf:    x,
 			Flash:   f,
 			Session: sess,
-			Repo:    &RepoContext{},
+			Repo: &RepoContext{
+				PullRequest: &PullRequestContext{},
+			},
 		}
 		// Compute current URL for real-time change language.
 		ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/")
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index 3e1835f5c9..f96b87d437 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -142,32 +142,6 @@ func RepoAssignment(args ...bool) macaron.Handler {
 		ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
 		ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
 
-		if repo.IsFork {
-			RetrieveBaseRepo(ctx, repo)
-			if ctx.Written() {
-				return
-			}
-		}
-
-		// People who have push access and propose a new pull request.
-		if ctx.Repo.IsWriter() {
-			// Pull request is allowed if this is a fork repository
-			// and base repository accepts pull requests.
-			if repo.BaseRepo != nil {
-				if repo.BaseRepo.AllowsPulls() {
-					ctx.Data["CanPullRequest"] = true
-					ctx.Data["BaseRepo"] = repo.BaseRepo
-				}
-			} else {
-				// Or, this is repository accepts pull requests between branches.
-				if repo.AllowsPulls() {
-					ctx.Data["CanPullRequest"] = true
-					ctx.Data["BaseRepo"] = repo
-					ctx.Data["IsBetweenBranches"] = true
-				}
-			}
-		}
-
 		ctx.Data["DisableSSH"] = setting.SSH.Disabled
 		ctx.Data["CloneLink"] = repo.CloneLink()
 		ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
@@ -209,10 +183,41 @@ func RepoAssignment(args ...bool) macaron.Handler {
 				ctx.Repo.BranchName = brs[0]
 			}
 		}
-
 		ctx.Data["BranchName"] = ctx.Repo.BranchName
 		ctx.Data["CommitID"] = ctx.Repo.CommitID
 
+		if repo.IsFork {
+			RetrieveBaseRepo(ctx, repo)
+			if ctx.Written() {
+				return
+			}
+		}
+
+		// People who have push access and propose a new pull request.
+		if ctx.Repo.IsWriter() {
+			// Pull request is allowed if this is a fork repository
+			// and base repository accepts pull requests.
+			if repo.BaseRepo != nil {
+				if repo.BaseRepo.AllowsPulls() {
+					ctx.Data["BaseRepo"] = repo.BaseRepo
+					ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
+					ctx.Repo.PullRequest.Allowed = true
+					ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
+				}
+			} else {
+				// Or, this is repository accepts pull requests between branches.
+				if repo.AllowsPulls() {
+					ctx.Data["BaseRepo"] = repo
+					ctx.Repo.PullRequest.BaseRepo = repo
+					ctx.Repo.PullRequest.Allowed = true
+					ctx.Repo.PullRequest.SameRepo = true
+					ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
+				}
+			}
+		}
+		fmt.Println(222222, ctx.Repo.PullRequest)
+		ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
+
 		if ctx.Query("go-get") == "1" {
 			ctx.Data["GoGetImport"] = path.Join(setting.Domain, setting.AppSubUrl, owner.Name, repo.Name)
 			prefix := setting.AppUrl + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName)
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 02ccd2f9db..3a1049e65d 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -55,15 +55,21 @@ var (
 func MustEnableIssues(ctx *middleware.Context) {
 	if !ctx.Repo.Repository.EnableIssues {
 		ctx.Handle(404, "MustEnableIssues", nil)
+		return
 	}
 }
 
 func MustAllowPulls(ctx *middleware.Context) {
 	if !ctx.Repo.Repository.AllowsPulls() {
 		ctx.Handle(404, "MustAllowPulls", nil)
+		return
 	}
 
-	ctx.Data["HasForkedRepo"] = ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)
+	// User can send pull request if owns a forked repository.
+	if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
+		ctx.Repo.PullRequest.Allowed = true
+		ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
+	}
 }
 
 func RetrieveLabels(ctx *middleware.Context) {
@@ -560,14 +566,18 @@ func ViewIssue(ctx *middleware.Context) {
 	}
 
 	if issue.IsPull {
+		MustAllowPulls(ctx)
+		if ctx.Written() {
+			return
+		}
+		ctx.Data["PageIsPullList"] = true
+
 		if err = issue.GetPullRequest(); err != nil {
 			ctx.Handle(500, "GetPullRequest", err)
 			return
 		}
 
-		ctx.Data["PageIsPullList"] = true
 		ctx.Data["PageIsPullConversation"] = true
-		ctx.Data["HasForkedRepo"] = ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)
 	} else {
 		MustEnableIssues(ctx)
 		if ctx.Written() {
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index c9d92297f6..fd4f6f69c5 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -462,7 +462,7 @@ func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository
 	}
 	ctx.Data["HeadUser"] = headUser
 	ctx.Data["HeadBranch"] = headBranch
-	ctx.Data["IsBetweenBranches"] = isSameRepo
+	ctx.Repo.PullRequest.SameRepo = isSameRepo
 
 	// Check if base branch is valid.
 	if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
diff --git a/templates/.VERSION b/templates/.VERSION
index 21142bce7c..873dfc2a76 100644
--- a/templates/.VERSION
+++ b/templates/.VERSION
@@ -1 +1 @@
-0.9.0.0306
\ No newline at end of file
+0.9.1.0306
\ No newline at end of file
diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl
index 36752a6eab..fe5a621634 100644
--- a/templates/repo/home.tmpl
+++ b/templates/repo/home.tmpl
@@ -7,9 +7,9 @@
 			<a class="link" href="{{.Repository.Website}}">{{.Repository.Website}}</a>
 		</p>
 		<div class="ui secondary menu">
-			{{if .CanPullRequest}}
+			{{if .PullRequestCtx.Allowed}}
 				<div class="fitted item">
-					<a href="{{.BaseRepo.RepoLink}}/compare/{{.BaseRepo.DefaultBranch}}...{{if not .IsBetweenBranches}}{{$.Owner.Name}}:{{end}}{{$.BranchName}}">
+					<a href="{{.BaseRepo.RepoLink}}/compare/{{.BaseRepo.DefaultBranch}}...{{.PullRequestCtx.HeadInfo}}">
 						<button class="ui green small button"><i class="octicon octicon-git-compare"></i></button>
 					</a>
 				</div>
diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl
index b4e79ee9c6..2e7de1f7ad 100644
--- a/templates/repo/issue/list.tmpl
+++ b/templates/repo/issue/list.tmpl
@@ -8,7 +8,7 @@
 				{{if .PageIsIssueList}}
 					<a class="ui green button" href="{{.RepoLink}}/issues/new">{{.i18n.Tr "repo.issues.new"}}</a>
 				{{else}}
-					<a class="ui green button {{if not (or .CanPullRequest .HasForkedRepo)}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.Repository.DefaultBranch}}...{{if not (eq .Owner.Name .SignedUserName)}}{{.SignedUserName}}:{{end}}{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
+					<a class="ui green button {{if not .PullRequestCtx.Allowed}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.Repository.DefaultBranch}}...{{.PullRequestCtx.HeadInfo}}">{{.i18n.Tr "repo.pulls.new"}}</a>
 				{{end}}
 			</div>
 		</div>
diff --git a/templates/repo/issue/view.tmpl b/templates/repo/issue/view.tmpl
index 4e2e69bbeb..c289dbcb7a 100644
--- a/templates/repo/issue/view.tmpl
+++ b/templates/repo/issue/view.tmpl
@@ -8,7 +8,7 @@
 				{{if .PageIsIssueList}}
 					<a class="ui green button" href="{{.RepoLink}}/issues/new">{{.i18n.Tr "repo.issues.new"}}</a>
 				{{else}}
-					<a class="ui green button {{if not .HasForkedRepo}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.SignedUserName}}:{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
+					<a class="ui green button {{if not .PullRequestCtx.Allowed}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.PullRequestCtx.HeadInfo}}">{{.i18n.Tr "repo.pulls.new"}}</a>
 				{{end}}
 			</div>
 		</div>
diff --git a/templates/repo/pulls/commits.tmpl b/templates/repo/pulls/commits.tmpl
index 469499ae84..99bac62cfa 100644
--- a/templates/repo/pulls/commits.tmpl
+++ b/templates/repo/pulls/commits.tmpl
@@ -5,7 +5,7 @@
 		<div class="navbar">
 			{{template "repo/issue/navbar" .}}
 			<div class="ui right">
-				<a class="ui green button {{if not .HasForkedRepo}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.SignedUserName}}:{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
+				<a class="ui green button {{if not .PullRequestCtx.Allowed}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.PullRequestCtx.HeadInfo}}">{{.i18n.Tr "repo.pulls.new"}}</a>
 			</div>
 		</div>
 		<div class="ui divider"></div>
diff --git a/templates/repo/pulls/compare.tmpl b/templates/repo/pulls/compare.tmpl
index 30a2fd5b69..0d7b6f9b55 100644
--- a/templates/repo/pulls/compare.tmpl
+++ b/templates/repo/pulls/compare.tmpl
@@ -21,7 +21,7 @@
 						</div>
 						<div class="scrolling menu">
 							{{range .Branches}}
-								<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{.}}...{{if not $.IsBetweenBranches}}{{$.HeadUser.Name}}:{{end}}{{$.HeadBranch}}">{{.}}</div>
+								<div class="item {{if eq $.BaseBranch .}}selected{{end}}" data-url="{{$.RepoLink}}/compare/{{.}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}:{{end}}{{$.HeadBranch}}">{{.}}</div>
 							{{end}}
 						</div>
 					</div>
@@ -39,7 +39,7 @@
 						</div>
 						<div class="scrolling menu">
 							{{range .HeadBranches}}
-								<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{$.BaseBranch}}...{{if not $.IsBetweenBranches}}{{$.HeadUser.Name}}:{{end}}{{.}}">{{.}}</div>
+								<div class="{{if eq $.HeadBranch .}}selected{{end}} item" data-url="{{$.RepoLink}}/compare/{{$.BaseBranch}}...{{if not $.PullRequestCtx.SameRepo}}{{$.HeadUser.Name}}:{{end}}{{.}}">{{.}}</div>
 							{{end}}
 						</div>
 					</div>
diff --git a/templates/repo/pulls/files.tmpl b/templates/repo/pulls/files.tmpl
index 14ef29037b..19a75e0763 100644
--- a/templates/repo/pulls/files.tmpl
+++ b/templates/repo/pulls/files.tmpl
@@ -5,7 +5,7 @@
 		<div class="navbar">
 			{{template "repo/issue/navbar" .}}
 			<div class="ui right">
-				<a class="ui green button {{if not .HasForkedRepo}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.SignedUserName}}:{{.BranchName}}">{{.i18n.Tr "repo.pulls.new"}}</a>
+				<a class="ui green button {{if not .PullRequestCtx.Allowed}}disabled{{end}}" href="{{.RepoLink}}/compare/{{.BranchName}}...{{.PullRequestCtx.HeadInfo}}">{{.i18n.Tr "repo.pulls.new"}}</a>
 			</div>
 		</div>
 		<div class="ui divider"></div>