Skip to content

Commit e2613c0

Browse files
committed
refactor
1 parent 0e34ecc commit e2613c0

File tree

11 files changed

+227
-384
lines changed

11 files changed

+227
-384
lines changed

routers/web/repo/editor.go

Lines changed: 128 additions & 151 deletions
Large diffs are not rendered by default.

routers/web/repo/editor_apply_patch.go

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,41 @@ import (
77
"net/http"
88
"strings"
99

10-
"code.gitea.io/gitea/models/unit"
11-
"code.gitea.io/gitea/modules/util"
12-
"code.gitea.io/gitea/modules/web"
1310
"code.gitea.io/gitea/services/context"
1411
"code.gitea.io/gitea/services/forms"
1512
"code.gitea.io/gitea/services/repository/files"
1613
)
1714

1815
func NewDiffPatch(ctx *context.Context) {
19-
_ = prepareEditorCommitFormOptions(ctx, "_diffpatch")
16+
prepareEditorCommitFormOptions(ctx, "_diffpatch")
2017
if ctx.Written() {
2118
return
2219
}
20+
2321
ctx.Data["PageIsPatch"] = true
2422
ctx.HTML(http.StatusOK, tplPatchFile)
2523
}
2624

2725
// NewDiffPatchPost response for sending patch page
2826
func NewDiffPatchPost(ctx *context.Context) {
29-
form := web.GetForm(ctx).(*forms.EditRepoFileForm)
30-
if ctx.HasError() {
31-
ctx.JSONError(ctx.GetErrMsg())
32-
return
33-
}
34-
35-
formOpts := prepareEditorCommitFormOptions(ctx, "_diffpatch")
27+
form, parsed := parseEditorCommitSubmittedForm[*forms.EditRepoFileForm](ctx)
3628
if ctx.Written() {
3729
return
3830
}
3931

40-
branchName := util.Iif(form.CommitChoice == editorCommitChoiceNewBranch, form.NewBranchName, ctx.Repo.BranchName)
41-
if branchName == ctx.Repo.BranchName && !formOpts.CommitFormBehaviors.CanCommitToBranch {
42-
ctx.JSONError(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName))
43-
return
44-
}
45-
46-
commitMessage := buildEditorCommitMessage(ctx.Locale.TrString("repo.editor.patch"), form.CommitSummary, form.CommitMessage)
47-
48-
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
49-
if !valid {
50-
ctx.Data["Err_CommitEmail"] = true
51-
ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplPatchFile, &form)
52-
return
53-
}
54-
55-
fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
32+
defaultCommitMessage := ctx.Locale.TrString("repo.editor.patch")
33+
_, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
5634
LastCommitID: form.LastCommit,
5735
OldBranch: ctx.Repo.BranchName,
58-
NewBranch: branchName,
59-
Message: commitMessage,
60-
Content: strings.ReplaceAll(form.Content.Value(), "\r", ""),
61-
Author: gitCommitter,
62-
Committer: gitCommitter,
36+
NewBranch: parsed.TargetBranchName,
37+
Message: parsed.GetCommitMessage(defaultCommitMessage),
38+
Content: strings.ReplaceAll(form.Content.Value(), "\r\n", "\n"),
39+
Author: parsed.GitCommitter,
40+
Committer: parsed.GitCommitter,
6341
})
6442
if err != nil {
65-
editorHandleFileOperationError(ctx, branchName, err)
43+
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
6644
return
6745
}
68-
69-
if form.CommitChoice == editorCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
70-
ctx.JSONRedirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
71-
} else {
72-
ctx.JSONRedirect(ctx.Repo.RepoLink + "/commit/" + fileResponse.Commit.SHA)
73-
}
46+
redirectForCommitChoice(ctx, parsed, form.TreePath)
7447
}

routers/web/repo/editor_cherry_pick.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ import (
88
"net/http"
99
"strings"
1010

11-
"code.gitea.io/gitea/models/unit"
1211
"code.gitea.io/gitea/modules/git"
1312
"code.gitea.io/gitea/modules/util"
14-
"code.gitea.io/gitea/modules/web"
1513
"code.gitea.io/gitea/services/context"
1614
"code.gitea.io/gitea/services/forms"
1715
"code.gitea.io/gitea/services/repository/files"
1816
)
1917

