Skip to content

Commit d3f4f9d

Browse files
committed
Don't allow committing to existing branch when choosing new branch
1 parent a1e1730 commit d3f4f9d

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

routers/web/repo/editor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
272272
renderCommitRights(ctx, editRepo)
273273

274274
// Cannot commit to an existing branch if user doesn't have rights
275-
if !canPushToEditRepository(ctx, editRepo, branchName, tplEditFile, &form) {
275+
if !canPushToEditRepository(ctx, editRepo, branchName, form.CommitChoice, tplEditFile, &form) {
276276
return
277277
}
278278

@@ -512,7 +512,7 @@ func DeleteFilePost(ctx *context.Context) {
512512
renderCommitRights(ctx, editRepo)
513513

514514
// Cannot commit to an existing branch if user doesn't have rights
515-
if !canPushToEditRepository(ctx, editRepo, branchName, tplDeleteFile, &form) {
515+
if !canPushToEditRepository(ctx, editRepo, branchName, form.CommitChoice, tplDeleteFile, &form) {
516516
return
517517
}
518518

@@ -710,7 +710,7 @@ func UploadFilePost(ctx *context.Context) {
710710

711711
renderCommitRights(ctx, editRepo)
712712

713-
if !canPushToEditRepository(ctx, editRepo, branchName, tplUploadFile, &form) {
713+
if !canPushToEditRepository(ctx, editRepo, branchName, form.CommitChoice, tplUploadFile, &form) {
714714
return
715715
}
716716

routers/web/repo/fork_to_edit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func getEditRepositoryOrError(ctx *context.Context, tpl templates.TplName, form
109109

110110
// CheckPushEditBranch chesk if pushing to the branch in the edit repository is possible,
111111
// and if not renders an error and returns false.
112-
func canPushToEditRepository(ctx *context.Context, editRepo *repo_model.Repository, branchName string, tpl templates.TplName, form any) bool {
113-
// When pushing to a fork or another branch on the same repository, it should not exist yet
114-
if ctx.Repo.Repository != editRepo || ctx.Repo.BranchName != branchName {
112+
func canPushToEditRepository(ctx *context.Context, editRepo *repo_model.Repository, branchName, commitChoice string, tpl templates.TplName, form any) bool {
113+
// When pushing to a fork or chosing to commit to a new branch, it should not exist yet
114+
if ctx.Repo.Repository != editRepo || commitChoice == frmCommitChoiceNewBranch {
115115
if exist, err := git_model.IsBranchExist(ctx, editRepo.ID, branchName); err == nil && exist {
116116
ctx.Data["Err_NewBranchName"] = true
117117
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tpl, form)

routers/web/repo/patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewDiffPatchPost(ctx *context.Context) {
7979
renderCommitRights(ctx, editRepo)
8080

8181
// Cannot commit to an existing branch if user doesn't have rights
82-
if !canPushToEditRepository(ctx, editRepo, branchName, tplPatchFile, &form) {
82+
if !canPushToEditRepository(ctx, editRepo, branchName, form.CommitChoice, tplPatchFile, &form) {
8383
return
8484
}
8585

0 commit comments

Comments
 (0)