From 58f4a4114eb53cff9da13923ea95edd22a904840 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 30 Aug 2021 16:43:06 +0100 Subject: [PATCH] Simplify split diff view generation and remove JS dependency (#16775) (#16863) Backport #16775 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. Partial fix #1351 Signed-off-by: Andrew Thornton --- modules/repofiles/diff_test.go | 3 + services/gitdiff/gitdiff.go | 18 +++- templates/repo/diff/box.tmpl | 28 ----- templates/repo/diff/section_split.tmpl | 135 ++++++++++++++++--------- 4 files changed, 109 insertions(+), 75 deletions(-) diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index d40f8a50fe08..da50284150a2 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, diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index d50e41eb4027..59da680d48f2 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 1ca2dcc4d814..4f8f7260976a 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 2f959ac2da7e..aed6d784b307 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -1,52 +1,97 @@ {{$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 and (eq .GetType 3) $hasmatch}} + {{$match := index $section.Lines $line.Match}} + {{if or (gt (len $line.Comments) 0) (gt (len $match.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 gt (len $match.Comments) 0}} + {{if eq $match.GetCommentSide "previous"}} + {{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}} + {{end}} + {{end}} + + + + + {{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}} {{end}}