@@ -26,13 +26,26 @@ const (
26
26
tplEditDiffPreview base.TplName = "repo/editor/diff_preview"
27
27
tplDeleteFile base.TplName = "repo/editor/delete"
28
28
tplUploadFile base.TplName = "repo/editor/upload"
29
+
30
+ frmCommitChoiceDirect string = "direct"
31
+ frmCommitChoiceNewBranch string = "commit-to-new-branch"
29
32
)
30
33
34
+ func renderCommitRights (ctx * context.Context ) bool {
35
+ canCommit , err := ctx .Repo .CanCommitToBranch ()
36
+ if err != nil {
37
+ log .Error (4 , "CanCommitToBranch: %v" , err )
38
+ }
39
+ ctx .Data ["CanCommitToBranch" ] = canCommit
40
+ return canCommit
41
+ }
42
+
31
43
func editFile (ctx * context.Context , isNewFile bool ) {
32
44
ctx .Data ["PageIsEdit" ] = true
33
45
ctx .Data ["IsNewFile" ] = isNewFile
34
46
ctx .Data ["RequireHighlightJS" ] = true
35
47
ctx .Data ["RequireSimpleMDE" ] = true
48
+ canCommit := renderCommitRights (ctx )
36
49
37
50
var treeNames []string
38
51
if len (ctx .Repo .TreePath ) > 0 {
@@ -90,7 +103,11 @@ func editFile(ctx *context.Context, isNewFile bool) {
90
103
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
91
104
ctx .Data ["commit_summary" ] = ""
92
105
ctx .Data ["commit_message" ] = ""
93
- ctx .Data ["commit_choice" ] = "direct"
106
+ if canCommit {
107
+ ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
108
+ } else {
109
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
110
+ }
94
111
ctx .Data ["new_branch_name" ] = ""
95
112
ctx .Data ["last_commit" ] = ctx .Repo .Commit .ID
96
113
ctx .Data ["MarkdownFileExts" ] = strings .Join (setting .Markdown .FileExtensions , "," )
@@ -116,14 +133,15 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
116
133
ctx .Data ["IsNewFile" ] = isNewFile
117
134
ctx .Data ["RequireHighlightJS" ] = true
118
135
ctx .Data ["RequireSimpleMDE" ] = true
136
+ canCommit := renderCommitRights (ctx )
119
137
120
138
oldBranchName := ctx .Repo .BranchName
121
139
branchName := oldBranchName
122
140
oldTreePath := ctx .Repo .TreePath
123
141
lastCommit := form .LastCommit
124
142
form .LastCommit = ctx .Repo .Commit .ID .String ()
125
143
126
- if form .CommitChoice == "commit-to-new-branch" {
144
+ if form .CommitChoice == frmCommitChoiceNewBranch {
127
145
branchName = form .NewBranchName
128
146
}
129
147
@@ -164,6 +182,11 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
164
182
ctx .RenderWithErr (ctx .Tr ("repo.editor.branch_already_exists" , branchName ), tplEditFile , & form )
165
183
return
166
184
}
185
+ } else if ! canCommit {
186
+ ctx .Data ["Err_NewBranchName" ] = true
187
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
188
+ ctx .RenderWithErr (ctx .Tr ("repo.editor.cannot_commit_to_protected_branch" , branchName ), tplEditFile , & form )
189
+ return
167
190
}
168
191
169
192
var newTreePath string
@@ -317,10 +340,17 @@ func DeleteFile(ctx *context.Context) {
317
340
ctx .Data ["PageIsDelete" ] = true
318
341
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
319
342
ctx .Data ["TreePath" ] = ctx .Repo .TreePath
343
+ canCommit := renderCommitRights (ctx )
344
+
320
345
ctx .Data ["commit_summary" ] = ""
321
346
ctx .Data ["commit_message" ] = ""
322
- ctx .Data ["commit_choice" ] = "direct"
347
+ if canCommit {
348
+ ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
349
+ } else {
350
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
351
+ }
323
352
ctx .Data ["new_branch_name" ] = ""
353
+
324
354
ctx .HTML (200 , tplDeleteFile )
325
355
}
326
356
@@ -329,11 +359,12 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
329
359
ctx .Data ["PageIsDelete" ] = true
330
360
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
331
361
ctx .Data ["TreePath" ] = ctx .Repo .TreePath
362
+ canCommit := renderCommitRights (ctx )
332
363
333
364
oldBranchName := ctx .Repo .BranchName
334
365
branchName := oldBranchName
335
366
336
- if form .CommitChoice == "commit-to-new-branch" {
367
+ if form .CommitChoice == frmCommitChoiceNewBranch {
337
368
branchName = form .NewBranchName
338
369
}
339
370
ctx .Data ["commit_summary" ] = form .CommitSummary
@@ -352,6 +383,11 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
352
383
ctx .RenderWithErr (ctx .Tr ("repo.editor.branch_already_exists" , branchName ), tplDeleteFile , & form )
353
384
return
354
385
}
386
+ } else if ! canCommit {
387
+ ctx .Data ["Err_NewBranchName" ] = true
388
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
389
+ ctx .RenderWithErr (ctx .Tr ("repo.editor.cannot_commit_to_protected_branch" , branchName ), tplDeleteFile , & form )
390
+ return
355
391
}
356
392
357
393
message := strings .TrimSpace (form .CommitSummary )
@@ -390,6 +426,7 @@ func renderUploadSettings(ctx *context.Context) {
390
426
func UploadFile (ctx * context.Context ) {
391
427
ctx .Data ["PageIsUpload" ] = true
392
428
renderUploadSettings (ctx )
429
+ canCommit := renderCommitRights (ctx )
393
430
394
431
// We must at least have one element for user to input.
395
432
treeNames := []string {"" }
@@ -401,7 +438,11 @@ func UploadFile(ctx *context.Context) {
401
438
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
402
439
ctx .Data ["commit_summary" ] = ""
403
440
ctx .Data ["commit_message" ] = ""
404
- ctx .Data ["commit_choice" ] = "direct"
441
+ if canCommit {
442
+ ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
443
+ } else {
444
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
445
+ }
405
446
ctx .Data ["new_branch_name" ] = ""
406
447
407
448
ctx .HTML (200 , tplUploadFile )
@@ -411,11 +452,12 @@ func UploadFile(ctx *context.Context) {
411
452
func UploadFilePost (ctx * context.Context , form auth.UploadRepoFileForm ) {
412
453
ctx .Data ["PageIsUpload" ] = true
413
454
renderUploadSettings (ctx )
455
+ canCommit := renderCommitRights (ctx )
414
456
415
457
oldBranchName := ctx .Repo .BranchName
416
458
branchName := oldBranchName
417
459
418
- if form .CommitChoice == "commit-to-new-branch" {
460
+ if form .CommitChoice == frmCommitChoiceNewBranch {
419
461
branchName = form .NewBranchName
420
462
}
421
463
@@ -446,6 +488,11 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
446
488
ctx .RenderWithErr (ctx .Tr ("repo.editor.branch_already_exists" , branchName ), tplUploadFile , & form )
447
489
return
448
490
}
491
+ } else if ! canCommit {
492
+ ctx .Data ["Err_NewBranchName" ] = true
493
+ ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
494
+ ctx .RenderWithErr (ctx .Tr ("repo.editor.cannot_commit_to_protected_branch" , branchName ), tplUploadFile , & form )
495
+ return
449
496
}
450
497
451
498
var newTreePath string
0 commit comments