fix(web): redirect to the branch after applying a patch (#40450) (#40455)

Backport #40450 by @lazerg

After applying a patch in the web editor, Gitea redirected to
`/src/branch/<branch>/__dummy_for_EditRepoFileForm.TreePath(Required)__`,
which is a 404. The patch form only sends that placeholder `tree_path`
to pass form validation, but the handler still used it as the redirect
path. It now redirects to the branch root, like cherry-pick and revert
do.

Fixes: https://github.com/go-gitea/gitea/issues/40449

Assisted-by: Claude Code:claude-opus-5-5

Co-authored-by: Lazizbek Ergashev <lazerg2@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-11-20 10:20:35 +00:00
1 parent 93ae5fb35b
commit 62e9f747e8
4 files changed
+14 -8

No files matched your search

+1 -1
View File
@@ -48,5 +48,5 @@ func NewDiffPatchPost(ctx *context.Context) {
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
return
}
redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)
redirectForCommitChoice(ctx, parsed, "")
}
+1 -1
View File
@@ -79,5 +79,5 @@ func CherryPickPost(ctx *context.Context) {
return
}
}
redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)
redirectForCommitChoice(ctx, parsed, "")
}
-1
View File
@@ -15,7 +15,6 @@
<div class="breadcrumb-divider">:</div>
<a class="section" href="{{$.BranchLink}}">{{.BranchName}}</a>
<span>{{ctx.Locale.Tr "repo.editor.or"}} <a href="{{$.BranchLink}}">{{ctx.Locale.Tr "repo.editor.cancel_lower"}}</a></span>
<input type="hidden" name="tree_path" value="__dummy_for_EditRepoFileForm.TreePath(Required)__">
<input id="file-name" type="hidden" value="diff.patch">
</div>
</div>
+12 -5
View File
@@ -209,12 +209,15 @@ index 0000000000..aaaaaaaaaa
+++ b/patch-file-1.txt
@@ -0,0 +1 @@
+`
testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", map[string]string{
resp := testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", map[string]string{
"content": pathContentCommon + "patched content\n",
"commit_choice": "commit-to-new-branch",
"new_branch_name": "patched-branch",
})
resp := MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/raw/branch/patched-branch/patch-file-1.txt"), http.StatusOK)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "/user2/repo1/compare/master...patched-branch", test.RedirectURL(resp))
resp = MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/raw/branch/patched-branch/patch-file-1.txt"), http.StatusOK)
assert.Equal(t, "patched content\n", resp.Body.String())
// patch again, it should fail
@@ -223,6 +226,7 @@ index 0000000000..aaaaaaaaaa
"commit_choice": "commit-to-new-branch",
"new_branch_name": "patched-branch-1",
})
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Equal(t, "Unable to apply patch", test.ParseJSONError(resp.Body.Bytes()).ErrorMessage)
}
@@ -334,9 +338,9 @@ func testEditorWebGitCommitEmail(t *testing.T) {
})
t.Run("ApplyPatchCherryPick", func(t *testing.T) {
testWebGit(t,
patchResp1, patchResp2 := testWebGit(t,
"/user2/repo1/_diffpatch/master", map[string]string{
"tree_path": "__dummy__",
"tree_path": "__dummy__", // the "tree_path" should be ignored and should not affect the redirect URL
"content": `diff --git a/patch-file-1.txt b/patch-file-1.txt
new file mode 100644
index 0000000000..aaaaaaaaaa
@@ -347,7 +351,6 @@ index 0000000000..aaaaaaaaaa
`,
},
"/user2/repo1/_diffpatch/master", map[string]string{
"tree_path": "__dummy__",
"content": `diff --git a/patch-file-2.txt b/patch-file-2.txt
new file mode 100644
index 0000000000..bbbbbbbbbb
@@ -358,6 +361,10 @@ index 0000000000..bbbbbbbbbb
`,
},
)
for _, resp := range []*httptest.ResponseRecorder{patchResp1, patchResp2} {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "/user2/repo1/src/branch/master", test.RedirectURL(resp))
}
commit1, err := gitRepo.GetCommitByPath(t.Context(), "patch-file-1.txt")
require.NoError(t, err)