Skip to content

Improve display of empty files #26568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ remove = Remove
remove_all = Remove All
remove_label_str = Remove item "%s"
edit = Edit
empty_file = Empty File

enabled = Enabled
disabled = Disabled
Expand Down
1 change: 0 additions & 1 deletion routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func RefBlame(ctx *context.Context) {
ctx.Data["FileName"] = blob.Name()

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

if err != nil {
ctx.NotFound("GetBlobLineCount", err)
Expand Down
21 changes: 10 additions & 11 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
ctx.Data["HasSourceRenderedToggle"] = true
}

buf, _ := io.ReadAll(rd)

// empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
// the NumLines is only used for the display on the UI: "xxx lines"
if len(buf) == 0 {
ctx.Data["NumLines"] = 0
} else {
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
}

if markupType != "" && !shouldRenderSource {
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
Expand All @@ -486,17 +496,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
// to prevent iframe load third-party url
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
} else {
buf, _ := io.ReadAll(rd)

// empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
// the NumLines is only used for the display on the UI: "xxx lines"
if len(buf) == 0 {
ctx.Data["NumLines"] = 0
} else {
ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
}
ctx.Data["NumLinesSet"] = true

language := ""

indexFilename, worktree, deleteTemporaryFile, err := ctx.Repo.GitRepo.ReadTreeToTemporaryIndex(ctx.Repo.CommitID)
Expand Down
76 changes: 40 additions & 36 deletions templates/repo/blame.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,50 @@
<div class="file-view code-view unicode-escaped">
<table>
<tbody>
{{range $row := .BlameRows}}
<tr class="{{if and (gt $.CommitCnt 1) ($row.CommitMessage)}}top-line-blame{{end}}">
<td class="lines-commit">
<div class="blame-info">
<div class="blame-data">
<div class="blame-avatar">
{{$row.Avatar}}
</div>
<div class="blame-message">
<a href="{{$row.CommitURL}}" title="{{$row.CommitMessage}}">
{{$row.CommitMessage}}
</a>
</div>
<div class="blame-time">
{{$row.CommitSince}}
{{if .BlameRows}}
{{range $row := .BlameRows}}
<tr class="{{if and (gt $.CommitCnt 1) ($row.CommitMessage)}}top-line-blame{{end}}">
<td class="lines-commit">
<div class="blame-info">
<div class="blame-data">
<div class="blame-avatar">
{{$row.Avatar}}
</div>
<div class="blame-message">
<a href="{{$row.CommitURL}}" title="{{$row.CommitMessage}}">
{{$row.CommitMessage}}
</a>
</div>
<div class="blame-time">
{{$row.CommitSince}}
</div>
</div>
</div>
</div>
</td>
<td class="lines-blame-btn">
{{if $row.PreviousSha}}
<a href="{{$row.PreviousShaURL}}" data-tooltip-content='{{$.locale.Tr "repo.blame_prior"}}'>
{{svg "octicon-versions"}}
</a>
{{end}}
</td>
<td class="lines-num">
<span id="L{{$row.RowNumber}}" data-line-number="{{$row.RowNumber}}"></span>
</td>
{{if $.EscapeStatus.Escaped}}
<td class="lines-escape">
{{if $row.EscapeStatus.Escaped}}
<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $row "locale" $.locale}}"></button>
</td>
<td class="lines-blame-btn">
{{if $row.PreviousSha}}
<a href="{{$row.PreviousShaURL}}" data-tooltip-content='{{$.locale.Tr "repo.blame_prior"}}'>
{{svg "octicon-versions"}}
</a>
{{end}}
</td>
{{end}}
<td rel="L{{$row.RowNumber}}" class="lines-code blame-code chroma">
<code class="code-inner gt-pl-3">{{$row.Code}}</code>
</td>
</tr>
<td class="lines-num">
<span id="L{{$row.RowNumber}}" data-line-number="{{$row.RowNumber}}"></span>
</td>
{{if $.EscapeStatus.Escaped}}
<td class="lines-escape">
{{if $row.EscapeStatus.Escaped}}
<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $row "locale" $.locale}}"></button>
{{end}}
</td>
{{end}}
<td rel="L{{$row.RowNumber}}" class="lines-code blame-code chroma">
<code class="code-inner gt-pl-3">{{$row.Code}}</code>
</td>
</tr>
{{end}}
{{else}}
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
{{end}}
</tbody>
</table>
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/file_info.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
{{.locale.Tr "repo.symbolic_link"}}
</div>
{{end}}
{{if .NumLinesSet}}{{/* Explicit attribute needed to show 0 line changes */}}
{{if not (eq .NumLines nil)}}
<div class="file-info-entry">
{{.NumLines}} {{.locale.TrN .NumLines "repo.line" "repo.lines"}}
</div>
{{end}}
{{if .FileSize}}
{{if not (eq .FileSize nil)}}
<div class="file-info-entry">
{{FileSize .FileSize}}{{if .IsLFSFile}} ({{.locale.Tr "repo.stored_lfs"}}){{end}}
</div>
Expand Down
14 changes: 12 additions & 2 deletions templates/repo/settings/lfs_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextFile}} code-view{{end}}">
{{if .IsMarkup}}
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
{{if .FileContent}}
{{.FileContent | Safe}}
{{else}}
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
{{end}}
{{else if .IsPlainText}}
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
{{if .FileContent}}
<pre>{{.FileContent | Safe}}</pre>
{{else}}
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
{{end}}
{{else if not .IsTextFile}}
<div class="view-raw">
{{if .IsImageFile}}
Expand Down Expand Up @@ -49,6 +57,8 @@
</tr>
</tbody>
</table>
{{else if not .FileSize}}
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
{{end}}
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@
{{end}}
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextSource}} code-view{{end}}">
{{if .IsMarkup}}
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
{{if .FileContent}}
{{.FileContent | Safe}}
{{else}}
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
{{end}}
{{else if .IsPlainText}}
<pre>{{if .FileContent}}{{.FileContent | Safe}}{{end}}</pre>
{{if .FileContent}}
<pre>{{.FileContent | Safe}}</pre>
{{else}}
<div class="gt-df gt-jc">{{.locale.Tr "empty_file"}}</div>
{{end}}
{{else if not .IsTextSource}}
<div class="view-raw">
{{if .IsImageFile}}
Expand Down Expand Up @@ -122,6 +130,8 @@
<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>
</div>
{{end}}
{{else if not .FileSize}}
<div class="gt-df gt-jc gt-py-4">{{.locale.Tr "empty_file"}}</div>
{{end}}
</div>
</div>
Expand Down