Simplify split diff view generation and remove JS dependency (#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 <art27@cantab.net>
This commit is contained in:
zeripath 2021-08-29 15:28:04 +01:00 committed by GitHub
parent d24eb6e6ce
commit f5b0e2c9d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 75 deletions

View file

@ -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{}