From 1e92cbba8f4aef0474479b5d59cbd89dd9f03132 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 23 Aug 2021 01:11:25 +0100 Subject: [PATCH 1/3] Simpliy split diff view generation and remove JS dependency Gitea has relied on some slow JS code to match up added and deleted lines on the diff pages. This can cause a considerable slow down on large diff pages. This PR makes a small change meaning that the matching up can occur much more simply. Signed-off-by: Andrew Thornton --- services/gitdiff/gitdiff.go | 18 +++- templates/repo/diff/box.tmpl | 28 ------ templates/repo/diff/section_split.tmpl | 122 ++++++++++++++++--------- 3 files changed, 93 insertions(+), 75 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index d50e41eb40279..59da680d48f22 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -75,6 +75,7 @@ const ( type DiffLine struct { LeftIdx int RightIdx int + Match int Type DiffLineType Content string Comments []*models.Comment @@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFileLFSPrefix bool ) + lastLeftIdx := -1 leftLine, rightLine := 1, 1 for { @@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFile.IsIncomplete = true continue } - diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine} + diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1} rightLine++ if curSection == nil { // Create a new section to represent this hunk curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) } + if lastLeftIdx > -1 { + diffLine.Match = lastLeftIdx + curSection.Lines[lastLeftIdx].Match = len(curSection.Lines) + lastLeftIdx++ + if lastLeftIdx >= len(curSection.Lines) || curSection.Lines[lastLeftIdx].Type != DiffLineDel { + lastLeftIdx = -1 + } + } curSection.Lines = append(curSection.Lines, diffLine) case '-': curFileLinesCount++ @@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFile.IsIncomplete = true continue } - diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine} + diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1} if leftLine > 0 { leftLine++ } @@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) } + if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel { + lastLeftIdx = len(curSection.Lines) + } curSection.Lines = append(curSection.Lines, diffLine) case ' ': curFileLinesCount++ @@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} leftLine++ rightLine++ + lastLeftIdx = -1 if curSection == nil { // Create a new section to represent this hunk curSection = &DiffSection{} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 1ca2dcc4d8144..4f8f7260976aa 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -154,33 +154,5 @@ {{end}} {{template "repo/issue/view_content/reference_issue_dialog" .}} - - {{if .IsSplitStyle}} - - {{end}} {{end}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 2f959ac2da7e8..1b900954b4b3a 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -1,52 +1,84 @@ {{$file := .file}} {{range $j, $section := $file.Sections}} {{range $k, $line := $section.Lines}} - - {{if eq .GetType 4}} - - {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }} - - {{svg "octicon-fold-down"}} - - {{end}} - {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4) }} - - {{svg "octicon-fold-up"}} - - {{end}} - {{if eq $line.GetExpandDirection 2}} - - {{svg "octicon-fold"}} - - {{end}} - - {{$section.GetComputedInlineDiffFor $line}} - {{else}} - - {{if $line.LeftIdx}}{{end}} - {{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}{{svg "octicon-plus"}}{{end}}{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} - - {{if $line.RightIdx}}{{end}} - {{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{svg "octicon-plus"}}{{end}}{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} - {{end}} - - {{if gt (len $line.Comments) 0}} - - - - - {{if eq $line.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} - {{end}} - - - - - {{if eq $line.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} - {{end}} - + {{$hasmatch := ne $line.Match -1}} + {{if or (ne .GetType 2) (not $hasmatch)}} + + {{if eq .GetType 4}} + + {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }} + + {{svg "octicon-fold-down"}} + + {{end}} + {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4) }} + + {{svg "octicon-fold-up"}} + + {{end}} + {{if eq $line.GetExpandDirection 2}} + + {{svg "octicon-fold"}} + + {{end}} + + {{$section.GetComputedInlineDiffFor $line}} + {{else if and (eq .GetType 3) $hasmatch}}{{/* DEL */}} + {{$match := index $section.Lines $line.Match}} + + + {{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{svg "octicon-plus"}}{{end}}{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} + + {{if $match.RightIdx}}{{end}} + {{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{svg "octicon-plus"}}{{end}}{{if $match.RightIdx}}{{$section.GetComputedInlineDiffFor $match}}{{end}} + {{else}} + + {{if $line.LeftIdx}}{{end}} + {{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}{{svg "octicon-plus"}}{{end}}{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} + + {{if $line.RightIdx}}{{end}} + {{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{svg "octicon-plus"}}{{end}}{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} + {{end}} + {{if gt (len $line.Comments) 0}} + + + + + {{if eq $line.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + + + + + {{if eq $line.GetCommentSide "proposed"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + + + {{end}} + {{if and (eq .GetType 3) $hasmatch}} + {{$match := index $section.Lines $line.Match}} + {{if gt (len $match.Comments) 0}} + + + + + {{if eq $match.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{end}} + + + + + {{if eq $match.GetCommentSide "proposed"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{end}} + + + {{end}} + {{end}} {{end}} {{end}} {{end}} From 4cd73861625381b424b506e4a963dc848479bd31 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 23 Aug 2021 20:35:29 +0100 Subject: [PATCH 2/3] fix test Signed-off-by: Andrew Thornton --- modules/repofiles/diff_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index d40f8a50fe089..da50284150a28 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -83,6 +83,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 3, RightIdx: 0, + Match: 4, Type: 3, Content: "-Description for repo1", Comments: nil, @@ -90,6 +91,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 0, RightIdx: 3, + Match: 3, Type: 2, Content: "+Description for repo1", Comments: nil, @@ -97,6 +99,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 0, RightIdx: 4, + Match: -1, Type: 2, Content: "+this is a new line", Comments: nil, From 4445909446a9a22e1eaf0368c91f091f1791c6e1 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 24 Aug 2021 11:00:14 +0100 Subject: [PATCH 3/3] fix comment rendering Signed-off-by: Andrew Thornton --- templates/repo/diff/section_split.tmpl | 61 ++++++++++++++++---------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 1b900954b4b3a..aed6d784b307c 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -40,44 +40,57 @@ {{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{svg "octicon-plus"}}{{end}}{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}} {{end}} - {{if gt (len $line.Comments) 0}} - - - - - {{if eq $line.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} - {{end}} - - - - - {{if eq $line.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} - {{end}} - - - {{end}} {{if and (eq .GetType 3) $hasmatch}} {{$match := index $section.Lines $line.Match}} - {{if gt (len $match.Comments) 0}} - + {{if or (gt (len $line.Comments) 0) (gt (len $match.Comments) 0)}} + - {{if eq $match.GetCommentSide "previous"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{if gt (len $line.Comments) 0}} + {{if eq $line.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + {{end}} + {{if gt (len $match.Comments) 0}} + {{if eq $match.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{end}} {{end}} - {{if eq $match.GetCommentSide "proposed"}} - {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{if eq $line.GetCommentSide "proposed"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + {{if gt (len $match.Comments) 0}} + {{if eq $match.GetCommentSide "proposed"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{end}} {{end}} {{end}} + {{else if gt (len $line.Comments) 0}} + + + + + {{if gt (len $line.Comments) 0}} + {{if eq $line.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + {{end}} + + + + + {{if eq $line.GetCommentSide "proposed"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}} + {{end}} + + {{end}} {{end}} {{end}}