From 58f4a4114eb53cff9da13923ea95edd22a904840 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 30 Aug 2021 16:43:06 +0100 Subject: [PATCH 1/2] 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 d40f8a50fe089..da50284150a28 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 d50e41eb40279..59da680d48f22 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 1ca2dcc4d8144..4f8f7260976aa 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 2f959ac2da7e8..aed6d784b307c 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}} From 49a71a646130f8d8da0f71b567051a0ce6ea9159 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Aug 2021 23:00:45 +0200 Subject: [PATCH 2/2] Ensure wiki repos are all closed (#16886) (#16888) There are multiple places where wiki git repositories are not properly closed. This PR ensures they are closed. Signed-off-by: Andrew Thornton Co-authored-by: Andrew Thornton --- routers/web/repo/wiki.go | 66 +++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 5271fe9b4ad99..523fc6ed06877 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -135,6 +135,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { wikiRepo, commit, err := findWikiRepoCommit(ctx) if err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } if !git.IsErrNotExist(err) { ctx.ServerError("GetBranchCommit", err) } @@ -222,6 +225,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { var buf strings.Builder if err := markdown.Render(rctx, bytes.NewReader(data), &buf); err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } ctx.ServerError("Render", err) return nil, nil } @@ -229,6 +235,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { buf.Reset() if err := markdown.Render(rctx, bytes.NewReader(sidebarContent), &buf); err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } ctx.ServerError("Render", err) return nil, nil } @@ -237,6 +246,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { buf.Reset() if err := markdown.Render(rctx, bytes.NewReader(footerContent), &buf); err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } ctx.ServerError("Render", err) return nil, nil } @@ -380,17 +392,14 @@ func Wiki(ctx *context.Context) { } wikiRepo, entry := renderViewPage(ctx) - if ctx.Written() { - if wikiRepo != nil { - wikiRepo.Close() - } - return - } defer func() { if wikiRepo != nil { wikiRepo.Close() } }() + if ctx.Written() { + return + } if entry == nil { ctx.Data["Title"] = ctx.Tr("repo.wiki") ctx.HTML(http.StatusOK, tplWikiStart) @@ -425,17 +434,15 @@ func WikiRevision(ctx *context.Context) { } wikiRepo, entry := renderRevisionPage(ctx) - if ctx.Written() { - if wikiRepo != nil { - wikiRepo.Close() - } - return - } defer func() { if wikiRepo != nil { wikiRepo.Close() } }() + + if ctx.Written() { + return + } if entry == nil { ctx.Data["Title"] = ctx.Tr("repo.wiki") ctx.HTML(http.StatusOK, tplWikiStart) @@ -472,13 +479,14 @@ func WikiPages(ctx *context.Context) { } return } - - entries, err := commit.ListEntries() - if err != nil { + defer func() { if wikiRepo != nil { wikiRepo.Close() } + }() + entries, err := commit.ListEntries() + if err != nil { ctx.ServerError("ListEntries", err) return } @@ -489,10 +497,6 @@ func WikiPages(ctx *context.Context) { } c, err := wikiRepo.GetCommitByPath(entry.Name()) if err != nil { - if wikiRepo != nil { - wikiRepo.Close() - } - ctx.ServerError("GetCommit", err) return } @@ -501,10 +505,6 @@ func WikiPages(ctx *context.Context) { if models.IsErrWikiInvalidFileName(err) { continue } - if wikiRepo != nil { - wikiRepo.Close() - } - ctx.ServerError("WikiFilenameToName", err) return } @@ -516,21 +516,25 @@ func WikiPages(ctx *context.Context) { } ctx.Data["Pages"] = pages - defer func() { - if wikiRepo != nil { - wikiRepo.Close() - } - }() ctx.HTML(http.StatusOK, tplWikiPages) } // WikiRaw outputs raw blob requested by user (image for example) func WikiRaw(ctx *context.Context) { wikiRepo, commit, err := findWikiRepoCommit(ctx) - if err != nil { + defer func() { if wikiRepo != nil { + wikiRepo.Close() + } + }() + + if err != nil { + if git.IsErrNotExist(err) { + ctx.NotFound("findEntryForFile", nil) return } + ctx.ServerError("findEntryForfile", err) + return } providedPath := ctx.Params("*") @@ -546,9 +550,7 @@ func WikiRaw(ctx *context.Context) { if entry == nil { // Try to find a wiki page with that name - if strings.HasSuffix(providedPath, ".md") { - providedPath = providedPath[:len(providedPath)-3] - } + providedPath = strings.TrimSuffix(providedPath, ".md") wikiPath := wiki_service.NameToFilename(providedPath) entry, err = findEntryForFile(commit, wikiPath)