@@ -13,6 +13,7 @@ import (
13
13
git_model "code.gitea.io/gitea/models/git"
14
14
repo_model "code.gitea.io/gitea/models/repo"
15
15
"code.gitea.io/gitea/models/unit"
16
+ user_model "code.gitea.io/gitea/models/user"
16
17
"code.gitea.io/gitea/modules/charset"
17
18
"code.gitea.io/gitea/modules/git"
18
19
"code.gitea.io/gitea/modules/json"
@@ -102,10 +103,32 @@ func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
102
103
return treeNames, treePaths
103
104
}
104
105
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) {
107
119
ctx.Data["PageIsEdit"] = true
108
120
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)
109
132
canCommit := renderCommitRights(ctx)
110
133
111
134
treePath := cleanUploadFileName(ctx.Repo.TreePath)
@@ -174,28 +197,19 @@ func editFile(ctx *context.Context, isNewFile bool) {
174
197
ctx.Data["FileContent"] = content
175
198
}
176
199
} 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.
178
201
treeNames = append(treeNames, fileName)
179
202
}
180
203
181
204
ctx.Data["TreeNames"] = treeNames
182
205
ctx.Data["TreePaths"] = treePaths
183
- ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL()
184
206
ctx.Data["commit_summary"] = ""
185
207
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)
191
209
ctx.Data["new_branch_name"] = GetUniquePatchBranchName(ctx)
192
210
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)
196
211
197
- ctx.Data["IsEditingFileOnly"] = ctx.FormString("return_uri") != ""
198
- ctx.Data["ReturnURI"] = ctx.FormString("return_uri")
212
+ ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, treePath)
199
213
200
214
ctx.HTML(http.StatusOK, tplEditFile)
201
215
}
@@ -224,36 +238,33 @@ func NewFile(ctx *context.Context) {
224
238
}
225
239
226
240
func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile bool) {
241
+ editFileCommon(ctx, isNewFile)
242
+ ctx.Data["PageHasPosted"] = true
243
+
227
244
canCommit := renderCommitRights(ctx)
228
245
treeNames, treePaths := getParentTreeFields(form.TreePath)
229
246
branchName := ctx.Repo.BranchName
230
247
if form.CommitChoice == frmCommitChoiceNewBranch {
231
248
branchName = form.NewBranchName
232
249
}
233
250
234
- ctx.Data["PageIsEdit"] = true
235
- ctx.Data["PageHasPosted"] = true
236
- ctx.Data["IsNewFile"] = isNewFile
237
251
ctx.Data["TreePath"] = form.TreePath
238
252
ctx.Data["TreeNames"] = treeNames
239
253
ctx.Data["TreePaths"] = treePaths
240
- ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(ctx.Repo.BranchName)
241
254
ctx.Data["FileContent"] = form.Content
242
255
ctx.Data["commit_summary"] = form.CommitSummary
243
256
ctx.Data["commit_message"] = form.CommitMessage
244
257
ctx.Data["commit_choice"] = form.CommitChoice
245
258
ctx.Data["new_branch_name"] = form.NewBranchName
246
259
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, ",")
249
260
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, form.TreePath)
250
261
251
262
if ctx.HasError() {
252
263
ctx.HTML(http.StatusOK, tplEditFile)
253
264
return
254
265
}
255
266
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
257
268
if branchName == ctx.Repo.BranchName && !canCommit {
258
269
ctx.Data["Err_NewBranchName"] = true
259
270
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
@@ -276,6 +287,17 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
276
287
message += "\n\n" + form.CommitMessage
277
288
}
278
289
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
+
279
301
operation := "update"
280
302
if isNewFile {
281
303
operation = "create"
@@ -294,7 +316,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
294
316
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content, "\r", "")),
295
317
},
296
318
},
297
- Signoff: form.Signoff,
319
+ Signoff: form.Signoff,
320
+ Author: gitCommitter,
321
+ Committer: gitCommitter,
298
322
}); err != nil {
299
323
// This is where we handle all the errors thrown by files_service.ChangeRepoFiles
300
324
if git.IsErrNotExist(err) {
0 commit comments