@@ -41,16 +41,49 @@ 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+ // redirectForCommitChoice redirects after committing the edit to a branch
61+ func redirectForCommitChoice (ctx * context.Context , commitChoice , newBranchName , treePath string ) {
62+ if commitChoice == frmCommitChoiceNewBranch {
63+ // Redirect to a pull request when possible
64+ redirectToPullRequest := false
65+ repo := ctx .Repo .Repository
66+ baseBranch := ctx .Repo .BranchName
67+ headBranch := newBranchName
68+ if repo .UnitEnabled (ctx , unit .TypePullRequests ) {
69+ redirectToPullRequest = true
70+ } else if canCreateBasePullRequest (ctx ) {
71+ redirectToPullRequest = true
72+ baseBranch = repo .BaseRepo .DefaultBranch
73+ headBranch = repo .Owner .Name + "/" + repo .Name + ":" + headBranch
74+ repo = repo .BaseRepo
75+ }
76+
77+ if redirectToPullRequest {
78+ ctx .Redirect (repo .Link () + "/compare/" + util .PathEscapeSegments (baseBranch ) + "..." + util .PathEscapeSegments (headBranch ))
79+ return
80+ }
81+ }
82+
83+ // Redirect to viewing file or folder
84+ ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (newBranchName ) + "/" + util .PathEscapeSegments (treePath ))
85+ }
86+
5487// getParentTreeFields returns list of parent tree names and corresponding tree paths
5588// based on given tree path.
5689func getParentTreeFields (treePath string ) (treeNames , treePaths []string ) {
@@ -331,11 +364,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
331364 _ = repo_model .UpdateRepositoryCols (ctx , & repo_model.Repository {ID : ctx .Repo .Repository .ID , IsEmpty : false }, "is_empty" )
332365 }
333366
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 {
337- ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (form .TreePath ))
338- }
367+ redirectForCommitChoice (ctx , form .CommitChoice , branchName , form .TreePath )
339368}
340369
341370// EditFilePost response for editing file
@@ -517,26 +546,23 @@ func DeleteFilePost(ctx *context.Context) {
517546 }
518547
519548 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 {
523- treePath := path .Dir (ctx .Repo .TreePath )
524- if treePath == "." {
525- treePath = "" // the file deleted was in the root, so we return the user to the root directory
526- }
527- if len (treePath ) > 0 {
528- // Need to get the latest commit since it changed
529- commit , err := ctx .Repo .GitRepo .GetBranchCommit (ctx .Repo .BranchName )
530- if err == nil && commit != nil {
531- // We have the comment, now find what directory we can return the user to
532- // (must have entries)
533- treePath = GetClosestParentWithFiles (treePath , commit )
534- } else {
535- treePath = "" // otherwise return them to the root of the repo
536- }
549+ treePath := path .Dir (ctx .Repo .TreePath )
550+ if treePath == "." {
551+ treePath = "" // the file deleted was in the root, so we return the user to the root directory
552+ }
553+ if len (treePath ) > 0 {
554+ // Need to get the latest commit since it changed
555+ commit , err := ctx .Repo .GitRepo .GetBranchCommit (ctx .Repo .BranchName )
556+ if err == nil && commit != nil {
557+ // We have the comment, now find what directory we can return the user to
558+ // (must have entries)
559+ treePath = GetClosestParentWithFiles (treePath , commit )
560+ } else {
561+ treePath = "" // otherwise return them to the root of the repo
537562 }
538- ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (treePath ))
539563 }
564+
565+ redirectForCommitChoice (ctx , form .CommitChoice , branchName , treePath )
540566}
541567
542568// UploadFile render upload file page
@@ -722,11 +748,7 @@ func UploadFilePost(ctx *context.Context) {
722748 _ = repo_model .UpdateRepositoryCols (ctx , & repo_model.Repository {ID : ctx .Repo .Repository .ID , IsEmpty : false }, "is_empty" )
723749 }
724750
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 {
728- ctx .Redirect (ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (form .TreePath ))
729- }
751+ redirectForCommitChoice (ctx , form .CommitChoice , branchName , form .TreePath )
730752}
731753
732754func cleanUploadFileName (name string ) string {
0 commit comments