diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index dae5aeea21..dc50671335 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1404,6 +1404,7 @@ editor.fork_create_description = You can not edit this repository directly. Inst editor.fork_edit_description = You can not edit this repository directly. The changes will be written to your fork %s, so you can create a pull request. editor.fork_not_editable = You have forked this repository but your fork is not editable. editor.fork_failed_to_push_branch = Failed to push branch %s to your repository. +editor.fork_branch_exists = Branch "%s" already exists in your fork, please choose a new branch name. commits.desc = Browse source code change history. commits.commits = Commits diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 8badfb37c8..0f1986a92e 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -146,7 +146,17 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co oldBranchName := ctx.Repo.BranchName fromBaseBranch := ctx.FormString("from_base_branch") if fromBaseBranch != "" { - err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, ctx.Repo.Repository, targetBranchName) + // if target branch exists, we should warn users + targetBranchExists, err := git_model.IsBranchExist(ctx, commitFormOptions.TargetRepo.ID, targetBranchName) + if err != nil { + ctx.ServerError("IsBranchExist", err) + return nil + } + if targetBranchExists { + ctx.JSONError(ctx.Tr("repo.editor.fork_branch_exists", targetBranchName)) + return nil + } + err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, commitFormOptions.TargetRepo, targetBranchName) if err != nil { log.Error("Unable to editorPushBranchToForkedRepository: %v", err) ctx.JSONError(ctx.Tr("repo.editor.fork_failed_to_push_branch", targetBranchName)) diff --git a/templates/repo/editor/cherry_pick.tmpl b/templates/repo/editor/cherry_pick.tmpl index 884f0e6b9e..7981fd0761 100644 --- a/templates/repo/editor/cherry_pick.tmpl +++ b/templates/repo/editor/cherry_pick.tmpl @@ -5,6 +5,7 @@ {{template "base/alert" .}}