Skip to content

Commit 58f4a41

Browse files
authored
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 <[email protected]>
1 parent b31307c commit 58f4a41

File tree

4 files changed

+109
-75
lines changed

4 files changed

+109
-75
lines changed

modules/repofiles/diff_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,23 @@ func TestGetDiffPreview(t *testing.T) {
8383
{
8484
LeftIdx: 3,
8585
RightIdx: 0,
86+
Match: 4,
8687
Type: 3,
8788
Content: "-Description for repo1",
8889
Comments: nil,
8990
},
9091
{
9192
LeftIdx: 0,
9293
RightIdx: 3,
94+
Match: 3,
9395
Type: 2,
9496
Content: "+Description for repo1",
9597
Comments: nil,
9698
},
9799
{
98100
LeftIdx: 0,
99101
RightIdx: 4,
102+
Match: -1,
100103
Type: 2,
101104
Content: "+this is a new line",
102105
Comments: nil,

services/gitdiff/gitdiff.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const (
7575
type DiffLine struct {
7676
LeftIdx int
7777
RightIdx int
78+
Match int
7879
Type DiffLineType
7980
Content string
8081
Comments []*models.Comment
@@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
943944
curFileLFSPrefix bool
944945
)
945946

947+
lastLeftIdx := -1
946948
leftLine, rightLine := 1, 1
947949

948950
for {
@@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10271029
curFile.IsIncomplete = true
10281030
continue
10291031
}
1030-
diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine}
1032+
diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1}
10311033
rightLine++
10321034
if curSection == nil {
10331035
// Create a new section to represent this hunk
10341036
curSection = &DiffSection{}
10351037
curFile.Sections = append(curFile.Sections, curSection)
10361038
}
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+
}
10371047
curSection.Lines = append(curSection.Lines, diffLine)
10381048
case '-':
10391049
curFileLinesCount++
@@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10421052
curFile.IsIncomplete = true
10431053
continue
10441054
}
1045-
diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine}
1055+
diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1}
10461056
if leftLine > 0 {
10471057
leftLine++
10481058
}
@@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10511061
curSection = &DiffSection{}
10521062
curFile.Sections = append(curFile.Sections, curSection)
10531063
}
1064+
if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel {
1065+
lastLeftIdx = len(curSection.Lines)
1066+
}
10541067
curSection.Lines = append(curSection.Lines, diffLine)
10551068
case ' ':
10561069
curFileLinesCount++
@@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
10611074
diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine}
10621075
leftLine++
10631076
rightLine++
1077+
lastLeftIdx = -1
10641078
if curSection == nil {
10651079
// Create a new section to represent this hunk
10661080
curSection = &DiffSection{}

templates/repo/diff/box.tmpl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,5 @@
154154
{{end}}
155155

156156
{{template "repo/issue/view_content/reference_issue_dialog" .}}
157-
158-
{{if .IsSplitStyle}}
159-
<script>
160-
document.addEventListener('DOMContentLoaded', () => {
161-
$('tr.add-code').each(function() {
162-
let prev = $(this).prev();
163-
if (prev.is('.del-code') && prev.children().eq(5).text().trim() === '') {
164-
while (prev.prev().is('.del-code') && prev.prev().children().eq(5).text().trim() === '') {
165-
prev = prev.prev();
166-
}
167-
prev.children().eq(3).attr('data-line-num', $(this).children().eq(3).attr('data-line-num'));
168-
prev.children().eq(3).html($(this).children().eq(3).html());
169-
prev.children().eq(4).html($(this).children().eq(4).html());
170-
prev.children().eq(5).html($(this).children().eq(5).html());
171-
172-
prev.children().eq(0).addClass('del-code');
173-
prev.children().eq(1).addClass('del-code');
174-
prev.children().eq(2).addClass('del-code');
175-
prev.children().eq(3).addClass('add-code');
176-
prev.children().eq(4).addClass('add-code');
177-
prev.children().eq(5).addClass('add-code');
178-
179-
$(this).remove();
180-
}
181-
});
182-
});
183-
</script>
184-
{{end}}
185157
</div>
186158
{{end}}
Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,97 @@
11
{{$file := .file}}
22
{{range $j, $section := $file.Sections}}
33
{{range $k, $line := $section.Lines}}
4-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
5-
{{if eq .GetType 4}}
6-
<td class="lines-num lines-num-old">
7-
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }}
8-
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=down" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
9-
{{svg "octicon-fold-down"}}
10-
</a>
11-
{{end}}
12-
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4) }}
13-
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=up" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
14-
{{svg "octicon-fold-up"}}
15-
</a>
16-
{{end}}
17-
{{if eq $line.GetExpandDirection 2}}
18-
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
19-
{{svg "octicon-fold"}}
20-
</a>
21-
{{end}}
22-
</td>
23-
<td colspan="5" class="lines-code lines-code-old "><code class="code-inner">{{$section.GetComputedInlineDiffFor $line}}</span></td>
24-
{{else}}
25-
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}"></span></td>
26-
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
27-
<td class="lines-code lines-code-old halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui primary button add-code-comment add-code-comment-left{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
28-
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}"></span></td>
29-
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
30-
<td class="lines-code lines-code-new halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
31-
{{end}}
32-
</tr>
33-
{{if gt (len $line.Comments) 0}}
34-
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
35-
<td class="lines-num"></td>
36-
<td class="lines-type-marker"></td>
37-
<td class="add-comment-left">
38-
{{if eq $line.GetCommentSide "previous"}}
39-
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
40-
{{end}}
41-
</td>
42-
<td class="lines-num"></td>
43-
<td class="lines-type-marker"></td>
44-
<td class="add-comment-right">
45-
{{if eq $line.GetCommentSide "proposed"}}
46-
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
47-
{{end}}
48-
</td>
4+
{{$hasmatch := ne $line.Match -1}}
5+
{{if or (ne .GetType 2) (not $hasmatch)}}
6+
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
7+
{{if eq .GetType 4}}
8+
<td class="lines-num lines-num-old">
9+
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }}
10+
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=down" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
11+
{{svg "octicon-fold-down"}}
12+
</a>
13+
{{end}}
14+
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4) }}
15+
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=up" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
16+
{{svg "octicon-fold-up"}}
17+
</a>
18+
{{end}}
19+
{{if eq $line.GetExpandDirection 2}}
20+
<a role="button" class="blob-excerpt" data-url="{{$.root.RepoLink}}/blob_excerpt/{{$.root.AfterCommitID}}" data-query="{{$line.GetBlobExcerptQuery}}&style=split&direction=" data-anchor="diff-{{Sha1 $file.Name}}K{{$line.SectionInfo.RightIdx}}">
21+
{{svg "octicon-fold"}}
22+
</a>
23+
{{end}}
24+
</td>
25+
<td colspan="5" class="lines-code lines-code-old "><code class="code-inner">{{$section.GetComputedInlineDiffFor $line}}</span></td>
26+
{{else if and (eq .GetType 3) $hasmatch}}{{/* DEL */}}
27+
{{$match := index $section.Lines $line.Match}}
28+
<td class="lines-num lines-num-old del-code" data-line-num="{{$line.LeftIdx}}"><span rel="diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}"></span></td>
29+
<td class="lines-type-marker lines-type-marker-old del-code"><span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span></td>
30+
<td class="lines-code lines-code-old halfwidth del-code">{{if and $.root.SignedUserID $.root.PageIsPullFiles}}<a class="ui primary button add-code-comment add-code-comment-left{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
31+
<td class="lines-num lines-num-new add-code" data-line-num="{{if $match.RightIdx}}{{$match.RightIdx}}{{end}}"><span rel="{{if $match.RightIdx}}diff-{{Sha1 $file.Name}}R{{$match.RightIdx}}{{end}}"></span></td>
32+
<td class="lines-type-marker lines-type-marker-new add-code">{{if $match.RightIdx}}<span class="mono" data-type-marker="{{$match.GetLineTypeMarker}}"></span>{{end}}</td>
33+
<td class="lines-code lines-code-new halfwidth add-code">{{if and $.root.SignedUserID $.root.PageIsPullFiles}}<a class="ui primary button add-code-comment add-code-comment-right{{if (not $match.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="right" data-idx="{{$match.RightIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $match.RightIdx}}{{$section.GetComputedInlineDiffFor $match}}{{end}}</code></td>
34+
{{else}}
35+
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}"></span></td>
36+
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
37+
<td class="lines-code lines-code-old halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui primary button add-code-comment add-code-comment-left{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
38+
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}"></span></td>
39+
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
40+
<td class="lines-code lines-code-new halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
41+
{{end}}
4942
</tr>
43+
{{if and (eq .GetType 3) $hasmatch}}
44+
{{$match := index $section.Lines $line.Match}}
45+
{{if or (gt (len $line.Comments) 0) (gt (len $match.Comments) 0)}}
46+
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
47+
<td class="lines-num"></td>
48+
<td class="lines-type-marker"></td>
49+
<td class="add-comment-left">
50+
{{if gt (len $line.Comments) 0}}
51+
{{if eq $line.GetCommentSide "previous"}}
52+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
53+
{{end}}
54+
{{end}}
55+
{{if gt (len $match.Comments) 0}}
56+
{{if eq $match.GetCommentSide "previous"}}
57+
{{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}}
58+
{{end}}
59+
{{end}}
60+
</td>
61+
<td class="lines-num"></td>
62+
<td class="lines-type-marker"></td>
63+
<td class="add-comment-right">
64+
{{if eq $line.GetCommentSide "proposed"}}
65+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
66+
{{end}}
67+
{{if gt (len $match.Comments) 0}}
68+
{{if eq $match.GetCommentSide "proposed"}}
69+
{{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}}
70+
{{end}}
71+
{{end}}
72+
</td>
73+
</tr>
74+
{{end}}
75+
{{else if gt (len $line.Comments) 0}}
76+
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
77+
<td class="lines-num"></td>
78+
<td class="lines-type-marker"></td>
79+
<td class="add-comment-left">
80+
{{if gt (len $line.Comments) 0}}
81+
{{if eq $line.GetCommentSide "previous"}}
82+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
83+
{{end}}
84+
{{end}}
85+
</td>
86+
<td class="lines-num"></td>
87+
<td class="lines-type-marker"></td>
88+
<td class="add-comment-right">
89+
{{if eq $line.GetCommentSide "proposed"}}
90+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
91+
{{end}}
92+
</td>
93+
</tr>
94+
{{end}}
5095
{{end}}
5196
{{end}}
5297
{{end}}

0 commit comments

Comments
 (0)