@@ -24,7 +24,12 @@ const (
2424
2525// NewDiffPatch render create patch page
2626func NewDiffPatch (ctx * context.Context ) {
27- canCommit := renderCommitRights (ctx , ctx .Repo .Repository )
27+ editRepo := GetEditRepositoryOrFork (ctx , "_diffpatch" )
28+ if editRepo == nil {
29+ return
30+ }
31+
32+ canCommit := renderCommitRights (ctx , editRepo )
2833
2934 ctx .Data ["PageIsPatch" ] = true
3035
@@ -35,7 +40,7 @@ func NewDiffPatch(ctx *context.Context) {
3540 } else {
3641 ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
3742 }
38- ctx .Data ["new_branch_name" ] = GetUniquePatchBranchName (ctx , ctx . Repo . Repository )
43+ ctx .Data ["new_branch_name" ] = GetUniquePatchBranchName (ctx , editRepo )
3944 ctx .Data ["last_commit" ] = ctx .Repo .CommitID
4045 ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
4146 ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
@@ -47,7 +52,6 @@ func NewDiffPatch(ctx *context.Context) {
4752func NewDiffPatchPost (ctx * context.Context ) {
4853 form := web .GetForm (ctx ).(* forms.EditRepoFileForm )
4954
50- canCommit := renderCommitRights (ctx , ctx .Repo .Repository )
5155 branchName := ctx .Repo .BranchName
5256 if form .CommitChoice == frmCommitChoiceNewBranch {
5357 branchName = form .NewBranchName
@@ -67,11 +71,15 @@ func NewDiffPatchPost(ctx *context.Context) {
6771 return
6872 }
6973
74+ editRepo := GetEditRepositoryOrError (ctx , tplPatchFile , & form )
75+ if editRepo == nil {
76+ return
77+ }
78+
79+ canCommit := renderCommitRights (ctx , editRepo )
80+
7081 // Cannot commit to an existing branch if user doesn't have rights
71- if branchName == ctx .Repo .BranchName && ! canCommit {
72- ctx .Data ["Err_NewBranchName" ] = true
73- ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
74- ctx .RenderWithErr (ctx .Tr ("repo.editor.cannot_commit_to_protected_branch" , branchName ), tplEditFile , & form )
82+ if ! CheckCanPushEditBranch (ctx , editRepo , branchName , canCommit , tplPatchFile , & form ) {
7583 return
7684 }
7785
@@ -94,9 +102,14 @@ func NewDiffPatchPost(ctx *context.Context) {
94102 return
95103 }
96104
97- fileResponse , err := files .ApplyDiffPatch (ctx , ctx .Repo .Repository , ctx .Doer , & files.ApplyDiffPatchOptions {
105+ editBranchName , err := PushEditBranchOrError (ctx , editRepo , branchName , tplPatchFile , & form )
106+ if err != nil {
107+ return
108+ }
109+
110+ fileResponse , err := files .ApplyDiffPatch (ctx , editRepo , ctx .Doer , & files.ApplyDiffPatchOptions {
98111 LastCommitID : form .LastCommit ,
99- OldBranch : ctx . Repo . BranchName ,
112+ OldBranch : editBranchName ,
100113 NewBranch : branchName ,
101114 Message : message ,
102115 Content : strings .ReplaceAll (form .Content , "\r " , "" ),
@@ -119,7 +132,8 @@ func NewDiffPatchPost(ctx *context.Context) {
119132 }
120133
121134 if form .CommitChoice == frmCommitChoiceNewBranch && ctx .Repo .Repository .UnitEnabled (ctx , unit .TypePullRequests ) {
122- ctx .Redirect (ctx .Repo .RepoLink + "/compare/" + util .PathEscapeSegments (ctx .Repo .BranchName ) + "..." + util .PathEscapeSegments (form .NewBranchName ))
135+ editBranch := editRepo .Owner .Name + "/" + editRepo .Name + ":" + branchName
136+ ctx .Redirect (ctx .Repo .RepoLink + "/compare/" + util .PathEscapeSegments (ctx .Repo .BranchName ) + "..." + util .PathEscapeSegments (editBranch ))
123137 } else {
124138 ctx .Redirect (ctx .Repo .RepoLink + "/commit/" + fileResponse .Commit .SHA )
125139 }
0 commit comments