From 8313490acb40d13a233c96c8254f6b09f29a271e Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 22 Jun 2025 10:28:57 +0800 Subject: [PATCH] show "Add File", check branch name, move prompt, handle json error --- options/locale/locale_en-US.ini | 1 + routers/web/repo/editor.go | 12 ++++++- templates/repo/editor/cherry_pick.tmpl | 1 + templates/repo/editor/commit_form.tmpl | 6 ---- templates/repo/editor/common_top.tmpl | 5 +++ templates/repo/editor/delete.tmpl | 1 + templates/repo/editor/edit.tmpl | 1 + templates/repo/editor/patch.tmpl | 1 + templates/repo/editor/upload.tmpl | 1 + templates/repo/view_content.tmpl | 4 +-- tests/integration/editor_test.go | 39 ++++++++++++++-------- tests/integration/html_helper.go | 2 +- web_src/js/features/common-fetch-action.ts | 34 +++++++++++-------- 13 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 templates/repo/editor/common_top.tmpl 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" .}}
{{.CsrfTokenHtml}} + {{template "repo/editor/common_top" .}}
- - {{if .CommitFormOptions.WillSubmitToFork}} -
-

{{ctx.Locale.Tr "repo.editor.fork_edit_description" .CommitFormOptions.TargetRepo.FullName}}

-
- {{end}} diff --git a/templates/repo/editor/common_top.tmpl b/templates/repo/editor/common_top.tmpl new file mode 100644 index 0000000000..caf2798467 --- /dev/null +++ b/templates/repo/editor/common_top.tmpl @@ -0,0 +1,5 @@ +{{if .CommitFormOptions.WillSubmitToFork}} +
+ {{ctx.Locale.Tr "repo.editor.fork_edit_description" .CommitFormOptions.TargetRepo.FullName}} +
+{{end}} diff --git a/templates/repo/editor/delete.tmpl b/templates/repo/editor/delete.tmpl index a2439fb728..bf6143f1cb 100644 --- a/templates/repo/editor/delete.tmpl +++ b/templates/repo/editor/delete.tmpl @@ -5,6 +5,7 @@ {{template "base/alert" .}} {{.CsrfTokenHtml}} + {{template "repo/editor/common_top" .}} {{template "repo/editor/commit_form" .}}
diff --git a/templates/repo/editor/edit.tmpl b/templates/repo/editor/edit.tmpl index 7b7854fda1..ccd4425142 100644 --- a/templates/repo/editor/edit.tmpl +++ b/templates/repo/editor/edit.tmpl @@ -12,6 +12,7 @@ {{template "repo/editor/common_breadcrumb" .}} {{if not .NotEditableReason}} + {{template "repo/editor/common_top" .}}
+ {{template "repo/editor/common_top" .}}