2018
func CherryPick(ctx *context.Context) {
21-
_ = prepareEditorCommitFormOptions(ctx, "_cherrypick")
19+
prepareEditorCommitFormOptions(ctx, "_cherrypick")
2220
if ctx.Written() {
2321
return
2422
}
@@ -46,41 +44,20 @@ func CherryPick(ctx *context.Context) {
4644
}
4745

4846
func CherryPickPost(ctx *context.Context) {
49-
form := web.GetForm(ctx).(*forms.CherryPickForm)
50-
if ctx.HasError() {
51-
ctx.JSONError(ctx.GetErrMsg())
52-
return
53-
}
54-
55-
formOpts := prepareEditorCommitFormOptions(ctx, "_cherrypick")
56-
if ctx.Written() {
57-
return
58-
}
59-
6047
fromCommitID := ctx.PathParam("sha")
61-
62-
branchName := util.Iif(form.CommitChoice == editorCommitChoiceNewBranch, form.NewBranchName, ctx.Repo.BranchName)
63-
if branchName == ctx.Repo.BranchName && !formOpts.CommitFormBehaviors.CanCommitToBranch {
64-
ctx.JSONError(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName))
65-
return
66-
}
67-
68-
defaultMessage := util.Iif(form.Revert, ctx.Locale.TrString("repo.commit.revert-header", fromCommitID), ctx.Locale.TrString("repo.commit.cherry-pick-header", fromCommitID))
69-
commitMessage := buildEditorCommitMessage(defaultMessage, form.CommitSummary, form.CommitMessage)
70-
71-
gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
72-
if !valid {
73-
ctx.JSONError(ctx.Tr("repo.editor.invalid_commit_email"))
48+
form, parsed := parseEditorCommitSubmittedForm[*forms.CherryPickForm](ctx)
49+
if ctx.Written() {
7450
return
7551
}
7652

53+
defaultCommitMessage := util.Iif(form.Revert, ctx.Locale.TrString("repo.commit.revert-header", fromCommitID), ctx.Locale.TrString("repo.commit.cherry-pick-header", fromCommitID))
7754
opts := &files.ApplyDiffPatchOptions{
7855
LastCommitID: form.LastCommit,
7956
OldBranch: ctx.Repo.BranchName,
80-
NewBranch: branchName,
81-
Message: commitMessage,
82-
Author: gitCommitter,
83-
Committer: gitCommitter,
57+
NewBranch: parsed.TargetBranchName,
58+
Message: parsed.GetCommitMessage(defaultCommitMessage),
59+
Author: parsed.GitCommitter,
60+
Committer: parsed.GitCommitter,
8461
}
8562

8663
// First try the simple plain read-tree -m approach
@@ -98,14 +75,9 @@ func CherryPickPost(ctx *context.Context) {
9875
_, err = files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, opts)
9976
}
10077
if err != nil {
101-
editorHandleFileOperationError(ctx, branchName, err)
78+
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
10279
return
10380
}
10481
}
105-
106-
if form.CommitChoice == editorCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
107-
ctx.JSONRedirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
108-
} else {
109-
ctx.JSONRedirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName))
110-
}
82+
redirectForCommitChoice(ctx, parsed, form.TreePath)
11183
}

routers/web/repo/editor_preview.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ package repo
66
import (
77
"net/http"
88

9-
"code.gitea.io/gitea/modules/web"
109
"code.gitea.io/gitea/services/context"
11-
"code.gitea.io/gitea/services/forms"
1210
files_service "code.gitea.io/gitea/services/repository/files"
1311
)
1412

