From a9ca4b410080814051952f71a8339ea81e0de8bf Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 9 May 2022 00:29:50 +0200 Subject: [PATCH] Calculate filename hash only once (#19654) * Calculate hash only once. * remove unused Sha1 template helper function, use ctx.Data["FileNameHash"] * fix unit tests --- modules/templates/helper.go | 6 ------ routers/web/repo/compare.go | 2 +- services/gitdiff/gitdiff.go | 5 ++++- services/repository/files/diff_test.go | 3 ++- templates/repo/diff/blob_excerpt.tmpl | 8 ++++---- templates/repo/diff/box.tmpl | 7 +++---- templates/repo/diff/section_split.tmpl | 14 +++++++------- templates/repo/diff/section_unified.tmpl | 10 +++++----- 8 files changed, 26 insertions(+), 29 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 1201710b92..682459d94a 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -146,7 +146,6 @@ func NewFuncMap() []template.FuncMap { "EllipsisString": base.EllipsisString, "DiffTypeToStr": DiffTypeToStr, "DiffLineTypeToStr": DiffLineTypeToStr, - "Sha1": Sha1, "ShortSha": base.ShortSha, "MD5": base.EncodeMD5, "ActionContent2Commits": ActionContent2Commits, @@ -649,11 +648,6 @@ func DotEscape(raw string) string { return strings.ReplaceAll(raw, ".", "\u200d.\u200d") } -// Sha1 returns sha1 sum of string -func Sha1(str string) string { - return base.EncodeSha1(str) -} - // RenderCommitMessage renders commit message with XSS-safe and special links. func RenderCommitMessage(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML { return RenderCommitMessageLink(ctx, msg, urlPrefix, "", metas) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index d483227ebf..31914c43ab 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -874,7 +874,7 @@ func ExcerptBlob(ctx *context.Context) { } } ctx.Data["section"] = section - ctx.Data["fileName"] = filePath + ctx.Data["FileNameHash"] = base.EncodeSha1(filePath) ctx.Data["AfterCommitID"] = commitID ctx.Data["Anchor"] = anchor ctx.HTML(http.StatusOK, tplBlobExcerpt) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 7fe056a481..92ff92a6c0 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -25,6 +25,7 @@ import ( pull_model "code.gitea.io/gitea/models/pull" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/analyze" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" @@ -604,6 +605,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) Dif // DiffFile represents a file diff. type DiffFile struct { Name string + NameHash string OldName string Index int Addition, Deletion int @@ -952,7 +954,6 @@ parsingLoop: break curFileLoop } } - } // TODO: There are numerous issues with this: @@ -964,6 +965,8 @@ parsingLoop: diffLineTypeBuffers[DiffLineAdd] = new(bytes.Buffer) diffLineTypeBuffers[DiffLineDel] = new(bytes.Buffer) for _, f := range diff.Files { + f.NameHash = base.EncodeSha1(f.Name) + for _, buffer := range diffLineTypeBuffers { buffer.Reset() } diff --git a/services/repository/files/diff_test.go b/services/repository/files/diff_test.go index c0a378dc4b..310308ca88 100644 --- a/services/repository/files/diff_test.go +++ b/services/repository/files/diff_test.go @@ -37,6 +37,7 @@ func TestGetDiffPreview(t *testing.T) { { Name: "README.md", OldName: "README.md", + NameHash: "8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d", Index: 1, Addition: 2, Deletion: 1, @@ -123,7 +124,7 @@ func TestGetDiffPreview(t *testing.T) { assert.NoError(t, err) bs, err := json.Marshal(diff) assert.NoError(t, err) - assert.EqualValues(t, expectedBs, bs) + assert.EqualValues(t, string(expectedBs), string(bs)) }) t.Run("empty branch, same results", func(t *testing.T) { diff --git a/templates/repo/diff/blob_excerpt.tmpl b/templates/repo/diff/blob_excerpt.tmpl index ebfae2795c..7ea33dcf9a 100644 --- a/templates/repo/diff/blob_excerpt.tmpl +++ b/templates/repo/diff/blob_excerpt.tmpl @@ -22,13 +22,13 @@ {{$inlineDiff := $.section.GetComputedInlineDiffFor $line}}{{$inlineDiff.Content}} {{else}} {{$inlineDiff := $.section.GetComputedInlineDiffFor $line}} - + {{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}{{end}} {{if $line.LeftIdx}}{{end}} {{/* */}}{{if $line.LeftIdx}}{{$inlineDiff.Content}}{{end}}{{/* */}} - + {{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}{{end}} {{if $line.RightIdx}}{{end}} {{/* @@ -59,8 +59,8 @@ {{end}} {{else}} - - + + {{end}} {{$inlineDiff := $.section.GetComputedInlineDiffFor $line}} {{if $inlineDiff.EscapeStatus.Escaped}}{{end}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index d6ad169821..bfac8c52e8 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -45,7 +45,7 @@   - {{.Name}} + {{.Name}} {{end}} {{if .Diff.IsIncomplete}} @@ -63,8 +63,7 @@ {{$isImage := or (call $.IsBlobAnImage $blobBase) (call $.IsBlobAnImage $blobHead)}} {{$isCsv := (call $.IsCsvFile $file)}} {{$showFileViewToggle := or $isImage (and (not $file.IsIncomplete) $isCsv)}} - {{$nameHash := Sha1 $file.Name}} -
+

- {{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}} + {{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}} {{if $file.IsGenerated}} {{$.i18n.Tr "repo.diff.generated"}} {{end}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 1f7b0d459c..01083c3dbf 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -7,17 +7,17 @@ {{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}} @@ -28,7 +28,7 @@ {{$match := index $section.Lines $line.Match}} {{- $leftDiff := ""}}{{if $line.LeftIdx}}{{$leftDiff = $section.GetComputedInlineDiffFor $line}}{{end}} {{- $rightDiff := ""}}{{if $match.RightIdx}}{{$rightDiff = $section.GetComputedInlineDiffFor $match}}{{end}} - + {{if $line.LeftIdx}}{{if $leftDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{/* @@ -43,7 +43,7 @@ */}}{{/* */}}{{end}}{{/* */}} - + {{if $match.RightIdx}}{{if $rightDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $match.RightIdx}}{{end}} {{/* @@ -60,7 +60,7 @@ */}} {{else}} {{$inlineDiff := $section.GetComputedInlineDiffFor $line}} - + {{if $line.LeftIdx}}{{if $inlineDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $line.LeftIdx}}{{end}} {{/* @@ -75,7 +75,7 @@ */}}{{/* */}}{{end}}{{/* */}} - + {{if $line.RightIdx}}{{if $inlineDiff.EscapeStatus.Escaped}}{{end}}{{end}} {{if $line.RightIdx}}{{end}} {{/* diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index 2fe3f64903..173b637e86 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -6,24 +6,24 @@ {{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}} {{else}} - - + + {{end}} {{$inlineDiff := $section.GetComputedInlineDiffFor $line -}} {{if $inlineDiff.EscapeStatus.Escaped}}{{end}}