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
Showing the changes to services/gitdiff/gitdiff.go (1 of 4 files). Show all
Unified · Split
| @@ -75,6 +75,7 @@ const ( · 74 unmodified lines | |||
| 75 | type DiffLine struct { | 75 | type DiffLine struct { |
| 76 | LeftIdx int | 76 | LeftIdx int |
| 77 | RightIdx int | 77 | RightIdx int |
| 78 | Match int | ||
| 78 | Type DiffLineType | 79 | Type DiffLineType |
| 79 | Content string | 80 | Content string |
| 80 | Comments []*models.Comment | 81 | Comments []*models.Comment |
| @@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 862 unmodified lines | |||
| 943 | curFileLFSPrefix bool | 944 | curFileLFSPrefix bool |
| 944 | ) | 945 | ) |
| 945 | 946 | ||
| 947 | lastLeftIdx := -1 | ||
| 946 | leftLine, rightLine := 1, 1 | 948 | leftLine, rightLine := 1, 1 |
| 947 | 949 | ||
| 948 | for { | 950 | for { |
| @@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 78 unmodified lines | |||
| 1027 | curFile.IsIncomplete = true | 1029 | curFile.IsIncomplete = true |
| 1028 | continue | 1030 | continue |
| 1029 | } | 1031 | } |
| 1030 | diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine} | 1032 | diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1} |
| 1031 | rightLine++ | 1033 | rightLine++ |
| 1032 | if curSection == nil { | 1034 | if curSection == nil { |
| 1033 | // Create a new section to represent this hunk | 1035 | // Create a new section to represent this hunk |
| 1034 | curSection = &DiffSection{} | 1036 | curSection = &DiffSection{} |
| 1035 | curFile.Sections = append(curFile.Sections, curSection) | 1037 | curFile.Sections = append(curFile.Sections, curSection) |
| 1036 | } | 1038 | } |
| 1039 | if lastLeftIdx > -1 { | ||
| 1040 | diffLine.Match = lastLeftIdx | ||
| 1041 | curSection.Lines[lastLeftIdx].Match = len(curSection.Lines) | ||
| 1042 | lastLeftIdx++ | ||
| 1043 | if lastLeftIdx >= len(curSection.Lines) || curSection.Lines[lastLeftIdx].Type != DiffLineDel { | ||
| 1044 | lastLeftIdx = -1 | ||
| 1045 | } | ||
| 1046 | } | ||
| 1037 | curSection.Lines = append(curSection.Lines, diffLine) | 1047 | curSection.Lines = append(curSection.Lines, diffLine) |
| 1038 | case '-': | 1048 | case '-': |
| 1039 | curFileLinesCount++ | 1049 | curFileLinesCount++ |
| @@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines | |||
| 1042 | curFile.IsIncomplete = true | 1052 | curFile.IsIncomplete = true |
| 1043 | continue | 1053 | continue |
| 1044 | } | 1054 | } |
| 1045 | diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine} | 1055 | diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1} |
| 1046 | if leftLine > 0 { | 1056 | if leftLine > 0 { |
| 1047 | leftLine++ | 1057 | leftLine++ |
| 1048 | } | 1058 | } |
| @@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines | |||
| 1051 | curSection = &DiffSection{} | 1061 | curSection = &DiffSection{} |
| 1052 | curFile.Sections = append(curFile.Sections, curSection) | 1062 | curFile.Sections = append(curFile.Sections, curSection) |
| 1053 | } | 1063 | } |
| 1064 | if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel { | ||
| 1065 | lastLeftIdx = len(curSection.Lines) | ||
| 1066 | } | ||
| 1054 | curSection.Lines = append(curSection.Lines, diffLine) | 1067 | curSection.Lines = append(curSection.Lines, diffLine) |
| 1055 | case ' ': | 1068 | case ' ': |
| 1056 | curFileLinesCount++ | 1069 | curFileLinesCount++ |
| @@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 4 unmodified lines | |||
| 1061 | diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} | 1074 | diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} |
| 1062 | leftLine++ | 1075 | leftLine++ |
| 1063 | rightLine++ | 1076 | rightLine++ |
| 1077 | lastLeftIdx = -1 | ||
| 1064 | if curSection == nil { | 1078 | if curSection == nil { |
| 1065 | // Create a new section to represent this hunk | 1079 | // Create a new section to represent this hunk |
| 1066 | curSection = &DiffSection{} | 1080 | curSection = &DiffSection{} |