Skip to content

Commit ff65057

Browse files
authored
Merge branch 'main' into fix/editor-handle-pasted-urls
2 parents 87dfeb1 + 439ebe7 commit ff65057

File tree

14 files changed

+561
-269
lines changed

14 files changed

+561
-269
lines changed

models/fixtures/branch.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,15 @@
201201
is_deleted: false
202202
deleted_by_id: 0
203203
deleted_unix: 0
204+
205+
-
206+
id: 25
207+
repo_id: 54
208+
name: 'master'
209+
commit_id: '73cf03db6ece34e12bf91e8853dc58f678f2f82d'
210+
commit_message: 'Initial commit'
211+
commit_time: 1671663402
212+
pusher_id: 2
213+
is_deleted: false
214+
deleted_by_id: 0
215+
deleted_unix: 0

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,9 @@ editor.upload_file = Upload File
13321332
editor.edit_file = Edit File
13331333
editor.preview_changes = Preview Changes
13341334
editor.cannot_edit_lfs_files = LFS files cannot be edited in the web interface.
1335+
editor.cannot_edit_too_large_file = The file is too large to be edited.
13351336
editor.cannot_edit_non_text_files = Binary files cannot be edited in the web interface.
1337+
editor.file_not_editable_hint = But you can still rename or move it.
13361338
editor.edit_this_file = Edit File
13371339
editor.this_file_locked = File is locked
13381340
editor.must_be_on_a_branch = You must be on a branch to make or propose changes to this file.

options/locale/locale_uk-UA.ini

Lines changed: 139 additions & 149 deletions
Large diffs are not rendered by default.

options/locale/locale_zh-CN.ini

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ oauth_signin_submit=绑定账号
463463
oauth.signin.error.general=处理授权请求时出错:%s。如果此错误仍然存在,请与站点管理员联系。
464464
oauth.signin.error.access_denied=授权请求被拒绝。
465465
oauth.signin.error.temporarily_unavailable=授权失败,因为认证服务器暂时不可用。请稍后再试。
466-
oauth_callback_unable_auto_reg=自动注册已启用,但OAuth2 提供商 %[1]s 返回缺失的字段:%[2]s,无法自动创建帐户,请创建或链接到一个帐户,或联系站点管理员。
466+
oauth_callback_unable_auto_reg=自动注册已启用,但 OAuth2 提供商 %[1]s 返回缺失的字段:%[2]s,无法自动创建帐户,请创建或链接到一个帐户,或联系站点管理员。
467467
openid_connect_submit=连接
468468
openid_connect_title=连接到现有的帐户
469469
openid_connect_desc=所选的 OpenID URI 未知。在这里关联一个新帐户。
@@ -1161,8 +1161,8 @@ template.issue_labels=工单标签
11611161
template.one_item=必须至少选择一个模板项
11621162
template.invalid=必须选择一个模板仓库
11631163

1164-
archive.title=该仓库已被归档。您可以查看文件和克隆它,但不能推送、打开工单或合并请求
1165-
archive.title_date=该仓库已于 %s 归档。您可以查看文件或克隆它,但不能推送、打开工单或合并请求
1164+
archive.title=该仓库已被归档。您可以查看文件和克隆它,但不能推送、创建工单或合并请求
1165+
archive.title_date=该仓库已于 %s 归档。您可以查看文件或克隆它,但不能推送、创建工单或合并请求
11661166
archive.issue.nocomment=此仓库已存档,您不能在此工单添加评论。
11671167
archive.pull.nocomment=此仓库已存档,您不能在此合并请求添加评论。
11681168

@@ -1506,7 +1506,7 @@ issues.choose.blank_about=从默认模板创建一个工单。
15061506
issues.choose.ignore_invalid_templates=已忽略无效模板
15071507
issues.choose.invalid_templates=发现了 %v 个无效模板
15081508
issues.choose.invalid_config=问题配置包含错误:
1509-
issues.no_ref=分支/标记未指定
1509+
issues.no_ref=分支/Git标签未指定
15101510
issues.create=创建工单
15111511
issues.new_label=创建标签
15121512
issues.new_label_placeholder=标签名称
@@ -2375,7 +2375,7 @@ settings.event_issues=工单
23752375
settings.event_issues_desc=工单已打开、已关闭、已重新打开或已编辑。
23762376
settings.event_issue_assign=工单已指派
23772377
settings.event_issue_assign_desc=工单已指派或取消指派。
2378-
settings.event_issue_label=已标记工单
2378+
settings.event_issue_label=工单增删标签
23792379
settings.event_issue_label_desc=工单标签已更新或清除。
23802380
settings.event_issue_milestone=工单已收入里程碑中
23812381
settings.event_issue_milestone_desc=工单已收入或取消收入里程碑中。

routers/web/repo/editor.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ func editFile(ctx *context.Context, isNewFile bool) {
145145
}
146146

147147
blob := entry.Blob()
148-
if blob.Size() >= setting.UI.MaxDisplayFileSize {
149-
ctx.NotFound(err)
150-
return
151-
}
152148