1513
// DiffPreviewPost render preview diff page
1614
func DiffPreviewPost(ctx *context.Context) {
17-
form := web.GetForm(ctx).(*forms.EditPreviewDiffForm)
15+
content := ctx.FormString("content")
1816
treePath := files_service.CleanGitTreePath(ctx.Repo.TreePath)
1917
if treePath == "" {
2018
ctx.HTTPError(http.StatusBadRequest, "file name to diff is invalid")
@@ -30,7 +28,7 @@ func DiffPreviewPost(ctx *context.Context) {
3028
return
3129
}
3230

33-
diff, err := files_service.GetDiffPreview(ctx, ctx.Repo.Repository, ctx.Repo.BranchName, treePath, form.Content)
31+
diff, err := files_service.GetDiffPreview(ctx, ctx.Repo.Repository, ctx.Repo.BranchName, treePath, content)
3432
if err != nil {
3533
ctx.ServerError("GetDiffPreview", err)
3634
return

routers/web/repo/editor_uploader.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99
repo_model "code.gitea.io/gitea/models/repo"
1010
"code.gitea.io/gitea/modules/setting"
1111
"code.gitea.io/gitea/modules/util"
12-
"code.gitea.io/gitea/modules/web"
1312
"code.gitea.io/gitea/services/context"
1413
"code.gitea.io/gitea/services/context/upload"
15-
"code.gitea.io/gitea/services/forms"
1614
files_service "code.gitea.io/gitea/services/repository/files"
1715
)
1816

@@ -54,12 +52,8 @@ func UploadFileToServer(ctx *context.Context) {
5452

5553
// RemoveUploadFileFromServer remove file from server file dir
5654
func RemoveUploadFileFromServer(ctx *context.Context) {
57-
form := web.GetForm(ctx).(*forms.RemoveUploadFileForm)
58-
if form.File == "" {
59-
ctx.Status(http.StatusNoContent)
60-
return
61-
}
62-
if err := repo_model.DeleteUploadByUUID(ctx, form.File); err != nil {
55+
fileUUID := ctx.FormString("file")
56+
if err := repo_model.DeleteUploadByUUID(ctx, fileUUID); err != nil {
6357
ctx.ServerError("DeleteUploadByUUID", err)
6458
return
6559
}

routers/web/repo/editor_util.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/json"
1616
"code.gitea.io/gitea/modules/log"
17-
"code.gitea.io/gitea/modules/util"
1817
context_service "code.gitea.io/gitea/services/context"
1918
)
2019

@@ -84,12 +83,3 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
8483
}
8584
return treeNames, treePaths
8685
}
87-
88-
func buildEditorCommitMessage(def, summary, body string) string {
89-
message := util.IfZero(strings.TrimSpace(summary), def)
90-
body = strings.TrimSpace(body)
91-
if body != "" {
92-
message += "\n\n" + body
93-
}
94-
return message
95-
}

routers/web/web.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,11 +1315,11 @@ func registerWebRoutes(m *web.Router) {
13151315
m.Group("/{username}/{reponame}", func() { // repo code
13161316
m.Group("", func() {
13171317
m.Group("", func() {
1318-
m.Post("/_preview/*", web.Bind(forms.EditPreviewDiffForm{}), repo.DiffPreviewPost)
1319-
m.Combo("/_edit/*").Get(repo.EditFile).
1318+
m.Post("/_preview/*", repo.DiffPreviewPost)
1319+
m.Combo("/{editor_action:_edit}/*").Get(repo.EditFile).
1320+
Post(web.Bind(forms.EditRepoFileForm{}), repo.EditFilePost)
1321+
m.Combo("/{editor_action:_new}/*").Get(repo.EditFile).
13201322
Post(web.Bind(forms.EditRepoFileForm{}), repo.EditFilePost)
1321-
m.Combo("/_new/*").Get(repo.NewFile).
1322-
Post(web.Bind(forms.EditRepoFileForm{}), repo.NewFilePost)
13231323
m.Combo("/_delete/*").Get(repo.DeleteFile).
13241324
Post(web.Bind(forms.DeleteRepoFileForm{}), repo.DeleteFilePost)
13251325
m.Combo("/_upload/*", repo.MustBeAbleToUpload).Get(repo.UploadFile).
@@ -1331,7 +1331,7 @@ func registerWebRoutes(m *web.Router) {
13311331
}, context.RepoRefByType(git.RefTypeBranch), context.CanWriteToBranch(), repo.WebGitOperationCommonData)
13321332
m.Group("", func() {
13331333
m.Post("/upload-file", repo.UploadFileToServer)
1334-
m.Post("/upload-remove", web.Bind(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)
1334+
m.Post("/upload-remove", repo.RemoveUploadFileFromServer)
13351335
}, repo.MustBeAbleToUpload, reqRepoCodeWriter)
13361336
}, repo.MustBeEditable, context.RepoMustNotBeArchived())
13371337

0 commit comments

Comments
 (0)