@@ -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,35 @@ 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+ } else {
116+ emails = append (emails , placeholderMail )
117+ }
118+ return emails
119+ }
120+
121+ func editFileCommon (ctx * context.Context , isNewFile bool ) {
107122 ctx .Data ["PageIsEdit" ] = true
108123 ctx .Data ["IsNewFile" ] = isNewFile
124+ ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
125+ ctx .Data ["PreviewableExtensions" ] = strings .Join (markup .PreviewableExtensions (), "," )
126+ ctx .Data ["LineWrapExtensions" ] = strings .Join (setting .Repository .Editor .LineWrapExtensions , "," )
127+ ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
128+ ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
129+ ctx .Data ["CommitCandidateEmails" ] = getCandidateEmailAddresses (ctx )
130+ ctx .Data ["CommitDefaultEmail" ] = ctx .Doer .GetEmail ()
131+ }
132+
133+ func editFile (ctx * context.Context , isNewFile bool ) {
134+ editFileCommon (ctx , isNewFile )
109135 canCommit := renderCommitRights (ctx )
110136
111137 treePath := cleanUploadFileName (ctx .Repo .TreePath )
@@ -174,28 +200,19 @@ func editFile(ctx *context.Context, isNewFile bool) {
174200 ctx .Data ["FileContent" ] = content
175201 }
176202 } else {
177- // Append filename from query, or empty string to allow user name the new file.
203+ // Append filename from query, or empty string to allow username the new file.
178204 treeNames = append (treeNames , fileName )
179205 }
180206
181207 ctx .Data ["TreeNames" ] = treeNames
182208 ctx .Data ["TreePaths" ] = treePaths
183- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .RefTypeNameSubURL ()
184209 ctx .Data ["commit_summary" ] = ""
185210 ctx .Data ["commit_message" ] = ""
186- if canCommit {
187- ctx .Data ["commit_choice" ] = frmCommitChoiceDirect
188- } else {
189- ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
190- }
211+ ctx .Data ["commit_choice" ] = util .Iif (canCommit , frmCommitChoiceDirect , frmCommitChoiceNewBranch )
191212 ctx .Data ["new_branch_name" ] = GetUniquePatchBranchName (ctx )
192213 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 )
196214
197- ctx .Data ["IsEditingFileOnly" ] = ctx .FormString ("return_uri" ) != ""
198- ctx .Data ["ReturnURI" ] = ctx .FormString ("return_uri" )
215+ ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , treePath )
199216
200217 ctx .HTML (http .StatusOK , tplEditFile )
201218}
@@ -224,36 +241,33 @@ func NewFile(ctx *context.Context) {
224241}
225242
226243func editFilePost (ctx * context.Context , form forms.EditRepoFileForm , isNewFile bool ) {
244+ editFileCommon (ctx , isNewFile )
245+ ctx .Data ["PageHasPosted" ] = true
246+
227247 canCommit := renderCommitRights (ctx )
228248 treeNames , treePaths := getParentTreeFields (form .TreePath )
229249 branchName := ctx .Repo .BranchName
230250 if form .CommitChoice == frmCommitChoiceNewBranch {
231251 branchName = form .NewBranchName
232252 }
233253
234- ctx .Data ["PageIsEdit" ] = true
235- ctx .Data ["PageHasPosted" ] = true
236- ctx .Data ["IsNewFile" ] = isNewFile
237254 ctx .Data ["TreePath" ] = form .TreePath
238255 ctx .Data ["TreeNames" ] = treeNames
239256 ctx .Data ["TreePaths" ] = treePaths
240- ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/branch/" + util .PathEscapeSegments (ctx .Repo .BranchName )
241257 ctx .Data ["FileContent" ] = form .Content
242258 ctx .Data ["commit_summary" ] = form .CommitSummary
243259 ctx .Data ["commit_message" ] = form .CommitMessage
244260 ctx .Data ["commit_choice" ] = form .CommitChoice
245261 ctx .Data ["new_branch_name" ] = form .NewBranchName
246262 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 , "," )
249263 ctx .Data ["EditorconfigJson" ] = GetEditorConfig (ctx , form .TreePath )
250264
251265 if ctx .HasError () {
252266 ctx .HTML (http .StatusOK , tplEditFile )
253267 return
254268 }
255269
256- // Cannot commit to a an existing branch if user doesn't have rights
270+ // Cannot commit to an existing branch if user doesn't have rights
257271 if branchName == ctx .Repo .BranchName && ! canCommit {
258272 ctx .Data ["Err_NewBranchName" ] = true
259273 ctx .Data ["commit_choice" ] = frmCommitChoiceNewBranch
@@ -276,6 +290,17 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
276290 message += "\n \n " + form .CommitMessage
277291 }
278292
293+ gitCommitter := & files_service.IdentityOptions {}
294+ if form .CommitEmail != "" {
295+ if util .SliceContainsString (getCandidateEmailAddresses (ctx ), form .CommitEmail , true ) {
296+ gitCommitter .GitUserEmail = form .CommitEmail
297+ } else {
298+ ctx .Data ["Err_CommitEmail" ] = true
299+ ctx .RenderWithErr (ctx .Tr ("repo.editor.invalid_commit_email" ), tplEditFile , & form )
300+ return
301+ }
302+ }
303+
279304 operation := "update"
280305 if isNewFile {
281306 operation = "create"
@@ -294,7 +319,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
294319 ContentReader : strings .NewReader (strings .ReplaceAll (form .Content , "\r " , "" )),
295320 },
296321 },
297- Signoff : form .Signoff ,
322+ Signoff : form .Signoff ,
323+ Author : gitCommitter ,
324+ Committer : gitCommitter ,
298325 }); err != nil {
299326 // This is where we handle all the errors thrown by files_service.ChangeRepoFiles
300327 if git .IsErrNotExist (err ) {
0 commit comments