mirror of
https://github.com/go-gitea/gitea
synced 2025-07-23 02:38:35 +00:00
Support choose email when creating a commit via web UI (more) (#33445)
Follow #33432
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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) {
|
||||
|
40
routers/web/repo/webgit.go
Normal file
40
routers/web/repo/webgit.go
Normal file
@@ -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
|
||||
}
|
Reference in New Issue
Block a user