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 all 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
49 changes: 49 additions & 0 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,43 @@ 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
}
defer reader.Close()
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 Expand Up @@ -310,6 +352,13 @@ func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, erro
return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String())
}

// HasFile returns true if the file given exists on this commit
// This does only mean it's there - it does not mean the file was changed during the commit.
func (c *Commit) HasFile(filename string) (bool, error) {
result, err := c.repo.LsFiles(filename)
return result[0] == filename, err
}

// GetSubModules get all the sub modules of current revision git tree
func (c *Commit) GetSubModules() (*ObjectCache, error) {
if c.submoduleCache != nil {
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 @@ -1358,6 +1358,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 = Size
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
11 changes: 11 additions & 0 deletions public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ a{cursor:pointer}
.ui .migrate{color:#888!important;opacity:.5}
.ui .migrate a{color:#444!important}
.ui .migrate a:hover{color:#000!important}
.ui .border{border:1px solid}
.ui .border.red{border-color:#d95c5c!important}
.ui .border.blue{border-color:#428bca!important}
.ui .border.black{border-color:#444}
.ui .border.grey{border-color:#767676!important}
.ui .border.light.grey{border-color:#888!important}
.ui .border.green{border-color:#6cc644!important}
.ui .border.purple{border-color:#6e5494!important}
.ui .border.yellow{border-color:#fbbd08!important}
.ui .border.gold{border-color:#a1882b!important}
.ui .branch-tag-choice{line-height:20px}
@media only screen and (max-width:767px){.ui.pagination.menu .item.navigation span.navigation_label,.ui.pagination.menu .item:not(.active):not(.navigation){display:none}
}
Expand Down Expand Up @@ -670,6 +680,7 @@ footer .ui.left,footer .ui.right{line-height:40px}
.repository .diff-file-box .code-diff td{padding:0 0 0 10px!important;border-top:0}
.repository .diff-file-box .code-diff .lines-num{border-color:#d4d4d5;border-right-width:1px;border-right-style:solid;padding:0 5px!important}
.repository .diff-file-box .code-diff tbody tr td.halfwidth{width:49%}
.repository .diff-file-box .code-diff tbody tr td.center{text-align:center}
.repository .diff-file-box .code-diff tbody tr .removed-code{background-color:#f99}
.repository .diff-file-box .code-diff tbody tr .added-code{background-color:#9f9}
.repository .diff-file-box .code-diff tbody tr [data-line-num]::before{content:attr(data-line-num);text-align:right}
Expand Down
39 changes: 39 additions & 0 deletions public/less/_base.less
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,45 @@ code,
}
}

.border {
border: 1px solid;
&.red {
border-color: #d95c5c !important;
}

&.blue {
border-color: #428bca !important;
}

&.black {
border-color: #444444;
}

&.grey {
border-color: #767676 !important;
}

&.light.grey {
border-color: #888888 !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
4 changes: 4 additions & 0 deletions public/less/_repository.less
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,10 @@
width: 49%;
}

td.center {
text-align: center;
}

.removed-code {
background-color: #ff9999;
}
Expand Down
20 changes: 19 additions & 1 deletion routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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 @@ -248,6 +265,7 @@ 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)

note := &git.Note{}
err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
Expand All @@ -259,8 +277,8 @@ func Diff(ctx *context.Context) {

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()
if err != nil {
ctx.ServerError("commit.GetBranchName", err)
Expand Down
54 changes: 53 additions & 1 deletion routers/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ func PrepareCompareDiff(
return false
}

baseGitRepo := ctx.Repo.GitRepo
baseCommitID := baseBranch
if ctx.Data["BaseIsCommit"] == false {
if ctx.Data["BaseIsTag"] == true {
baseCommitID, err = baseGitRepo.GetTagCommitID(baseBranch)
} else {
baseCommitID, err = baseGitRepo.GetBranchCommitID(baseBranch)
}
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return false
}
}

baseCommit, err := baseGitRepo.GetCommit(baseCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
return false
}

compareInfo.Commits = models.ValidateCommitsWithEmails(compareInfo.Commits)
compareInfo.Commits = models.ParseCommitsWithSignature(compareInfo.Commits)
compareInfo.Commits = models.ParseCommitsWithStatus(compareInfo.Commits, headRepo)
Expand All @@ -272,11 +292,43 @@ func PrepareCompareDiff(
ctx.Data["Username"] = headUser.Name
ctx.Data["Reponame"] = headRepo.Name
ctx.Data["IsImageFile"] = headCommit.IsImageFile
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
}
ctx.Data["FileExistsInBaseCommit"] = func(filename string) bool {
result, err := baseCommit.HasFile(filename)
if err != nil {
log.Error(
"Error while checking if file \"%s\" exists in base commit \"%s\" (repo: %s): %v",
filename,
baseCommit,
baseGitRepo.Path,
err)
return false
}
return result
}
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
}

headTarget := path.Join(headUser.Name, repo.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", compareInfo.MergeBase)
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "src", "commit", baseCommitID)
ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "raw", "commit", baseCommitID)

return false
}

Expand Down
27 changes: 26 additions & 1 deletion routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,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
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 @@ -164,10 +162,10 @@
{{else}}
{{template "repo/diff/section_unified" dict "file" . "root" $}}
{{end}}
</tbody>
</table>
</div>
{{end}}
{{end}}
</tbody>
</table>
</div>
{{end}}
</div>
</div>
Expand Down
Loading