aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-01-31 10:36:18 +0800
committerGitHub <noreply@github.com>2025-01-31 02:36:18 +0000
commit5a7b42dac7aa073de2d8665c1ced429cf52e3213 (patch)
treefb69bcb259c553ad49c463a08cd6a0d2b35a91b9 /routers/web/repo
parentb57d9f41d4936382a985b41b4a74df463abd983a (diff)
downloadgitea-5a7b42dac7aa073de2d8665c1ced429cf52e3213.tar.gz
gitea-5a7b42dac7aa073de2d8665c1ced429cf52e3213.zip
Support choose email when creating a commit via web UI (more) (#33445)
Follow #33432
Diffstat (limited to 'routers/web/repo')
-rw-r--r--routers/web/repo/cherry_pick.go8
-rw-r--r--routers/web/repo/editor.go51
-rw-r--r--routers/web/repo/patch.go11
-rw-r--r--routers/web/repo/webgit.go40
4 files changed, 83 insertions, 27 deletions
diff --git a/routers/web/repo/cherry_pick.go b/routers/web/repo/cherry_pick.go
index 33d941c9d8..616bb8c6cb 100644
--- a/routers/web/repo/cherry_pick.go
+++ b/routers/web/repo/cherry_pick.go
@@ -114,11 +114,19 @@ func CherryPickPost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage
}
+ gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
+ if !valid {
+ ctx.Data["Err_CommitEmail"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplCherryPick, &form)
+ return
+ }
opts := &files.ApplyDiffPatchOptions{
LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName,
NewBranch: branchName,
Message: message,
+ Author: gitCommitter,
+ Committer: gitCommitter,
}
// First lets try the simple plain read-tree -m approach
diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go
index 48e041fb1d..cc4ffc698d 100644
--- a/routers/web/repo/editor.go
+++ b/routers/web/repo/editor.go
@@ -13,7 +13,6 @@ import (
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
- user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
@@ -103,18 +102,6 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
return treeNames, treePaths
}
-func getCandidateEmailAddresses(ctx *context.Context) []string {
- emails, err := user_model.GetActivatedEmailAddresses(ctx, ctx.Doer.ID)
- if err != nil {
- log.Error("getCandidateEmailAddresses: GetActivatedEmailAddresses: %v", err)
- }
-
- if ctx.Doer.KeepEmailPrivate {
- emails = append([]string{ctx.Doer.GetPlaceholderEmail()}, emails...)
- }
- return emails
-}
-
func editFileCommon(ctx *context.Context, isNewFile bool) {
ctx.Data["PageIsEdit"] = true
ctx.Data["IsNewFile"] = isNewFile
@@ -123,8 +110,6 @@ func editFileCommon(ctx *context.Context, isNewFile bool) {
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
ctx.Data["IsEditingFileOnly"] = ctx.FormString("return_uri") != ""
ctx.Data["ReturnURI"] = ctx.FormString("return_uri")
- ctx.Data["CommitCandidateEmails"] = getCandidateEmailAddresses(ctx)
- ctx.Data["CommitDefaultEmail"] = ctx.Doer.GetEmail()
}
func editFile(ctx *context.Context, isNewFile bool) {
@@ -287,15 +272,11 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
message += "\n\n" + form.CommitMessage
}
- gitCommitter := &files_service.IdentityOptions{}
- if form.CommitEmail != "" {
- if util.SliceContainsString(getCandidateEmailAddresses(ctx), form.CommitEmail, true) {
- gitCommitter.GitUserEmail = form.CommitEmail
- } else {
- ctx.Data["Err_CommitEmail"] = true
- ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplEditFile, &form)
- return
- }
+ gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
+ if !valid {
+ ctx.Data["Err_CommitEmail"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplEditFile, &form)
+ return
}
operation := "update"
@@ -515,6 +496,13 @@ func DeleteFilePost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage
}
+ gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
+ if !valid {
+ ctx.Data["Err_CommitEmail"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplDeleteFile, &form)
+ return
+ }
+
if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName,
@@ -525,8 +513,10 @@ func DeleteFilePost(ctx *context.Context) {
TreePath: ctx.Repo.TreePath,
},
},
- Message: message,
- Signoff: form.Signoff,
+ Message: message,
+ Signoff: form.Signoff,
+ Author: gitCommitter,
+ Committer: gitCommitter,
}); err != nil {
// This is where we handle all the errors thrown by repofiles.DeleteRepoFile
if git.IsErrNotExist(err) || files_service.IsErrRepoFileDoesNotExist(err) {
@@ -726,6 +716,13 @@ func UploadFilePost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage
}
+ gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
+ if !valid {
+ ctx.Data["Err_CommitEmail"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplUploadFile, &form)
+ return
+ }
+
if err := files_service.UploadRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.UploadRepoFileOptions{
LastCommitID: ctx.Repo.CommitID,
OldBranch: oldBranchName,
@@ -734,6 +731,8 @@ func UploadFilePost(ctx *context.Context) {
Message: message,
Files: form.Files,
Signoff: form.Signoff,
+ Author: gitCommitter,
+ Committer: gitCommitter,
}); err != nil {
if git_model.IsErrLFSFileLocked(err) {
ctx.Data["Err_TreePath"] = true
diff --git a/routers/web/repo/patch.go b/routers/web/repo/patch.go
index 4d47a705d6..120b3469f6 100644
--- a/routers/web/repo/patch.go
+++ b/routers/web/repo/patch.go
@@ -66,7 +66,7 @@ func NewDiffPatchPost(ctx *context.Context) {
return
}
- // Cannot commit to a an existing branch if user doesn't have rights
+ // Cannot commit to an existing branch if user doesn't have rights
if branchName == ctx.Repo.BranchName && !canCommit {
ctx.Data["Err_NewBranchName"] = true
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
@@ -86,12 +86,21 @@ func NewDiffPatchPost(ctx *context.Context) {
message += "\n\n" + form.CommitMessage
}
+ gitCommitter, valid := WebGitOperationGetCommitChosenEmailIdentity(ctx, form.CommitEmail)
+ if !valid {
+ ctx.Data["Err_CommitEmail"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.editor.invalid_commit_email"), tplPatchFile, &form)
+ return
+ }
+
fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
LastCommitID: form.LastCommit,
OldBranch: ctx.Repo.BranchName,
NewBranch: branchName,
Message: message,
Content: strings.ReplaceAll(form.Content, "\r", ""),
+ Author: gitCommitter,
+ Committer: gitCommitter,
})
if err != nil {
if git_model.IsErrBranchAlreadyExists(err) {
diff --git a/routers/web/repo/webgit.go b/routers/web/repo/webgit.go
new file mode 100644
index 0000000000..5f390197e7
--- /dev/null
+++ b/routers/web/repo/webgit.go
@@ -0,0 +1,40 @@
+// Copyright 2025 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
+ "code.gitea.io/gitea/services/context"
+ files_service "code.gitea.io/gitea/services/repository/files"
+)
+
+func WebGitOperationCommonData(ctx *context.Context) {
+ // TODO: more places like "wiki page" and "merging a pull request or creating an auto merge merging task"
+ emails, err := user_model.GetActivatedEmailAddresses(ctx, ctx.Doer.ID)
+ if err != nil {
+ log.Error("WebGitOperationCommonData: GetActivatedEmailAddresses: %v", err)
+ }
+ if ctx.Doer.KeepEmailPrivate {
+ emails = append([]string{ctx.Doer.GetPlaceholderEmail()}, emails...)
+ }
+ ctx.Data["CommitCandidateEmails"] = emails
+ ctx.Data["CommitDefaultEmail"] = ctx.Doer.GetEmail()
+}
+
+func WebGitOperationGetCommitChosenEmailIdentity(ctx *context.Context, email string) (_ *files_service.IdentityOptions, valid bool) {
+ if ctx.Data["CommitCandidateEmails"] == nil {
+ setting.PanicInDevOrTesting("no CommitCandidateEmails in context data")
+ }
+ emails, _ := ctx.Data["CommitCandidateEmails"].([]string)
+ if email == "" {
+ return nil, true
+ }
+ if util.SliceContainsString(emails, email, true) {
+ return &files_service.IdentityOptions{GitUserEmail: email}, true
+ }
+ return nil, false
+}