summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorMario Lubenka <mario.lubenka@googlemail.com>2019-09-16 11:03:22 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2019-09-16 17:03:22 +0800
commita37236314c88ea7990d4d7b1beeef74dd0aa112b (patch)
tree185fdc69c8a170a94510982a3310efa50b1857e7 /routers
parenta5f87feefd0622115611873da276d4725b6379bb (diff)
downloadgitea-a37236314c88ea7990d4d7b1beeef74dd0aa112b.tar.gz
gitea-a37236314c88ea7990d4d7b1beeef74dd0aa112b.zip
Adds side-by-side diff for images (#6784)
* Adds side-by-side diff for images Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Explain blank imports Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use complete word for width and height labels on image compare Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update index.css from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ImageInfo to git commit file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Assign ImageInfo function for template and sets correct target for BeforeSourcePath Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error if ImageInfo failed Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Avoid template panic when ImageInfo failed for some reason Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show file size on image diff Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unused helper function Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Reverts copyright year change Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Close file reader Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update commit.go Sets correct data key * Moves reader.Close() up a few lines * Updates index.css * Updates CSS file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers adjustments for image compare to compare.go file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts variable name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Apply lesshint recommendations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Do not show old image on image compare if it is not in index of base commit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change file size text Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/commit.go20
-rw-r--r--routers/repo/compare.go54
-rw-r--r--routers/repo/pull.go27
3 files changed, 98 insertions, 3 deletions
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index c3181cbe46..919ebabf42 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -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)
+ return
+ }
+ ctx.Data["ImageInfo"] = parentCommit.ImageInfo
+ }
ctx.Data["Title"] = commit.Summary() + " ยท " + base.ShortSha(commitID)
ctx.Data["Commit"] = commit
ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
@@ -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)
@@ -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)
diff --git a/routers/repo/compare.go b/routers/repo/compare.go
index 4f9a918a7c..7c55dfca30 100644
--- a/routers/repo/compare.go
+++ b/routers/repo/compare.go
@@ -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)
@@ -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
}
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index 14d2f50821..14b8670a20 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -535,6 +535,11 @@ 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)
@@ -542,9 +547,29 @@ func ViewPullFiles(ctx *context.Context) {
}
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)
+ ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "raw", "commit", startCommitID)
+
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireTribute"] = true
if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil {