@@ -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,33 @@ 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+ placeholderMail := ctx .Doer .GetPlaceholderEmail ()
113+ if ctx .Doer .KeepEmailPrivate {
114+ emails = append ([]string {placeholderMail }, emails ... )
115+ }
116+ return emails
117+ }
118+
119+ func editFileCommon (ctx * context.Context , isNewFile bool ) {
107120 ctx .Data ["PageIsEdit" ] = true
108121 ctx .Data ["IsNewFile" ] = isNewFile
122+ ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
123+ ctx .Data ["PreviewableExtensions" ] = strings .Join (markup .PreviewableExtensions (), "," )
124+ ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
125+ ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
126+ ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
127+ ctx .Data ["CommitCandidateEmails" ] = getCandidateEmailAddresses (ctx )
128+ ctx .Data ["CommitDefaultEmail" ] = ctx .Doer .GetEmail ()
129+ }
130+
131+ func editFile (ctx * context.Context , isNewFile bool ) {
132+ editFileCommon (ctx , isNewFile )
109133 canCommit := renderCommitRights (ctx )
110134
111135 treePath := cleanUploadFileName (ctx .Repo .TreePath )
@@ -174,28 +198,19 @@ func editFile(ctx *context.Context, isNewFile bool) {
174198 ctx .Data ["FileContent" ] = content
175199 }
176200 } else {
177- // Append filename from query, or empty string to allow user name the new file.
201+ // Append filename from query, or empty string to allow username the new file.
178202 treeNames = append (treeNames , fileName )
179203 }
180204
181205 ctx .Data ["TreeNames" ] = treeNames
182206 ctx .Data ["TreePaths" ] = treePaths
183- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
184207 ctx .Data ["commit_summary" ] = ""
185208 ctx .Data ["commit_message" ] = ""
186- if canCommit {
187- ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
188- } else {
189- ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
190- }
209+ ctx .Data ["commit_choice" ] = util .Iif (canCommit , frmCommitChoiceDirect , frmCommitChoiceNewBranch )
191210 ctx .Data ["new_branch_name" ] = GetUniquePatchBranchName (ctx )
192211 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 )
196212
197- ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
198- ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
213+ ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , treePath )
199214
200215 ctx .HTML (http .StatusOK , tplEditFile )
201216}
@@ -224,36 +239,33 @@ func NewFile(ctx *context.Context) {
224239}
225240
226241func editFilePost (ctx * context.Context , form forms.EditRepoFileForm , isNewFile bool ) {
242+ editFileCommon (ctx , isNewFile )
243+ ctx .Data ["PageHasPosted" ] = true
244+
227245 canCommit := renderCommitRights (ctx )
228246 treeNames , treePaths := getParentTreeFields (form .TreePath )
229247 branchName := ctx .Repo .BranchName
230248 if form .CommitChoice == frmCommitChoiceNewBranch {
231249 branchName = form .NewBranchName
232250 }
233251
234- ctx .Data ["PageIsEdit" ] = true
235- ctx .Data ["PageHasPosted" ] = true
236- ctx .Data ["IsNewFile" ] = isNewFile
237252 ctx .Data ["TreePath" ] = form .TreePath
238253 ctx .Data ["TreeNames" ] = treeNames
239254 ctx .Data ["TreePaths" ] = treePaths
240- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (ctx .Repo .BranchName )
241255 ctx .Data ["FileContent" ] = form .Content
242256 ctx .Data ["commit_summary" ] = form .CommitSummary
243257 ctx .Data ["commit_message" ] = form .CommitMessage
244258 ctx .Data ["commit_choice" ] = form .CommitChoice
245259 ctx .Data ["new_branch_name" ] = form .NewBranchName
246260 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 , "," )
249261 ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , form .TreePath )
250262
251263 if ctx .HasError () {
252264 ctx .HTML (http .StatusOK , tplEditFile )
253265 return
254266 }
255267
256- // Cannot commit to a an existing branch if user doesn't have rights
268+ // Cannot commit to an existing branch if user doesn't have rights
257269 if branchName == ctx .Repo .BranchName && ! canCommit {
258270 ctx .Data ["Err_NewBranchName" ] = true
259271 ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
@@ -276,6 +288,17 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
276288 message += "\n \n " + form .CommitMessage
277289 }
278290
291+ gitCommitter := & files_service.IdentityOptions {}
292+ if form .CommitEmail != "" {
293+ if util .SliceContainsString (getCandidateEmailAddresses (ctx ), form .CommitEmail , true ) {
294+ gitCommitter .GitUserEmail = form .CommitEmail
295+ } else {
296+ ctx .Data ["Err_CommitEmail" ] = true
297+ ctx .RenderWithErr (ctx .Tr ("repo.editor.invalid_commit_email" ), tplEditFile , & form )
298+ return
299+ }
300+ }
301+
279302 operation := "update"
280303 if isNewFile {
281304 operation = "create"
@@ -294,7 +317,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
294317 ContentReader : strings .NewReader (strings .ReplaceAll (form .Content , "\r " , "" )),
295318 },
296319 },
297- Signoff : form .Signoff ,
320+ Signoff : form .Signoff ,
321+ Author : gitCommitter ,
322+ Committer : gitCommitter ,
298323 }); err != nil {
299324 // This is where we handle all the errors thrown by files_service.ChangeRepoFiles
300325 if git .IsErrNotExist (err ) {
0 commit comments