99 "errors"
1010 "fmt"
1111 "html"
12+ "html/template"
1213 "net/http"
1314 "net/url"
1415 "path"
@@ -124,7 +125,7 @@ func (r *Repository) CanWriteToBranch(ctx context.Context, user *user_model.User
124125
125126// CanCreateBranch returns true if repository is editable and user has proper access level.
126127func (r * Repository ) CanCreateBranch () bool {
127- return r .Permission .CanWrite (unit_model .TypeCode ) && r .Repository .CanCreateBranch ()
128+ return r .Permission .CanWrite (unit_model .TypeCode ) && r .Repository .CanContentChange ()
128129}
129130
130131func (r * Repository ) GetObjectFormat () git.ObjectFormat {
@@ -143,15 +144,18 @@ func RepoMustNotBeArchived() func(ctx *Context) {
143144type CommitFormOptions struct {
144145 NeedFork bool
145146
146- TargetRepo * repo_model.Repository
147- TargetFormAction string
148- WillSubmitToFork bool
147+ TargetRepo * repo_model.Repository
148+ TargetFormAction string
149+
150+ WillSubmitToFork bool
151+
149152 CanCommitToBranch bool
150- UserCanPush bool
151- RequireSigned bool
152- WillSign bool
153- SigningKeyFormDisplay string
154- WontSignReason string
153+ DenyCommitToBranchReason template.HTML
154+
155+ WillSign bool
156+ SigningKeyFormDisplay string
157+ WontSignReason string
158+
155159 CanCreatePullRequest bool
156160 CanCreateBasePullRequest bool
157161}
@@ -172,7 +176,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
172176 }
173177 // now, we get our own forked repo; it must be writable by us.
174178 }
175- submitToForkedRepo := targetRepo . ID != originRepo . ID
179+
176180 err := targetRepo .GetBaseRepo (ctx )
177181 if err != nil {
178182 return nil , err
@@ -214,33 +218,43 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
214218 return nil , err
215219 }
216220
217- canCommitToBranch := ! submitToForkedRepo /* same repo */ && targetRepo .CanEnableEditor () && canPushWithProtection
218- if protectionRequireSigned {
219- canCommitToBranch = canCommitToBranch && willSign
220- }
221-
222221 canCreateBasePullRequest := targetRepo .BaseRepo != nil && targetRepo .BaseRepo .UnitEnabled (ctx , unit_model .TypePullRequests )
223222 canCreatePullRequest := targetRepo .UnitEnabled (ctx , unit_model .TypePullRequests ) || canCreateBasePullRequest
224223
225224 opts := & CommitFormOptions {
226- TargetRepo : targetRepo ,
227- WillSubmitToFork : submitToForkedRepo ,
228- CanCommitToBranch : canCommitToBranch ,
229- UserCanPush : canPushWithProtection ,
230- RequireSigned : protectionRequireSigned ,
225+ TargetRepo : targetRepo ,
226+
227+ WillSubmitToFork : targetRepo .ID != originRepo .ID ,
228+
231229 WillSign : willSign ,
232230 SigningKeyFormDisplay : asymkey_model .GetDisplaySigningKey (signKey ),
233231 WontSignReason : wontSignReason ,
234232
235233 CanCreatePullRequest : canCreatePullRequest ,
236234 CanCreateBasePullRequest : canCreateBasePullRequest ,
237235 }
236+
238237 editorAction := ctx .PathParam ("editor_action" )
239238 editorPathParamRemaining := util .PathEscapeSegments (branchName ) + "/" + util .PathEscapeSegments (ctx .Repo .TreePath )
240- if submitToForkedRepo {
239+
240+ opts .CanCommitToBranch = false
241+ if opts .WillSubmitToFork {
242+ opts .DenyCommitToBranchReason = ctx .Locale .Tr ("repo.editor.no_write_permission" )
241243 // there is only "default branch" in forked repo, we will use "from_base_branch" to get a new branch from base repo
242244 editorPathParamRemaining = util .PathEscapeSegments (targetRepo .DefaultBranch ) + "/" + util .PathEscapeSegments (ctx .Repo .TreePath ) + "?from_base_branch=" + url .QueryEscape (branchName )
245+ } else {
246+ // if the user is committing to the same repo, we need to check if the branch is protected and if the user can push to it
247+ if ! targetRepo .CanContentChange () {
248+ opts .DenyCommitToBranchReason = ctx .Locale .Tr ("repo.editor.repo_not_editable" )
249+ } else if ! canPushWithProtection {
250+ opts .DenyCommitToBranchReason = ctx .Locale .Tr ("repo.editor.branch_is_protected" )
251+ } else if protectionRequireSigned && ! willSign {
252+ opts .DenyCommitToBranchReason = ctx .Locale .Tr ("repo.editor.require_signed_commit" )
253+ } else {
254+ opts .CanCommitToBranch = true
255+ }
243256 }
257+
244258 if editorAction == "_cherrypick" {
245259 opts .TargetFormAction = targetRepo .Link () + "/" + editorAction + "/" + ctx .PathParam ("sha" ) + "/" + editorPathParamRemaining
246260 } else {
0 commit comments