From c6598c27d3b8b90f041bc83110a517f4799b7823 Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 2 May 2025 23:22:12 +0200 Subject: [PATCH] Enable path editing of non text files --- options/locale/locale_en-US.ini | 4 ++- routers/web/repo/editor.go | 11 ++----- routers/web/repo/view_file.go | 33 +++++++++---------- templates/repo/editor/edit.tmpl | 58 +++++++++++++++++++++------------ 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a8fabc9ca1..029ffc4c6f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1331,7 +1331,6 @@ editor.upload_file = Upload File editor.edit_file = Edit File editor.preview_changes = Preview Changes editor.cannot_edit_lfs_files = LFS files cannot be edited in the web interface. -editor.cannot_edit_non_text_files = Binary files cannot be edited in the web interface. editor.edit_this_file = Edit File editor.this_file_locked = File is locked editor.must_be_on_a_branch = You must be on a branch to make or propose changes to this file. @@ -1395,6 +1394,9 @@ editor.user_no_push_to_branch = User cannot push to branch editor.require_signed_commit = Branch requires a signed commit editor.cherry_pick = Cherry-pick %s onto: editor.revert = Revert %s onto: +editor.file_too_large_not_editable = The file is too large to be edited. +editor.binary_file_not_editable = Binary file content is not editable. +editor.file_not_editable_hint = But you can still rename or move it. commits.desc = Browse source code change history. commits.commits = Commits diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index c925b61151..81c2cd8e69 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -146,11 +146,6 @@ func editFile(ctx *context.Context, isNewFile bool) { } blob := entry.Blob() - if blob.Size() >= setting.UI.MaxDisplayFileSize { - ctx.NotFound(err) - return - } - dataRc, err := blob.DataAsync() if err != nil { ctx.NotFound(err) @@ -159,6 +154,7 @@ func editFile(ctx *context.Context, isNewFile bool) { defer dataRc.Close() + ctx.Data["IsFileTooLarge"] = blob.Size() >= setting.UI.MaxDisplayFileSize ctx.Data["FileSize"] = blob.Size() buf := make([]byte, 1024) @@ -166,10 +162,7 @@ func editFile(ctx *context.Context, isNewFile bool) { buf = buf[:n] // Only some file types are editable online as text. - if !typesniffer.DetectContentType(buf).IsRepresentableAsText() { - ctx.NotFound(nil) - return - } + ctx.Data["IsFileText"] = typesniffer.DetectContentType(buf).IsRepresentableAsText() d, _ := io.ReadAll(dataRc) diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 3df6051975..009aa6b505 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -143,8 +143,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { // Assume file is not editable first. if fInfo.isLFSFile { ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files") - } else if !isRepresentableAsText { - ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files") } // read all needed attributes which will be used later @@ -243,21 +241,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["FileContent"] = fileContent ctx.Data["LineEscapeStatus"] = statuses } - if !fInfo.isLFSFile { - if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) { - if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID { - ctx.Data["CanEditFile"] = false - ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked") - } else { - ctx.Data["CanEditFile"] = true - ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file") - } - } else if !ctx.Repo.RefFullName.IsBranch() { - ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch") - } else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) { - ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit") - } - } case fInfo.st.IsPDF(): ctx.Data["IsPDFFile"] = true @@ -307,6 +290,22 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { } } + if !fInfo.isLFSFile { + if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) { + if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID { + ctx.Data["CanEditFile"] = false + ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked") + } else { + ctx.Data["CanEditFile"] = true + ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file") + } + } else if !ctx.Repo.RefFullName.IsBranch() { + ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch") + } else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) { + ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit") + } + } + if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) { if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID { ctx.Data["CanDeleteFile"] = false diff --git a/templates/repo/editor/edit.tmpl b/templates/repo/editor/edit.tmpl index ae8a60c20c..9a59fdac70 100644 --- a/templates/repo/editor/edit.tmpl +++ b/templates/repo/editor/edit.tmpl @@ -28,31 +28,47 @@ -
-
-