Skip to content

Commit 596f06e

Browse files
committed
fine tune target branch check
1 parent eead5c0 commit 596f06e

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

routers/web/repo/editor.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
130130
}
131131

132132
// check commit behavior
133-
targetBranchName := util.Iif(commonForm.CommitChoice == editorCommitChoiceNewBranch, commonForm.NewBranchName, ctx.Repo.BranchName)
133+
fromBaseBranch := ctx.FormString("from_base_branch")
134+
commitToNewBranch := commonForm.CommitChoice == editorCommitChoiceNewBranch || fromBaseBranch != ""
135+
targetBranchName := util.Iif(commitToNewBranch, commonForm.NewBranchName, ctx.Repo.BranchName)
134136
if targetBranchName == ctx.Repo.BranchName && !commitFormOptions.CanCommitToBranch {
135137
ctx.JSONError(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", targetBranchName))
136138
return nil
@@ -143,19 +145,24 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
143145
return nil
144146
}
145147

146-
oldBranchName := ctx.Repo.BranchName
147-
fromBaseBranch := ctx.FormString("from_base_branch")
148-
if fromBaseBranch != "" {
149-
// if target branch exists, we should warn users
148+
if commitToNewBranch {
149+
// if target branch exists, we should stop
150150
targetBranchExists, err := git_model.IsBranchExist(ctx, commitFormOptions.TargetRepo.ID, targetBranchName)
151151
if err != nil {
152152
ctx.ServerError("IsBranchExist", err)
153153
return nil
154-
}
155-
if targetBranchExists {
156-
ctx.JSONError(ctx.Tr("repo.editor.fork_branch_exists", targetBranchName))
154+
} else if targetBranchExists {
155+
if fromBaseBranch != "" {
156+
ctx.JSONError(ctx.Tr("repo.editor.fork_branch_exists", targetBranchName))
157+
} else {
158+
ctx.JSONError(ctx.Tr("repo.editor.branch_already_exists", targetBranchName))
159+
}
157160
return nil
158161
}
162+
}
163+
164+
oldBranchName := ctx.Repo.BranchName
165+
if fromBaseBranch != "" {
159166
err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, commitFormOptions.TargetRepo, targetBranchName)
160167
if err != nil {
161168
log.Error("Unable to editorPushBranchToForkedRepository: %v", err)

tests/integration/editor_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func testEditorCreateFile(t *testing.T) {
6565
testEditorActionPostRequestError(t, session, "/user2/repo1/_new/master/", map[string]string{
6666
"tree_path": "test.txt",
6767
"commit_choice": "commit-to-new-branch",
68-
"new_branch_name": "branch2",
69-
}, `Branch "branch2" already exists in this repository.`)
68+
"new_branch_name": "master",
69+
}, `Branch "master" already exists in this repository.`)
7070
}
7171

7272
func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) {
@@ -153,24 +153,27 @@ func testEditorDiffPreview(t *testing.T) {
153153

154154
func testEditorPatchFile(t *testing.T) {
155155
session := loginUser(t, "user2")
156-
pathContent := `diff --git a/patch-file-1.txt b/patch-file-1.txt
156+
pathContentCommon := `diff --git a/patch-file-1.txt b/patch-file-1.txt
157157
new file mode 100644
158158
index 0000000000..aaaaaaaaaa
159159
--- /dev/null
160160
+++ b/patch-file-1.txt
161161
@@ -0,0 +1 @@
162-
+patched content
163-
`
164-
patchForm := map[string]string{
165-
"content": pathContent,
162+
+`
163+
testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", map[string]string{
164+
"content": pathContentCommon + "patched content\n",
166165
"commit_choice": "commit-to-new-branch",
167166
"new_branch_name": "patched-branch",
168-
}
169-
testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", patchForm)
167+
})
170168
resp := MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/raw/branch/patched-branch/patch-file-1.txt"), http.StatusOK)
171169
assert.Equal(t, "patched content\n", resp.Body.String())
172170

173-
resp = testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", patchForm)
171+
// patch again, it should fail
172+
resp = testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/patched-branch/", map[string]string{
173+
"content": pathContentCommon + "another patched content\n",
174+
"commit_choice": "commit-to-new-branch",
175+
"new_branch_name": "patched-branch-1",
176+
})
174177
assert.Equal(t, "Unable to apply patch", test.ParseJSONError(resp.Body.Bytes()).ErrorMessage)
175178
}
176179

0 commit comments

Comments
 (0)