Skip to content
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

Render diff stats server-side #13579

Merged
merged 1 commit into from
Nov 15, 2020
Merged
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
3 changes: 3 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func NewFuncMap() []template.FuncMap {
}
return path
},
"DiffStatsWidth": func(adds int, dels int) string {
return fmt.Sprintf("%f", float64(adds)/(float64(adds)+float64(dels))*100)
},
"Json": func(in interface{}) string {
out, err := json.Marshal(in)
if err != nil {
Expand Down
21 changes: 3 additions & 18 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@
<li>
<div class="diff-counter count pull-right">
{{if not .IsBin}}
<span class="add" data-line="{{.Addition}}">+{{.Addition}}</span>
<span class="bar">
<div class="pull-left add"></div>
<div class="pull-left del"></div>
</span>
<span class="del" data-line="{{.Deletion}}">-{{.Deletion}}</span>
{{template "repo/diff/stats" .}}
{{else}}
<span>{{$.i18n.Tr "repo.diff.bin"}}</span>
{{end}}
Expand All @@ -60,12 +55,7 @@
<h4 class="ui top attached normal header rounded">
<div class="diff-counter count ui left">
{{if not $file.IsRenamed}}
<span class="add" data-line="{{.Addition}}">+ {{.Addition}}</span>
<span class="bar">
<div class="pull-left add"></div>
<div class="pull-left del"></div>
</span>
<span class="del" data-line="{{.Deletion}}">- {{.Deletion}}</span>
{{template "repo/diff/stats" .}}
{{end}}
</div>
<span class="file">{{$file.Name}}</span>
Expand Down Expand Up @@ -101,12 +91,7 @@
{{if $file.IsBin}}
{{$.i18n.Tr "repo.diff.bin"}}
{{else if not $file.IsRenamed}}
<span class="add" data-line="{{.Addition}}">+ {{.Addition}}</span>
<span class="bar">
<div class="pull-left add"></div>
<div class="pull-left del"></div>
</span>
<span class="del" data-line="{{.Deletion}}">- {{.Deletion}}</span>
{{template "repo/diff/stats" .}}
{{end}}
</div>
<span class="file">{{if $file.IsRenamed}}{{$file.OldName}} &rarr; {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}}</span>
Expand Down
6 changes: 6 additions & 0 deletions templates/repo/diff/stats.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<span class="add" data-line="{{.Addition}}">+ {{.Addition}}</span>
<span class="bar">
<div class="pull-left add" style="width: {{DiffStatsWidth .Addition .Deletion}}%"></div>
<div class="pull-left del"></div>
</span>
<span class="del" data-line="{{.Deletion}}">- {{.Deletion}}</span>
11 changes: 0 additions & 11 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,17 +1134,6 @@ async function initRepository() {
initReactionSelector();
}

// Diff
if ($('.repository.diff').length > 0) {
$('.diff-counter').each(function () {
const $item = $(this);
const addLine = $item.find('span[data-line].add').data('line');
const delLine = $item.find('span[data-line].del').data('line');
const addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
$item.find('.bar .add').css('width', `${addPercent}%`);
});
}

// Quick start and repository home
$('#repo-clone-ssh').on('click', function () {
$('.clone-url').text($(this).data('link'));
Expand Down