Skip to content

Commit 47ca61d

Browse files
Improve detecting empty files (#31332)
Co-authored-by: silverwind <me@silverwind.io>
1 parent 7115dce commit 47ca61d

File tree

11 files changed

+29
-13
lines changed

11 files changed

+29
-13
lines changed

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,7 @@ file_view_rendered = View Rendered
12381238
file_view_raw = View Raw
12391239
file_permalink = Permalink
12401240
file_too_large = The file is too large to be shown.
1241+
file_is_empty = The file is empty.
12411242
code_preview_line_from_to = Lines %[1]d to %[2]d in %[3]s
12421243
code_preview_line_in = Line %[1]d in %[2]s
12431244
invisible_runes_header = `This file contains invisible Unicode characters`

routers/web/repo/blame.go

-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ func RefBlame(ctx *context.Context) {
9999
}
100100

101101
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
102-
ctx.Data["NumLinesSet"] = true
103-
104102
if err != nil {
105103
ctx.NotFound("GetBlobLineCount", err)
106104
return

routers/web/repo/setting/lfs.go

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func LFSFileGet(ctx *context.Context) {
303303
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
304304

305305
// Building code view blocks with line number on server side.
306+
// FIXME: the logic is not right here: it first calls EscapeControlReader then calls HTMLEscapeString: double-escaping
306307
escapedContent := &bytes.Buffer{}
307308
ctx.Data["EscapeStatus"], _ = charset.EscapeControlReader(rd, escapedContent, ctx.Locale)
308309

routers/web/repo/view.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr
286286

287287
ctx.Data["FileIsText"] = fInfo.isTextFile
288288
ctx.Data["FileName"] = path.Join(subfolder, readmeFile.Name())
289+
ctx.Data["FileSize"] = fInfo.fileSize
289290
ctx.Data["IsLFSFile"] = fInfo.isLFSFile
290291

291292
if fInfo.isLFSFile {
@@ -301,7 +302,6 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr
301302
// Pretend that this is a normal text file to display 'This file is too large to be shown'
302303
ctx.Data["IsFileTooLarge"] = true
303304
ctx.Data["IsTextFile"] = true
304-
ctx.Data["FileSize"] = fInfo.fileSize
305305
return
306306
}
307307

@@ -552,7 +552,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
552552
} else {
553553
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
554554
}
555-
ctx.Data["NumLinesSet"] = true
556555

557556
language, err := files_service.TryGetContentLanguage(ctx.Repo.GitRepo, ctx.Repo.CommitID, ctx.Repo.TreePath)
558557
if err != nil {
@@ -606,8 +605,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
606605
break
607606
}
608607

609-
// TODO: this logic seems strange, it duplicates with "isRepresentableAsText=true", it is not the same as "LFSFileGet" in "lfs.go"
610-
// maybe for this case, the file is a binary file, and shouldn't be rendered?
608+
// TODO: this logic duplicates with "isRepresentableAsText=true", it is not the same as "LFSFileGet" in "lfs.go"
609+
// It is used by "external renders", markupRender will execute external programs to get rendered content.
611610
if markupType := markup.Type(blob.Name()); markupType != "" {
612611
rd := io.MultiReader(bytes.NewReader(buf), dataRc)
613612
ctx.Data["IsMarkup"] = true

templates/repo/blame.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<div class="file-view code-view unicode-escaped">
3333
{{if .IsFileTooLarge}}
3434
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
35+
{{else if not .FileSize}}
36+
{{template "shared/fileisempty"}}
3537
{{else}}
3638
<table>
3739
<tbody>

templates/repo/file_info.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
{{ctx.Locale.Tr "repo.symbolic_link"}}
55
</div>
66
{{end}}
7-
{{if .NumLinesSet}}{{/* Explicit attribute needed to show 0 line changes */}}
7+
{{if ne .NumLines nil}}
88
<div class="file-info-entry">
99
{{.NumLines}} {{ctx.Locale.TrN .NumLines "repo.line" "repo.lines"}}
1010
</div>
1111
{{end}}
12-
{{if .FileSize}}
12+
{{if ne .FileSize nil}}
1313
<div class="file-info-entry">
1414
{{FileSize .FileSize}}{{if .IsLFSFile}} ({{ctx.Locale.Tr "repo.stored_lfs"}}){{end}}
1515
</div>

templates/repo/settings/lfs_file.tmpl

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextFile}} code-view{{end}}">
1717
{{if .IsFileTooLarge}}
1818
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
19-
{{else if .IsMarkup}}
20-
{{if .FileContent}}{{.FileContent | SafeHTML}}{{end}}
21-
{{else if .IsPlainText}}
22-
<pre>{{if .FileContent}}{{.FileContent | SafeHTML}}{{end}}</pre>
19+
{{else if not .FileSize}}
20+
{{template "shared/fileisempty"}}
2321
{{else if not .IsTextFile}}
2422
<div class="view-raw">
2523
{{if .IsImageFile}}

templates/repo/view_file.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextSource}} code-view{{end}}">
9292
{{if .IsFileTooLarge}}
9393
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
94+
{{else if not .FileSize}}
95+
{{template "shared/fileisempty"}}
9496
{{else if .IsMarkup}}
9597
{{if .FileContent}}{{.FileContent}}{{end}}
9698
{{else if .IsPlainText}}

templates/shared/fileisempty.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="file-not-rendered-prompt">
2+
{{ctx.Locale.Tr "repo.file_is_empty"}}
3+
</div>

templates/shared/filetoolarge.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="tw-p-4">
1+
<div class="file-not-rendered-prompt">
22
{{ctx.Locale.Tr "repo.file_too_large"}}
33
{{if .RawFileLink}}<a href="{{.RawFileLink}}" rel="nofollow">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>{{end}}
44
</div>

web_src/css/repo.css

+12
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,18 @@ td .commit-summary {
17061706
.file-view.markup {
17071707
padding: 1em 2em;
17081708
}
1709+
1710+
.file-view.markup:has(.file-not-rendered-prompt) {
1711+
padding: 0; /* let the file-not-rendered-prompt layout itself */
1712+
}
1713+
1714+
.file-not-rendered-prompt {
1715+
padding: 1rem;
1716+
text-align: center;
1717+
font-size: 1rem !important; /* use consistent styles for various containers (code, markup, etc) */
1718+
line-height: var(--line-height-default) !important; /* same as above */
1719+
}
1720+
17091721
.repository .activity-header {
17101722
display: flex;
17111723
justify-content: space-between;

0 commit comments

Comments
 (0)