@@ -13,6 +13,7 @@ import (
1313 git_model "code.gitea.io/gitea/models/git"
1414 repo_model "code.gitea.io/gitea/models/repo"
1515 "code.gitea.io/gitea/models/unit"
16+ user_model "code.gitea.io/gitea/models/user"
1617 "code.gitea.io/gitea/modules/charset"
1718 "code.gitea.io/gitea/modules/git"
1819 "code.gitea.io/gitea/modules/json"
@@ -102,10 +103,32 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
102103 return treeNames , treePaths
103104}
104105
105- func editFile (ctx * context.Context , isNewFile bool ) {
106- ctx .Data ["PageIsViewCode" ] = true
106+ func getCandidateEmailAddresses (ctx * context.Context ) []string {
107+ emails , err := user_model .GetActivatedEmailAddresses (ctx , ctx .Doer .ID )
108+ if err != nil {
109+ log .Error ("getCandidateEmailAddresses: GetActivatedEmailAddresses: %v" , err )
110+ }
111+
112+ if ctx .Doer .KeepEmailPrivate {
113+ emails = append ([]string {ctx .Doer .GetPlaceholderEmail ()}, emails ... )
114+ }
115+ return emails
116+ }
117+
118+ func editFileCommon (ctx * context.Context , isNewFile bool ) {
107119 ctx .Data ["PageIsEdit" ] = true
108120 ctx .Data ["IsNewFile" ] = isNewFile
121+ ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
122+ ctx .Data ["PreviewableExtensions" ] = strings .Join (markup .PreviewableExtensions (), "," )
123+ ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
124+ ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
125+ ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
126+ ctx .Data ["CommitCandidateEmails" ] = getCandidateEmailAddresses (ctx )
127+ ctx .Data ["CommitDefaultEmail" ] = ctx .Doer .GetEmail ()
128+ }
129+
130+ func editFile (ctx * context.Context , isNewFile bool ) {
131+ editFileCommon (ctx , isNewFile )
109132 canCommit := renderCommitRights (ctx )
110133
111134 treePath := cleanUploadFileName (ctx .Repo .TreePath )
@@ -174,28 +197,19 @@ func editFile(ctx *context.Context, isNewFile bool) {
174197 ctx .Data ["FileContent" ] = content
175198 }
176199 } else {
177- // Append filename from query, or empty string to allow user name the new file.
200+ // Append filename from query, or empty string to allow username the new file.
178201 treeNames = append (treeNames , fileName )
179202 }
180203
181204 ctx .Data ["TreeNames" ] = treeNames
182205 ctx .Data ["TreePaths" ] = treePaths
183- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
184206 ctx .Data ["commit_summary" ] = ""
185207 ctx .Data ["commit_message" ] = ""
186- if canCommit {
187- ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
188- } else {
189- ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
190- }
208+ ctx .Data ["commit_choice" ] = util .Iif (canCommit , frmCommitChoiceDirect , frmCommitChoiceNewBranch )
191209 ctx .Data ["new_branch_name" ] = GetUniquePatchBranchName (ctx )
192210 ctx .Data ["last_commit" ] = ctx .Repo .CommitID
193- ctx .Data ["PreviewableExtensions" ] = strings .Join (markup .PreviewableExtensions (), "," )
194- ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
195- ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , treePath )
196211
197- ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
198- ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
212+ ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , treePath )
199213
200214 ctx .HTML (http .StatusOK , tplEditFile )
201215}
@@ -224,36 +238,33 @@ func NewFile(ctx *context.Context) {
224238}
225239
226240func editFilePost (ctx * context.Context , form forms.EditRepoFileForm , isNewFile bool ) {
241+ editFileCommon (ctx , isNewFile )
242+ ctx .Data ["PageHasPosted" ] = true
243+
227244 canCommit := renderCommitRights (ctx )
228245 treeNames , treePaths := getParentTreeFields (form .TreePath )
229246 branchName := ctx .Repo .BranchName
230247 if form .CommitChoice == frmCommitChoiceNewBranch {
231248 branchName = form .NewBranchName
232249 }
233250
234- ctx .Data ["PageIsEdit" ] = true
235- ctx .Data ["PageHasPosted" ] = true
236- ctx .Data ["IsNewFile" ] = isNewFile
237251 ctx .Data ["TreePath" ] = form .TreePath
238252 ctx .Data ["TreeNames" ] = treeNames
239253 ctx .Data ["TreePaths" ] = treePaths
240- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (ctx .Repo .BranchName )
241254 ctx .Data ["FileContent" ] = form .Content
242255 ctx .Data ["commit_summary" ] = form .CommitSummary
243256 ctx .Data ["commit_message" ] = form .CommitMessage
244257 ctx .Data ["commit_choice" ] = form .CommitChoice
245258 ctx .Data ["new_branch_name" ] = form .NewBranchName
246259 ctx .Data ["last_commit" ] = ctx .Repo .CommitID
247- ctx .Data ["PreviewableExtensions" ] = strings .Join (markup .PreviewableExtensions (), "," )
248- ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
249260 ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , form .TreePath )
250261
251262 if ctx .HasError () {
252263 ctx .HTML (http .StatusOK , tplEditFile )
253264 return
254265 }
255266
256- // Cannot commit to a an existing branch if user doesn't have rights
267+ // Cannot commit to an existing branch if user doesn't have rights
257268 if branchName == ctx .Repo .BranchName && ! canCommit {
258269 ctx .Data ["Err_NewBranchName" ] = true
259270 ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
@@ -276,6 +287,17 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
276287 message += "\n \n " + form .CommitMessage
277288 }
278289
290+ gitCommitter := & files_service.IdentityOptions {}
291+ if form .CommitEmail != "" {
292+ if util .SliceContainsString (getCandidateEmailAddresses (ctx ), form .CommitEmail , true ) {
293+ gitCommitter .GitUserEmail = form .CommitEmail
294+ } else {
295+ ctx .Data ["Err_CommitEmail" ] = true
296+ ctx .RenderWithErr (ctx .Tr ("repo.editor.invalid_commit_email" ), tplEditFile , & form )
297+ return
298+ }
299+ }
300+
279301 operation := "update"
280302 if isNewFile {
281303 operation = "create"
@@ -294,7 +316,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
294316 ContentReader : strings .NewReader (strings .ReplaceAll (form .Content , "\r " , "" )),
295317 },
296318 },
297- Signoff : form .Signoff ,
319+ Signoff : form .Signoff ,
320+ Author : gitCommitter ,
321+ Committer : gitCommitter ,
298322 }); err != nil {
299323 // This is where we handle all the errors thrown by files_service.ChangeRepoFiles
300324 if git .IsErrNotExist (err ) {
0 commit comments