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

---
 modules/middleware/context.go | 13 +++++++++-
 modules/middleware/repo.go    | 59 +++++++++++++++++++++++--------------------
 2 files changed, 44 insertions(+), 28 deletions(-)

(limited to 'modules')

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)
-- 
cgit v1.2.3