Skip to content

Commit 1e92cbb

Browse files
committed
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 <[email protected]>
1 parent db1e3d0 commit 1e92cbb

File tree

3 files changed

+93
-75
lines changed

3 files changed

+93
-75
lines changed

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: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
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 gt (len $line.Comments) 0}}
44+
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
45+
<td class="lines-num"></td>
46+
<td class="lines-type-marker"></td>
47+
<td class="add-comment-left">
48+
{{if eq $line.GetCommentSide "previous"}}
49+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
50+
{{end}}
51+
</td>
52+
<td class="lines-num"></td>
53+
<td class="lines-type-marker"></td>
54+
<td class="add-comment-right">
55+
{{if eq $line.GetCommentSide "proposed"}}
56+
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}
57+
{{end}}
58+
</td>
59+
</tr>
60+
{{end}}
61+
{{if and (eq .GetType 3) $hasmatch}}
62+
{{$match := index $section.Lines $line.Match}}
63+
{{if gt (len $match.Comments) 0}}
64+
<tr class="add-comment" data-line-type="add">
65+
<td class="lines-num"></td>
66+
<td class="lines-type-marker"></td>
67+
<td class="add-comment-left">
68+
{{if eq $match.GetCommentSide "previous"}}
69+
{{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}}
70+
{{end}}
71+
</td>
72+
<td class="lines-num"></td>
73+
<td class="lines-type-marker"></td>
74+
<td class="add-comment-right">
75+
{{if eq $match.GetCommentSide "proposed"}}
76+
{{template "repo/diff/conversation" mergeinto $.root "comments" $match.Comments}}
77+
{{end}}
78+
</td>
79+
</tr>
80+
{{end}}
81+
{{end}}
5082
{{end}}
5183
{{end}}
5284
{{end}}

0 commit comments

Comments
 (0)