Skip to content

Commit c6598c2

Browse files
committed
Enable path editing of non text files
1 parent cbb2e52 commit c6598c2

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

options/locale/locale_en-US.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,6 @@ editor.upload_file = Upload File
13311331
editor.edit_file = Edit File
13321332
editor.preview_changes = Preview Changes
13331333
editor.cannot_edit_lfs_files = LFS files cannot be edited in the web interface.
1334-
editor.cannot_edit_non_text_files = Binary files cannot be edited in the web interface.
13351334
editor.edit_this_file = Edit File
13361335
editor.this_file_locked = File is locked
13371336
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
13951394
editor.require_signed_commit = Branch requires a signed commit
13961395
editor.cherry_pick = Cherry-pick %s onto:
13971396
editor.revert = Revert %s onto:
1397+
editor.file_too_large_not_editable = The file is too large to be edited.
1398+
editor.binary_file_not_editable = Binary file content is not editable.
1399+
editor.file_not_editable_hint = But you can still rename or move it.
13981400
13991401
commits.desc = Browse source code change history.
14001402
commits.commits = Commits

routers/web/repo/editor.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ func editFile(ctx *context.Context, isNewFile bool) {
146146
}
147147

148148
blob := entry.Blob()
149-
if blob.Size() >= setting.UI.MaxDisplayFileSize {
150-
ctx.NotFound(err)
151-
return
152-
}
153-
154149
dataRc, err := blob.DataAsync()
155150
if err != nil {
156151
ctx.NotFound(err)
@@ -159,17 +154,15 @@ func editFile(ctx *context.Context, isNewFile bool) {
159154

160155
defer dataRc.Close()
161156

157+
ctx.Data["IsFileTooLarge"] = blob.Size() >= setting.UI.MaxDisplayFileSize
162158
ctx.Data["FileSize"] = blob.Size()
163159

164160
buf := make([]byte, 1024)
165161
n, _ := util.ReadAtMost(dataRc, buf)
166162
buf = buf[:n]
167163

168164
// Only some file types are editable online as text.
169-
if !typesniffer.DetectContentType(buf).IsRepresentableAsText() {
170-
ctx.NotFound(nil)
171-
return
172-
}
165+
ctx.Data["IsFileText"] = typesniffer.DetectContentType(buf).IsRepresentableAsText()
173166

174167
d, _ := io.ReadAll(dataRc)
175168

routers/web/repo/view_file.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
143143
// Assume file is not editable first.
144144
if fInfo.isLFSFile {
145145
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
146-
} else if !isRepresentableAsText {
147-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
148146
}
149147

150148
// read all needed attributes which will be used later
@@ -243,21 +241,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
243241
ctx.Data["FileContent"] = fileContent
244242
ctx.Data["LineEscapeStatus"] = statuses
245243
}
246-
if !fInfo.isLFSFile {
247-
if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
248-
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
249-
ctx.Data["CanEditFile"] = false
250-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
251-
} else {
252-
ctx.Data["CanEditFile"] = true
253-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
254-
}
255-
} else if !ctx.Repo.RefFullName.IsBranch() {
256-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
257-
} else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
258-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
259-
}
260-
}
261244

262245
case fInfo.st.IsPDF():
263246
ctx.Data["IsPDFFile"] = true
@@ -307,6 +290,22 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
307290
}
308291
}
309292

293+
if !fInfo.isLFSFile {
294+
if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
295+
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
296+
ctx.Data["CanEditFile"] = false
297+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
298+
} else {
299+
ctx.Data["CanEditFile"] = true
300+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
301+
}
302+
} else if !ctx.Repo.RefFullName.IsBranch() {
303+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
304+
} else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
305+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
306+
}
307+
}
308+
310309
if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
311310
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
312311
ctx.Data["CanDeleteFile"] = false

templates/repo/editor/edit.tmpl

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,47 @@
2828
<input type="hidden" id="tree_path" name="tree_path" value="{{.TreePath}}" required>
2929
</div>
3030
</div>
31-
<div class="field">
32-
<div class="ui top attached header">
33-
<div class="ui compact small menu small-menu-items repo-editor-menu">
34-
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a>
35-
<a class="item" data-tab="preview" data-preview-url="{{.Repository.Link}}/markup" data-preview-context-ref="{{.RepoLink}}/src/{{.RefTypeNameSubURL}}">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
36-
{{if not .IsNewFile}}
37-
<a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a>
38-
{{end}}
31+
{{if .IsFileText}}
32+
<div class="field">
33+
<div class="ui top attached header">
34+
<div class="ui compact small menu small-menu-items repo-editor-menu">
35+
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a>
36+
<a class="item" data-tab="preview" data-preview-url="{{.Repository.Link}}/markup" data-preview-context-ref="{{.RepoLink}}/src/{{.RefTypeNameSubURL}}">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
37+
{{if not .IsNewFile}}
38+
<a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a>
39+
{{end}}
40+
</div>
3941
</div>
40-
</div>
41-
<div class="ui bottom attached segment tw-p-0">
42-
<div class="ui active tab tw-rounded-b" data-tab="write">
43-
<textarea id="edit_area" name="content" class="tw-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
44-
data-previewable-extensions="{{.PreviewableExtensions}}"
45-
data-line-wrap-extensions="{{.LineWrapExtensions}}">{{.FileContent}}</textarea>
46-
<div class="editor-loading is-loading"></div>
42+
<div class="ui bottom attached segment tw-p-0">
43+
<div class="ui active tab tw-rounded-b" data-tab="write">
44+
<textarea id="edit_area" name="content" class="tw-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
45+
data-previewable-extensions="{{.PreviewableExtensions}}"
46+
data-line-wrap-extensions="{{.LineWrapExtensions}}">{{.FileContent}}</textarea>
47+
<div class="editor-loading is-loading"></div>
48+
</div>
49+
<div class="ui tab tw-px-4 tw-py-3" data-tab="preview">
50+
{{ctx.Locale.Tr "loading"}}
51+
</div>
52+
<div class="ui tab" data-tab="diff">
53+
<div class="tw-p-16"></div>
54+
</div>
4755
</div>
48-
<div class="ui tab tw-px-4 tw-py-3" data-tab="preview">
49-
{{ctx.Locale.Tr "loading"}}
56+
</div>
57+
{{else if .IsFileTooLarge}}
58+
<div class="field">
59+
<div class="ui segment tw-text-center">
60+
<h4 class="tw-font-semibold tw-mb-2">{{ctx.Locale.Tr "repo.editor.file_too_large_not_editable"}}</h4>
61+
<p>{{ctx.Locale.Tr "repo.editor.file_not_editable_hint"}}</p>
5062
</div>
51-
<div class="ui tab" data-tab="diff">
52-
<div class="tw-p-16"></div>
63+
</div>
64+
{{else}}
65+
<div class="field">
66+
<div class="ui segment tw-text-center">
67+
<h4 class="tw-font-semibold tw-mb-2">{{ctx.Locale.Tr "repo.editor.binary_file_not_editable"}}</h4>
68+
<p>{{ctx.Locale.Tr "repo.editor.file_not_editable_hint"}}</p>
5369
</div>
5470
</div>
55-
</div>
71+
{{end}}
5672
{{template "repo/editor/commit_form" .}}
5773
</form>
5874
</div>

0 commit comments

Comments
 (0)