Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds side-by-side diff for images #6784

Merged
merged 42 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5a4401f
Adds side-by-side diff for images
saitho Apr 27, 2019
b034635
Explain blank imports
saitho Apr 27, 2019
0989146
Use complete word for width and height labels on image compare
saitho Apr 28, 2019
72913a6
Merge remote-tracking branch 'origin/master' into feature/6255-Image_…
saitho Apr 28, 2019
1b1915e
Update index.css from master
saitho Apr 28, 2019
8c74e23
Moves ImageInfo to git commit file
saitho Apr 28, 2019
14791f8
Assign ImageInfo function for template and sets correct target for Be…
saitho Apr 28, 2019
1087cb0
Adds missing comment
saitho Apr 28, 2019
a17d536
Merge branch 'master' into feature/6255-Image_diff
jonasfranz Apr 29, 2019
d8d6249
Merge branch 'master' into feature/6255-Image_diff
saitho May 1, 2019
7565651
Return error if ImageInfo failed
saitho May 1, 2019
5cbcd9d
Avoid template panic when ImageInfo failed for some reason
saitho May 1, 2019
903b6e0
Show file size on image diff
saitho May 4, 2019
5b7af2f
Removes unused helper function
saitho May 4, 2019
e4aaf9c
Reverts copyright year change
saitho May 4, 2019
67ff159
Close file reader
saitho May 4, 2019
8f2cedf
Update commit.go
saitho May 4, 2019
f03d38d
Moves reader.Close() up a few lines
saitho May 5, 2019
33f636e
Merge remote-tracking branch 'origin/master' into feature/6255-Image_…
saitho May 17, 2019
210d036
Updates index.css
saitho May 17, 2019
f963740
Updates CSS file
saitho May 19, 2019
172bd4d
Merge branch 'master' into feature/6255-Image_diff
saitho May 25, 2019
d9910df
Merge remote-tracking branch 'upstream/master' into feature/6255-Imag…
saitho Jun 8, 2019
af9dcc5
Transfers adjustments for image compare to compare.go file
saitho Jun 8, 2019
99ac70e
Adjusts variable name
saitho Jun 8, 2019
2f9bfc7
Apply lesshint recommendations
saitho Jun 8, 2019
6834221
Merge branch 'master' into feature/6255-Image_diff
lunny Jun 10, 2019
72849e2
Merge branch 'master' into feature/6255-Image_diff
lunny Jun 22, 2019
3af68e8
Merge branch 'master' into feature/6255-Image_diff
lunny Jul 7, 2019
2507754
Merge branch 'master' into feature/6255-Image_diff
techknowlogick Jul 7, 2019
83c14fc
Merge branch 'master' into feature/6255-Image_diff
saitho Aug 9, 2019
561f080
Do not show old image on image compare if it is not in index of base …
saitho Aug 10, 2019
4f97c18
Change file size text
saitho Aug 10, 2019
d9e8a4b
Merge branch 'master' into feature/6255-Image_diff
lunny Aug 14, 2019
de85e46
Merge branch 'master' into feature/6255-Image_diff
lunny Aug 14, 2019
498b8d2
Merge branch 'master' into feature/6255-Image_diff
lafriks Sep 14, 2019
46bee76
Merge branch 'master' into feature/6255-Image_diff
lunny Sep 14, 2019
6d0453b
Merge branch 'master' into feature/6255-Image_diff
lafriks Sep 14, 2019
8cbbc7e
Merge branch 'master' into feature/6255-Image_diff
lunny Sep 15, 2019
978ea10
Merge branch 'master' into feature/6255-Image_diff
lunny Sep 15, 2019
e4e99a5
Merge branch 'master' into feature/6255-Image_diff
lunny Sep 16, 2019
69083b1
Merge branch 'master' into feature/6255-Image_diff
lafriks Sep 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
saitho marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand All @@ -10,6 +10,11 @@ import (
"bytes"
"container/list"
"fmt"
"image"
"image/color"
_ "image/gif" // for processing gif images
_ "image/jpeg" // for processing jpeg images
_ "image/png" // for processing png images
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -158,6 +163,42 @@ func (c *Commit) IsImageFile(name string) bool {
return isImage
}

// ImageMetaData represents metadata of an image file
type ImageMetaData struct {
ColorModel color.Model
Width int
Height int
ByteSize int64
}

// ImageInfo returns information about the dimensions of an image
func (c *Commit) ImageInfo(name string) (*ImageMetaData, error) {
if !c.IsImageFile(name) {
return nil, nil
}

blob, err := c.GetBlobByPath(name)
if err != nil {
return nil, err
}
reader, err := blob.DataAsync()
if err != nil {
return nil, err
}
config, _, err := image.DecodeConfig(reader)
saitho marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

metadata := ImageMetaData{
ColorModel: config.ColorModel,
Width: config.Width,
Height: config.Height,
ByteSize: blob.Size(),
}
return &metadata, nil
}

// GetCommitByPath return the commit of relative path object.
func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
return c.repo.getCommitByPathWithID(c.ID, relpath)
Expand Down
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,11 @@ diff.whitespace_ignore_at_eol = Ignore changes in whitespace at EOL
diff.stats_desc = <strong> %d changed files</strong> with <strong>%d additions</strong> and <strong>%d deletions</strong>
diff.bin = BIN
diff.view_file = View File
diff.file_before = Before
diff.file_after = After
diff.file_image_width = Width
diff.file_image_height = Height
diff.file_byte_size = File Size
saitho marked this conversation as resolved.
Show resolved Hide resolved
diff.file_suppressed = File diff suppressed because it is too large
diff.too_many_files = Some files were not shown because too many files changed in this diff
diff.comment.placeholder = Leave a comment
Expand Down
2 changes: 1 addition & 1 deletion public/css/index.css

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions public/less/_base.less
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,37 @@ pre, code {
}
}

