@@ -53,7 +53,7 @@ func canCreateBasePullRequest(ctx *context.Context, editRepo *repo_model.Reposit
5353func renderCommitRights (ctx * context.Context , editRepo * repo_model.Repository ) bool {
5454 if editRepo == ctx .Repo .Repository {
5555 // Editing the same repository that we are viewing
56- canCommitToBranch , err := ctx . Repo . CanCommitToBranch (ctx , ctx .Doer )
56+ canCommitToBranch , err := context . CanCommitToBranch (ctx , ctx .Doer , ctx . Repo . Repository , ctx . Repo . BranchName )
5757 if err != nil {
5858 log .Error ("CanCommitToBranch: %v" , err )
5959 }
@@ -64,7 +64,7 @@ func renderCommitRights(ctx *context.Context, editRepo *repo_model.Repository) b
6464 return canCommitToBranch .CanCommitToBranch
6565 }
6666
67- // Editing a user fork of the repository we are viewing
67+ // Editing a user fork of the repository we are viewing, always choose a new branch
6868 ctx .Data ["CanCommitToBranch" ] = context.CanCommitToBranchResults {}
6969 ctx .Data ["CanCreatePullRequest" ] = canCreateBasePullRequest (ctx , editRepo )
7070
@@ -121,7 +121,7 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
121121// This may be a fork of the repository owned by the user. If no repository can be found
122122// for editing, nil is returned along with a message explaining why editing is not possible.
123123func getEditRepository (ctx * context.Context ) (* repo_model.Repository , any ) {
124- if ctx . Repo . CanWriteToBranch (ctx , ctx .Doer , ctx .Repo .BranchName ) {
124+ if context . CanWriteToBranch (ctx , ctx .Doer , ctx . Repo . Repository , ctx .Repo .BranchName ) {
125125 return ctx .Repo .Repository , nil
126126 }
127127
@@ -197,18 +197,23 @@ func GetEditRepositoryOrError(ctx *context.Context, tpl templates.TplName, form
197197
198198// CheckPushEditBranch chesk if pushing to the branch in the edit repository is possible,
199199// and if not renders an error and returns false.
200- func CheckCanPushEditBranch (ctx * context.Context , editRepo * repo_model.Repository , branchName string , canCommit bool , tpl templates.TplName , form any ) bool {
200+ func CheckCanPushEditBranch (ctx * context.Context , editRepo * repo_model.Repository , branchName string , tpl templates.TplName , form any ) bool {
201+ // When pushing to a fork or another branch on the same repository, it should not exist yet
201202 if ctx .Repo .Repository != editRepo || ctx .Repo .BranchName != branchName {
202- // When pushing to a fork or another branch on the same repository, it should not exist yet
203203 if exist , err := git_model .IsBranchExist (ctx , editRepo .ID , branchName ); err == nil && exist {
204204 ctx .Data ["Err_NewBranchName" ] = true
205205 ctx .RenderWithErr (ctx .Tr ("repo.editor.branch_already_exists" , branchName ), tpl , form )
206206 return false
207207 }
208- } else if ctx .Repo .Repository == editRepo && ! canCommit {
209- // When we can't commit to the same branch on the same repository, it's protected
208+ }
209+
210+ // Check for protected branch
211+ canCommitToBranch , err := context .CanCommitToBranch (ctx , ctx .Doer , editRepo , branchName )
212+ if err != nil {
213+ log .Error ("CanCommitToBranch: %v" , err )
214+ }
215+ if ! canCommitToBranch .CanCommitToBranch {
210216 ctx .Data ["Err_NewBranchName" ] = true
211- ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
212217 ctx .RenderWithErr (ctx .Tr ("repo.editor.cannot_commit_to_protected_branch" , branchName ), tpl , form )
213218 return false
214219 }
@@ -464,10 +469,10 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
464469 return
465470 }
466471
467- canCommit := renderCommitRights (ctx , editRepo )
472+ renderCommitRights (ctx , editRepo )
468473
469474 // Cannot commit to an existing branch if user doesn't have rights
470- if ! CheckCanPushEditBranch (ctx , editRepo , branchName , canCommit , tplEditFile , & form ) {
475+ if ! CheckCanPushEditBranch (ctx , editRepo , branchName , tplEditFile , & form ) {
471476 return
472477 }
473478
@@ -704,10 +709,10 @@ func DeleteFilePost(ctx *context.Context) {
704709 return
705710 }
706711
707- canCommit := renderCommitRights (ctx , editRepo )
712+ renderCommitRights (ctx , editRepo )
708713
709714 // Cannot commit to an existing branch if user doesn't have rights
710- if ! CheckCanPushEditBranch (ctx , editRepo , branchName , canCommit , tplDeleteFile , & form ) {
715+ if ! CheckCanPushEditBranch (ctx , editRepo , branchName , tplDeleteFile , & form ) {
711716 return
712717 }
713718
@@ -903,9 +908,9 @@ func UploadFilePost(ctx *context.Context) {
903908 return
904909 }
905910
906- canCommit := renderCommitRights (ctx , editRepo )
911+ renderCommitRights (ctx , editRepo )
907912
908- if ! CheckCanPushEditBranch (ctx , editRepo , branchName , canCommit , tplUploadFile , & form ) {
913+ if ! CheckCanPushEditBranch (ctx , editRepo , branchName , tplUploadFile , & form ) {
909914 return
910915 }
911916
0 commit comments