Skip to content

Commit 246f0af

Browse files
committed
Improve display of empty files
1 parent b658f2c commit 246f0af

File tree

7 files changed

+77
-54
lines changed

7 files changed

+77
-54
lines changed

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ remove = Remove
9090
remove_all = Remove All
9191
remove_label_str = Remove item "%s"
9292
edit = Edit
93+
empty_file = Empty File
9394

9495
enabled = Enabled
9596
disabled = Disabled

routers/web/repo/blame.go

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func RefBlame(ctx *context.Context) {
9494
ctx.Data["FileName"] = blob.Name()
9595

9696
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
97-
ctx.Data["NumLinesSet"] = true
9897

9998
if err != nil {
10099
ctx.NotFound("GetBlobLineCount", err)

routers/web/repo/view.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,16 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
462462
ctx.Data["HasSourceRenderedToggle"] = true
463463
}
464464

465+
buf, _ := io.ReadAll(rd)
466+
467+
// empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
468+
// the NumLines is only used for the display on the UI: "xxx lines"
469+
if len(buf) == 0 {
470+
ctx.Data["NumLines"] = 0
471+
} else {
472+
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
473+
}
474+
465475
if markupType != "" && !shouldRenderSource {
466476
ctx.Data["IsMarkup"] = true
467477
ctx.Data["MarkupType"] = markupType
@@ -485,17 +495,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
485495
// to prevent iframe load third-party url
486496
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
487497
} else {
488-
buf, _ := io.ReadAll(rd)
489-
490-
// empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
491-
// the NumLines is only used for the display on the UI: "xxx lines"
492-
if len(buf) == 0 {
493-
ctx.Data["NumLines"] = 0
494-
} else {
495-
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
496-
}
497-
ctx.Data["NumLinesSet"] = true
498-
499498
language := ""
500499

501500
indexFilename, worktree, deleteTemporaryFile, err := ctx.Repo.GitRepo.ReadTreeToTemporaryIndex(ctx.Repo.CommitID)

templates/repo/blame.tmpl

+40-36
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,50 @@
2020
<div class="file-view code-view unicode-escaped">
2121
<table>
2222
<tbody>
23-
{{range $row := .BlameRows}}
24-
<tr class="{{if and (gt $.CommitCnt 1) ($row.CommitMessage)}}top-line-blame{{end}}">
25-
<td class="lines-commit">
26-
<div class="blame-info">
27-
<div class="blame-data">
28-
<div class="blame-avatar">
29-
{{$row.Avatar}}
30-
</div>
31-
<div class="blame-message">
32-
<a href="{{$row.CommitURL}}" title="{{$row.CommitMessage}}">
33-
{{$row.CommitMessage}}
34-
</a>
35-
</div>
36-
<div class="blame-time">
37-
{{$row.CommitSince}}
23+
{{if .BlameRows}}
24+
{{range $row := .BlameRows}}
25+
<tr class="{{if and (gt $.CommitCnt 1) ($row.CommitMessage)}}top-line-blame{{end}}">
26+
<td class="lines-commit">
27+
<div class="blame-info">
28+
<div class="blame-data">
29+
<div class="blame-avatar">
30+
{{$row.Avatar}}
31+
</div>
32+
<div class="blame-message">
33+
<a href="{{$row.CommitURL}}" title="{{$row.CommitMessage}}">
34+
{{$row.CommitMessage}}
35+
</a>
36+
</div>
37+
<div class="blame-time">
38+
{{$row.CommitSince}}
39+
</div>
3840
</div>
3941
</div>
40-
</div>
41-
</td>
42-
<td class="lines-blame-btn">
43-
{{if $row.PreviousSha}}
44-
<a href="{{$row.PreviousShaURL}}" data-tooltip-content='{{$.locale.Tr "repo.blame_prior"}}'>
45-
{{svg "octicon-versions"}}
46-
</a>
47-
{{end}}
48-
</td>
49-
<td class="lines-num">
50-
<span id="L{{$row.RowNumber}}" data-line-number="{{$row.RowNumber}}"></span>
51-
</td>
52-
{{if $.EscapeStatus.Escaped}}
53-
<td class="lines-escape">
54-
{{if $row.EscapeStatus.Escaped}}
55-
<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $row "locale" $.locale}}"></button>
42+
</td>
43+
<td class="lines-blame-btn">
44+
{{if $row.PreviousSha}}
45+
<a href="{{$row.PreviousShaURL}}" data-tooltip-content='{{$.locale.Tr "repo.blame_prior"}}'>
46+
{{svg "octicon-versions"}}
47+
</a>
5648
{{end}}
5749
</td>
58-
{{end}}
59-
<td rel="L{{$row.RowNumber}}" class="lines-code blame-code chroma">
60-
<code class="code-inner gt-pl-3">{{$row.Code}}</code>
61-
</td>
62-
</tr>
50+
<td class="lines-num">
51+
<span id="L{{$row.RowNumber}}" data-line-number="{{$row.RowNumber}}"></span>
52+
</td>
53+
{{if $.EscapeStatus.Escaped}}
54+
<td class="lines-escape">
55+
{{if $row.EscapeStatus.Escaped}}
56+
<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $row "locale" $.locale}}"></button>
57+
{{end}}
58+
</td>
59+
{{end}}
60+
<td rel="L{{$row.RowNumber}}" class="lines-code blame-code chroma">
61+
<code class="code-inner gt-pl-3">{{$row.Code}}</code>
62+
</td>
63+
</tr>
64+
{{end}}
65+
{{else}}
66+
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
6367
{{end}}
6468
</tbody>
6569
</table>

templates/repo/file_info.tmpl

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

templates/repo/settings/lfs_file.tmpl

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
1616
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextFile}} code-view{{end}}">
1717
{{if .IsMarkup}}
18-
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
18+
{{if .FileContent}}
19+
{{.FileContent | Safe}}
20+
{{else}}
21+
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
22+
{{end}}
1923
{{else if .IsPlainText}}
20-
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
24+
{{if .FileContent}}
25+
<pre>{{.FileContent | Safe}}</pre>
26+
{{else}}
27+
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
28+
{{end}}
2129
{{else if not .IsTextFile}}
2230
<div class="view-raw">
2331
{{if .IsImageFile}}
@@ -49,6 +57,8 @@
4957
</tr>
5058
</tbody>
5159
</table>
60+
{{else if not .FileSize}}
61+
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
5262
{{end}}
5363
</div>
5464
</div>

templates/repo/view_file.tmpl

+12-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,17 @@
6969
{{end}}
7070
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextSource}} code-view{{end}}">
7171
{{if .IsMarkup}}
72-
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
72+
{{if .FileContent}}
73+
{{.FileContent | Safe}}
74+
{{else}}
75+
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
76+
{{end}}
7377
{{else if .IsPlainText}}
74-
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
78+
{{if .FileContent}}
79+
<pre>{{.FileContent | Safe}}</pre>
80+
{{else}}
81+
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
82+
{{end}}
7583
{{else if not .IsTextSource}}
7684
<div class="view-raw">
7785
{{if .IsImageFile}}
@@ -122,6 +130,8 @@
122130
<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>
123131
</div>
124132
{{end}}
133+
{{else if not .FileSize}}
134+
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
125135
{{end}}
126136
</div>
127137
</div>

0 commit comments

Comments
 (0)