.border {
border: 1px solid;
&.red {
border-color: #d95c5c !important;
}
&.blue {
border-color: #428bca !important;
}
&.black {
border-color: #444;
}
&.grey {
border-color: #767676 !important;
}
&.light.grey {
border-color: #888 !important;
}
&.green {
border-color: #6cc644 !important;
}
&.purple {
border-color: #6e5494 !important;
}
&.yellow {
border-color: #FBBD08 !important;
}
&.gold {
border-color: #a1882b !important;
}
}

.branch-tag-choice {
line-height: 20px;
}
Expand Down
3 changes: 3 additions & 0 deletions public/less/_repository.less
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,9 @@
// halfwidth is used in split view - and in that case, 1% of each
width: 49%;
}
td.center {
text-align: center;
}

&.tag-code td, td.tag-code {
background-color: #F0F0F0 !important;
Expand Down
42 changes: 41 additions & 1 deletion routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ func Diff(ctx *context.Context) {
ctx.Data["Username"] = userName
ctx.Data["Reponame"] = repoName
ctx.Data["IsImageFile"] = commit.IsImageFile
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
result, err := commit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}
ctx.Data["ImageInfoBase"] = ctx.Data["ImageInfo"]
if commit.ParentCount() > 0 {
parentCommit, err := ctx.Repo.GitRepo.GetCommit(parents[0])
if err != nil {
ctx.NotFound("GetParentCommit", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ctx.ServerError( is better.

return
}
ctx.Data["ImageInfo"] = parentCommit.ImageInfo
}
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
ctx.Data["Commit"] = commit
ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
Expand All @@ -246,10 +263,11 @@ func Diff(ctx *context.Context) {
ctx.Data["Parents"] = parents
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
if commit.ParentCount() > 0 {
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", parents[0])
ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", parents[0])
}
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
ctx.Data["BranchName"], err = commit.GetBranchName()
ctx.HTML(200, tplDiff)
}
Expand All @@ -276,6 +294,12 @@ func CompareDiff(ctx *context.Context) {
beforeCommitID := ctx.Params(":before")
afterCommitID := ctx.Params(":after")

beforeCommit, err := ctx.Repo.GitRepo.GetCommit(beforeCommitID)
if err != nil {
ctx.NotFound("GetCommit", err)
return
}

commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
if err != nil {
ctx.NotFound("GetCommit", err)
Expand Down Expand Up @@ -307,6 +331,22 @@ func CompareDiff(ctx *context.Context) {
ctx.Data["Username"] = userName
ctx.Data["Reponame"] = repoName
ctx.Data["IsImageFile"] = commit.IsImageFile
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
result, err := commit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
saitho marked this conversation as resolved.
Show resolved Hide resolved
result, err := beforeCommit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}
ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
ctx.Data["Commit"] = commit
ctx.Data["Diff"] = diff
Expand Down
58 changes: 53 additions & 5 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Gitea Authors.
// Copyright 2019 The Gitea Authors.
saitho marked this conversation as resolved.
Show resolved Hide resolved
// Copyright 2014 The Gogs Authors.
// All rights reserved.
// Use of this source code is governed by a MIT-style
Expand Down Expand Up @@ -510,16 +510,41 @@ func ViewPullFiles(ctx *context.Context) {
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0

baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
return
}
commit, err := gitRepo.GetCommit(endCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
return
}

ctx.Data["IsImageFile"] = commit.IsImageFile
ctx.Data["ImageInfoBase"] = func(name string) *git.ImageMetaData {
result, err := baseCommit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
result, err := commit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}

baseTarget := path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", endCommitID)
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", startCommitID)
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID)
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "src", "commit", startCommitID)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I changed "headTarget" to "baseTarget", as the "before" paths should point to the forked repository.

ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "raw", "commit", startCommitID)
saitho marked this conversation as resolved.
Show resolved Hide resolved

ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireTribute"] = true
if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {
Expand Down Expand Up @@ -759,7 +784,6 @@ func PrepareCompareDiff(
baseBranch, headBranch string) bool {

var (
repo = ctx.Repo.Repository
err error
title string
)
Expand Down Expand Up @@ -789,6 +813,12 @@ func PrepareCompareDiff(
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0

startCommitID := prInfo.MergeBase
baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
return false
}
headCommit, err := headGitRepo.GetCommit(headCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
Expand Down Expand Up @@ -817,11 +847,29 @@ func PrepareCompareDiff(
ctx.Data["Username"] = headUser.Name
ctx.Data["Reponame"] = headRepo.Name
ctx.Data["IsImageFile"] = headCommit.IsImageFile
ctx.Data["ImageInfoBase"] = func(name string) *git.ImageMetaData {
result, err := baseCommit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
result, err := headCommit.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
}

headTarget := path.Join(headUser.Name, repo.Name)
headTarget := path.Join(headUser.Name, headRepo.Name)
baseTarget := path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", prInfo.MergeBase)
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "src", "commit", prInfo.MergeBase)
ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "raw", "commit", startCommitID)
return false
}

Expand Down
22 changes: 10 additions & 12 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@
<div class="ui attached unstackable table segment">
{{if ne $file.Type 4}}
{{$isImage := (call $.IsImageFile $file.Name)}}
{{if and $isImage}}
<div class="center">
<img src="{{$.RawPath}}/{{EscapePound .Name}}">
</div>
{{else}}
<div class="file-body file-code code-view code-diff {{if $.IsSplitStyle}}code-diff-split{{else}}code-diff-unified{{end}}">
<table>
<tbody>
<div class="file-body file-code code-view code-diff {{if $.IsSplitStyle}}code-diff-split{{else}}code-diff-unified{{end}}">
<table>
<tbody>
{{if $isImage}}
{{template "repo/diff/image_diff" dict "file" . "root" $}}
{{else}}
{{if $.IsSplitStyle}}
{{$highlightClass := $file.GetHighlightClass}}
{{range $j, $section := $file.Sections}}
Expand Down Expand Up @@ -175,10 +173,10 @@
{{else}}
{{template "repo/diff/section_unified" dict "file" . "root" $}}
{{end}}
</tbody>
</table>
</div>
{{end}}
{{end}}
</tbody>
</table>
</div>
{{end}}
</div>
</div>
Expand Down
43 changes: 43 additions & 0 deletions templates/repo/diff/image_diff.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{ $imagePathOld := printf "%s/%s" .root.BeforeRawPath (EscapePound .file.OldName) }}
{{ $imagePathNew := printf "%s/%s" .root.RawPath (EscapePound .file.Name) }}

<tr>
<th class="halfwidth center">
{{.root.i18n.Tr "repo.diff.file_before"}}
</th>
<th class="halfwidth center">
{{.root.i18n.Tr "repo.diff.file_after"}}
</th>
</tr>
<tr>
<td class="halfwidth center">
<a href="{{$imagePathOld}}" target="_blank">
<img src="{{$imagePathOld}}" class="border red" />
</a>
</td>
<td class="halfwidth center">
<a href="{{$imagePathNew}}" target="_blank">
<img src="{{$imagePathNew}}" class="border green" />
</a>
</td>
</tr>
{{ $imageInfoBase := (call .root.ImageInfoBase .file.OldName) }}
{{ $imageInfoHead := (call .root.ImageInfo .file.Name) }}
{{if and $imageInfoBase $imageInfoHead }}
<tr>
<td class="halfwidth center">
{{.root.i18n.Tr "repo.diff.file_image_width"}}: <span class="text {{if not (eq $imageInfoBase.Width $imageInfoHead.Width)}}red{{end}}">{{$imageInfoBase.Width}}</span>
&nbsp;|&nbsp;
{{.root.i18n.Tr "repo.diff.file_image_height"}}: <span class="text {{if not (eq $imageInfoBase.Height $imageInfoHead.Height)}}red{{end}}">{{$imageInfoBase.Height}}</span>
&nbsp;|&nbsp;
{{.root.i18n.Tr "repo.diff.file_byte_size"}}: <span class="text {{if not (eq $imageInfoBase.ByteSize $imageInfoHead.ByteSize)}}red{{end}}">{{FileSize $imageInfoBase.ByteSize}}</span>
</td>
<td class="halfwidth center">
{{.root.i18n.Tr "repo.diff.file_image_width"}}: <span class="text {{if not (eq $imageInfoBase.Width $imageInfoHead.Width)}}green{{end}}">{{$imageInfoHead.Width}}</span>
&nbsp;|&nbsp;
{{.root.i18n.Tr "repo.diff.file_image_height"}}: <span class="text {{if not (eq $imageInfoBase.Height $imageInfoHead.Height)}}green{{end}}">{{$imageInfoHead.Height}}</span>
&nbsp;|&nbsp;
{{.root.i18n.Tr "repo.diff.file_byte_size"}}: <span class="text {{if not (eq $imageInfoBase.ByteSize $imageInfoHead.ByteSize)}}green{{end}}">{{FileSize $imageInfoHead.ByteSize}}</span>
</td>
</tr>
{{end}}