From 8e262104c25d1c2578f683109e1b373aade3a17c Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Sat, 5 Jun 2021 14:32:19 +0200 Subject: Add Image Diff for SVG files (#14867) * Added type sniffer. * Switched content detection from base to typesniffer. * Added GuessContentType to Blob. * Moved image info logic to client. Added support for SVG images in diff. * Restore old blocked svg behaviour. * Added missing image formats. * Execute image diff only when container is visible. * add margin to spinner * improve BIN tag on image diffs * Default to render view. * Show image diff on incomplete diff. Co-authored-by: silverwind Co-authored-by: Lunny Xiao Co-authored-by: Lauris BH --- templates/repo/diff/box.tmpl | 156 ++++++++++++++++-------------------- templates/repo/diff/image_diff.tmpl | 66 +++++---------- 2 files changed, 87 insertions(+), 135 deletions(-) (limited to 'templates/repo/diff') diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index d8678c95c6..1ca2dcc4d8 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -29,10 +29,12 @@ {{range .Diff.Files}}
  • - {{if not .IsBin}} - {{template "repo/diff/stats" dict "file" . "root" $}} + {{if .IsBin}} + + {{$.i18n.Tr "repo.diff.bin"}} + {{else}} - {{$.i18n.Tr "repo.diff.bin"}} + {{template "repo/diff/stats" dict "file" . "root" $}} {{end}}
    @@ -42,108 +44,84 @@ {{end}} {{range $i, $file := .Diff.Files}} - {{if $file.IsIncomplete}} -
    -

    + {{$blobBase := call $.GetBlobByPathForCommit $.BaseCommit $file.OldName}} + {{$blobHead := call $.GetBlobByPathForCommit $.HeadCommit $file.Name}} + {{$isImage := or (call $.IsBlobAnImage $blobBase) (call $.IsBlobAnImage $blobHead)}} + {{$isCsv := (call $.IsCsvFile $file)}} + {{$showFileViewToggle := or $isImage (and (not $file.IsIncomplete) $isCsv)}} +
    +

    +
    {{svg "octicon-chevron-down" 18}} -
    - {{template "repo/diff/stats" dict "file" . "root" $}} -
    - {{$file.Name}} -
    -
    - {{if $file.IsIncompleteLineTooLong}} - {{$.i18n.Tr "repo.diff.file_suppressed_line_too_long"}} - {{else}} - {{$.i18n.Tr "repo.diff.file_suppressed"}} - {{end}} -
    - {{if $file.IsProtected}} - {{$.i18n.Tr "repo.diff.protected"}} - {{end}} - {{if and (not $file.IsSubmodule) (not $.PageIsWiki)}} - {{if $file.IsDeleted}} - {{$.i18n.Tr "repo.diff.view_file"}} - {{else}} - {{$.i18n.Tr "repo.diff.view_file"}} - {{end}} +
    + {{if $file.IsBin}} + + {{$.i18n.Tr "repo.diff.bin"}} + + {{else}} + {{template "repo/diff/stats" dict "file" . "root" $}} {{end}}
    -

    -
    - {{else}} -
    -

    -
    - {{$isImage := false}} + {{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}} +
    +
    + {{if $showFileViewToggle}} +
    + {{svg "octicon-code"}} + {{svg "octicon-file"}} +
    + {{end}} + {{if $file.IsProtected}} + {{$.i18n.Tr "repo.diff.protected"}} + {{end}} + {{if and (not $file.IsSubmodule) (not $.PageIsWiki)}} {{if $file.IsDeleted}} - {{$isImage = (call $.IsImageFileInBase $file.Name)}} + {{$.i18n.Tr "repo.diff.view_file"}} {{else}} - {{$isImage = (call $.IsImageFileInHead $file.Name)}} + {{$.i18n.Tr "repo.diff.view_file"}} {{end}} - {{$isCsv := (call $.IsCsvFile $file)}} - {{$showFileViewToggle := or $isImage $isCsv}} - - {{svg "octicon-chevron-down" 18}} - -
    - {{if $file.IsBin}} - {{$.i18n.Tr "repo.diff.bin"}} + {{end}} +
    +

    +
    +
    + {{if or $file.IsIncomplete $file.IsBin}} +
    + {{if $file.IsIncomplete}} + {{if $file.IsIncompleteLineTooLong}} + {{$.i18n.Tr "repo.diff.file_suppressed_line_too_long"}} + {{else}} + {{$.i18n.Tr "repo.diff.file_suppressed"}} + {{end}} {{else}} - {{template "repo/diff/stats" dict "file" . "root" $}} + {{$.i18n.Tr "repo.diff.bin_not_shown"}} {{end}}
    - {{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}} -
    -
    - {{if $showFileViewToggle}} -
    - {{svg "octicon-code"}} - {{svg "octicon-file"}} -
    - {{end}} - {{if $file.IsProtected}} - {{$.i18n.Tr "repo.diff.protected"}} - {{end}} - {{if and (not $file.IsSubmodule) (not $.PageIsWiki)}} - {{if $file.IsDeleted}} - {{$.i18n.Tr "repo.diff.view_file"}} + {{else}} + + {{if $.IsSplitStyle}} + {{template "repo/diff/section_split" dict "file" . "root" $}} {{else}} - {{$.i18n.Tr "repo.diff.view_file"}} + {{template "repo/diff/section_unified" dict "file" . "root" $}} {{end}} - {{end}} - - -
    -
    - {{if $file.IsBin}} -
    {{$.i18n.Tr "repo.diff.bin_not_shown"}}
    - {{else}} -
    - {{if $.IsSplitStyle}} - {{template "repo/diff/section_split" dict "file" . "root" $}} - {{else}} - {{template "repo/diff/section_unified" dict "file" . "root" $}} - {{end}} -
    - {{end}} -
    - {{if or $isImage $isCsv}} -
    - - {{if $isImage}} - {{template "repo/diff/image_diff" dict "file" . "root" $}} - {{else}} - {{template "repo/diff/csv_diff" dict "file" . "root" $}} - {{end}} -
    -
    + {{end}}
    + {{if $showFileViewToggle}} +
    + + {{if $isImage}} + {{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}} + {{else}} + {{template "repo/diff/csv_diff" dict "file" . "root" $}} + {{end}} +
    +
    + {{end}}
    - {{end}} +

    {{end}} {{if .Diff.IsIncomplete}} diff --git a/templates/repo/diff/image_diff.tmpl b/templates/repo/diff/image_diff.tmpl index 91092c412f..33fa8c9e2c 100644 --- a/templates/repo/diff/image_diff.tmpl +++ b/templates/repo/diff/image_diff.tmpl @@ -1,15 +1,13 @@ {{ $imagePathOld := printf "%s/%s" .root.BeforeRawPath (EscapePound .file.OldName) }} {{ $imagePathNew := printf "%s/%s" .root.RawPath (EscapePound .file.Name) }} -{{ $imageInfoBase := (call .root.ImageInfoBase .file.OldName) }} -{{ $imageInfoHead := (call .root.ImageInfo .file.Name) }} -{{if or $imageInfoBase $imageInfoHead}} +{{if or .blobBase .blobHead}}