153149
buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, blob)
154150
if err != nil {
@@ -162,22 +158,37 @@ func editFile(ctx *context.Context, isNewFile bool) {
162158

163159
defer dataRc.Close()
164160

165-
ctx.Data["FileSize"] = blob.Size()
166-
167-
// Only some file types are editable online as text.
168-
if !fInfo.st.IsRepresentableAsText() || fInfo.isLFSFile {
169-
ctx.NotFound(nil)
170-
return
161+
if fInfo.isLFSFile {
162+
lfsLock, err := git_model.GetTreePathLock(ctx, ctx.Repo.Repository.ID, ctx.Repo.TreePath)
163+
if err != nil {
164+
ctx.ServerError("GetTreePathLock", err)
165+
return
166+
}
167+
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
168+
ctx.NotFound(nil)
169+
return
170+
}
171171
}
172172

173-
d, _ := io.ReadAll(dataRc)
173+
ctx.Data["FileSize"] = fInfo.fileSize
174174

175-
buf = append(buf, d...)
176-
if content, err := charset.ToUTF8(buf, charset.ConvertOpts{KeepBOM: true}); err != nil {
177-
log.Error("ToUTF8: %v", err)
178-
ctx.Data["FileContent"] = string(buf)
175+
// Only some file types are editable online as text.
176+
if fInfo.isLFSFile {
177+
ctx.Data["NotEditableReason"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
178+
} else if !fInfo.st.IsRepresentableAsText() {
179+
ctx.Data["NotEditableReason"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
180+
} else if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
181+
ctx.Data["NotEditableReason"] = ctx.Tr("repo.editor.cannot_edit_too_large_file")
179182
} else {
180-
ctx.Data["FileContent"] = content
183+
d, _ := io.ReadAll(dataRc)
184+
185+
buf = append(buf, d...)
186+
if content, err := charset.ToUTF8(buf, charset.ConvertOpts{KeepBOM: true}); err != nil {
187+
log.Error("ToUTF8: %v", err)
188+
ctx.Data["FileContent"] = string(buf)
189+
} else {
190+
ctx.Data["FileContent"] = content
191+
}
181192
}
182193
} else {
183194
// Append filename from query, or empty string to allow username the new file.
@@ -280,6 +291,10 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
280291
operation := "update"
281292
if isNewFile {
282293
operation = "create"
294+
} else if !form.Content.Has() && ctx.Repo.TreePath != form.TreePath {
295+
// The form content only has data if file is representable as text, is not too large and not in lfs. If it doesn't
296+
// have data, the only possible operation is a rename
297+
operation = "rename"
283298
}
284299

285300
if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
@@ -292,7 +307,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
292307
Operation: operation,
293308
FromTreePath: ctx.Repo.TreePath,
294309
TreePath: form.TreePath,
295-
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content, "\r", "")),
310+
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content.Value(), "\r", "")),
296311
},
297312
},
298313
Signoff: form.Signoff,

routers/web/repo/patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func NewDiffPatchPost(ctx *context.Context) {
9999
OldBranch: ctx.Repo.BranchName,
100100
NewBranch: branchName,
101101
Message: message,
102-
Content: strings.ReplaceAll(form.Content, "\r", ""),
102+
Content: strings.ReplaceAll(form.Content.Value(), "\r", ""),
103103
Author: gitCommitter,
104104
Committer: gitCommitter,
105105
})

routers/web/repo/view_file.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,10 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
285285
}
286286
}
287287

288-
prepareToRenderButtons(ctx, fInfo.isLFSFile, isRepresentableAsText, lfsLock)
288+
prepareToRenderButtons(ctx, lfsLock)
289289
}
290290

291-
func prepareToRenderButtons(ctx *context.Context, isLFSFile, isRepresentableAsText bool, lfsLock *git_model.LFSLock) {
291+
func prepareToRenderButtons(ctx *context.Context, lfsLock *git_model.LFSLock) {
292292
// archived or mirror repository, the buttons should not be shown
293293
if ctx.Repo.Repository.IsArchived || !ctx.Repo.Repository.CanEnableEditor() {
294294
return
@@ -301,33 +301,16 @@ func prepareToRenderButtons(ctx *context.Context, isLFSFile, isRepresentableAsTe
301301
return
302302
}
303303

304-
if isLFSFile {
305-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
306-
} else if !isRepresentableAsText {
307-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
308-
}
309-
310304
if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
311-
if !isLFSFile { // lfs file cannot be edited after fork
312-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
313-
}
305+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
314306
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")
315307
return
316308
}
317309

318310
// it's a lfs file and the user is not the owner of the lock
319-
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
320-
ctx.Data["CanEditFile"] = false
321-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
322-
ctx.Data["CanDeleteFile"] = false
323-
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
324-
return
325-
}
326-
327-
if !isLFSFile { // lfs file cannot be edited
328-
ctx.Data["CanEditFile"] = true
329-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
330-
}
331-
ctx.Data["CanDeleteFile"] = true
332-
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
311+
isLFSLocked := lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID
312+
ctx.Data["CanEditFile"] = !isLFSLocked
313+
ctx.Data["EditFileTooltip"] = util.Iif(isLFSLocked, ctx.Tr("repo.editor.this_file_locked"), ctx.Tr("repo.editor.edit_this_file"))
314+
ctx.Data["CanDeleteFile"] = !isLFSLocked
315+
ctx.Data["DeleteFileTooltip"] = util.Iif(isLFSLocked, ctx.Tr("repo.editor.this_file_locked"), ctx.Tr("repo.editor.delete_this_file"))
333316
}

services/forms/repo_form.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
issues_model "code.gitea.io/gitea/models/issues"
1212
project_model "code.gitea.io/gitea/models/project"
13+
"code.gitea.io/gitea/modules/optional"
1314
"code.gitea.io/gitea/modules/structs"
1415
"code.gitea.io/gitea/modules/web/middleware"
1516
"code.gitea.io/gitea/services/context"
@@ -689,7 +690,7 @@ func (f *NewWikiForm) Validate(req *http.Request, errs binding.Errors) binding.E
689690
// EditRepoFileForm form for changing repository file
690691
type EditRepoFileForm struct {
691692
TreePath string `binding:"Required;MaxSize(500)"`
692-
Content string
693+
Content optional.Option[string]
693694
CommitSummary string `binding:"MaxSize(100)"`
694695
CommitMessage string
695696
CommitChoice string `binding:"Required;MaxSize(50)"`

0 commit comments

Comments
 (0)