@@ -41,16 +41,39 @@ const (
4141 frmCommitChoiceNewBranch string = "commit-to-new-branch"
4242)
4343
44+ func canCreateBasePullRequest (ctx * context.Context ) bool {
45+ baseRepo := ctx .Repo .Repository .BaseRepo
46+ return baseRepo != nil && baseRepo .UnitEnabled (ctx , unit .TypePullRequests )
47+ }
48+
4449func renderCommitRights (ctx * context.Context ) bool {
4550 canCommitToBranch , err := ctx .Repo .CanCommitToBranch (ctx , ctx .Doer )
4651 if err != nil {
4752 log .Error ("CanCommitToBranch: %v" , err )
4853 }
4954 ctx .Data ["CanCommitToBranch" ] = canCommitToBranch
55+ ctx .Data ["CanCreatePullRequest" ] = ctx .Repo .Repository .UnitEnabled (ctx , unit .TypePullRequests ) || canCreateBasePullRequest (ctx )
5056
5157 return canCommitToBranch .CanCommitToBranch
5258}
5359
60+ // redirect to pull request after commit to branch
61+ func redirectToPullRequest (ctx * context.Context , newBranchName string ) bool {
62+ repo := ctx .Repo .Repository
63+ baseBranch := util .PathEscapeSegments (ctx .Repo .BranchName )
64+ headBranch := util .PathEscapeSegments (newBranchName )
65+ if ! repo .UnitEnabled (ctx , unit .TypePullRequests ) {
66+ if ! canCreateBasePullRequest (ctx ) {
67+ return false
68+ }
69+ baseBranch = util .PathEscapeSegments (repo .BaseRepo .DefaultBranch )
70+ headBranch = util .PathEscapeSegments (repo .Owner .Name ) + "/" + util .PathEscapeSegments (repo .Name ) + ":" + headBranch
71+ repo = repo .BaseRepo
72+ }
73+ ctx .Redirect (repo .Link () + "/compare/" + baseBranch + "..." + headBranch )
74+ return true
75+ }
76+
5477// getParentTreeFields returns list of parent tree names and corresponding tree paths
5578// based on given tree path.
5679func getParentTreeFields (treePath string ) (treeNames , treePaths []string ) {
@@ -331,9 +354,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
331354 _ = repo_model .UpdateRepositoryCols (ctx , & repo_model.Repository {ID : ctx .Repo .Repository .ID , IsEmpty : false }, "is_empty" )
332355 }
333356
334- if form .CommitChoice == frmCommitChoiceNewBranch && ctx .Repo .Repository .UnitEnabled (ctx , unit .TypePullRequests ) {
335- ctx .Redirect (ctx .Repo .RepoLink + "/compare/" + util .PathEscapeSegments (ctx .Repo .BranchName ) + "..." + util .PathEscapeSegments (form .NewBranchName ))
336- } else {
357+ if ! (form .CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest (ctx , branchName )) {
337358 ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (form .TreePath ))
338359 }
339360}
@@ -517,9 +538,7 @@ func DeleteFilePost(ctx *context.Context) {
517538 }
518539
519540 ctx .Flash .Success (ctx .Tr ("repo.editor.file_delete_success" , ctx .Repo .TreePath ))
520- if form .CommitChoice == frmCommitChoiceNewBranch && ctx .Repo .Repository .UnitEnabled (ctx , unit .TypePullRequests ) {
521- ctx .Redirect (ctx .Repo .RepoLink + "/compare/" + util .PathEscapeSegments (ctx .Repo .BranchName ) + "..." + util .PathEscapeSegments (form .NewBranchName ))
522- } else {
541+ if ! (form .CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest (ctx , branchName )) {
523542 treePath := path .Dir (ctx .Repo .TreePath )
524543 if treePath == "." {
525544 treePath = "" // the file deleted was in the root, so we return the user to the root directory
@@ -722,9 +741,7 @@ func UploadFilePost(ctx *context.Context) {
722741 _ = repo_model .UpdateRepositoryCols (ctx , & repo_model.Repository {ID : ctx .Repo .Repository .ID , IsEmpty : false }, "is_empty" )
723742 }
724743
725- if form .CommitChoice == frmCommitChoiceNewBranch && ctx .Repo .Repository .UnitEnabled (ctx , unit .TypePullRequests ) {
726- ctx .Redirect (ctx .Repo .RepoLink + "/compare/" + util .PathEscapeSegments (ctx .Repo .BranchName ) + "..." + util .PathEscapeSegments (form .NewBranchName ))
727- } else {
744+ if ! (form .CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest (ctx , branchName )) {
728745 ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (form .TreePath ))
729746 }
730747}
0 commit comments