Просмотр исходного кода

template: better diff handling of rename and deleted files (#6048)

* dep: bump github.com/gogs/git-module from 1.0.2 to 1.1.0

* template: better diff handling or rename and deleted files
ᴜɴᴋɴᴡᴏɴ 4 лет назад
Родитель
Сommit
72111e698e

+ 1 - 1
go.mod

@@ -18,7 +18,7 @@ require (
 	github.com/go-sql-driver/mysql v1.5.0
 	github.com/gogs/chardet v0.0.0-20150115103509-2404f7772561
 	github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
-	github.com/gogs/git-module v1.0.2
+	github.com/gogs/git-module v1.1.0
 	github.com/gogs/go-gogs-client v0.0.0-20200128182646-c69cb7680fd4
 	github.com/gogs/go-libravatar v0.0.0-20191106065024-33a75213d0a0
 	github.com/gogs/minwinsvc v0.0.0-20170301035411-95be6356811a

+ 2 - 0
go.sum

@@ -110,6 +110,8 @@ github.com/gogs/git-module v1.0.1 h1:Xh/sfk6zKjF3y9w2G/dN0YMfLjMhRQzqxMTUPHOL5n4
 github.com/gogs/git-module v1.0.1/go.mod h1:oN37FFStFjdnTJXsSbhIHKJXh2YeDsEcXPATVz/oeuQ=
 github.com/gogs/git-module v1.0.2 h1:YrDZV4g489A4sOF3+gQq85UnVBjLn30+w3PF5PBoGpQ=
 github.com/gogs/git-module v1.0.2/go.mod h1:oN37FFStFjdnTJXsSbhIHKJXh2YeDsEcXPATVz/oeuQ=
+github.com/gogs/git-module v1.1.0 h1:OEQAWvhZ4TCsq6Vw/ftyA37Os1QkiPu1uMQpF6ErzG0=
+github.com/gogs/git-module v1.1.0/go.mod h1:oN37FFStFjdnTJXsSbhIHKJXh2YeDsEcXPATVz/oeuQ=
 github.com/gogs/go-gogs-client v0.0.0-20200128182646-c69cb7680fd4 h1:C7NryI/RQhsIWwC2bHN601P1wJKeuQ6U/UCOYTn3Cic=
 github.com/gogs/go-gogs-client v0.0.0-20200128182646-c69cb7680fd4/go.mod h1:fR6z1Ie6rtF7kl/vBYMfgD5/G5B1blui7z426/sj2DU=
 github.com/gogs/go-libravatar v0.0.0-20191106065024-33a75213d0a0 h1:K02vod+sn3M1OOkdqi2tPxN2+xESK4qyITVQ3JkGEv4=

Разница между файлами не показана из-за своего большого размера
+ 1 - 1
internal/assets/templates/templates_gen.go


+ 6 - 2
internal/route/repo/commit.go

@@ -152,16 +152,18 @@ func Diff(c *context.Context) {
 	c.Data["Username"] = userName
 	c.Data["Reponame"] = repoName
 	c.Data["IsImageFile"] = commit.IsImageFile
+	c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
 	c.Data["Commit"] = commit
 	c.Data["Author"] = db.ValidateCommitWithEmail(commit)
 	c.Data["Diff"] = diff
 	c.Data["Parents"] = parents
 	c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
 	c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", commitID)
+	c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
 	if commit.ParentsCount() > 0 {
 		c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", parents[0])
+		c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", parents[0])
 	}
-	c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
 	c.Success(DIFF)
 }
 
@@ -213,12 +215,14 @@ func CompareDiff(c *context.Context) {
 	c.Data["Username"] = userName
 	c.Data["Reponame"] = repoName
 	c.Data["IsImageFile"] = commit.IsImageFile
+	c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
 	c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
 	c.Data["Commit"] = commit
 	c.Data["Diff"] = diff
 	c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
 	c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", afterCommitID)
-	c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
 	c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", afterCommitID)
+	c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
+	c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", beforeCommitID)
 	c.Success(DIFF)
 }

+ 6 - 2
internal/route/repo/pull.go

@@ -378,6 +378,7 @@ func ViewPullFiles(c *context.Context) {
 
 	c.Data["IsSplitStyle"] = c.Query("style") == "split"
 	c.Data["IsImageFile"] = commit.IsImageFile
+	c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
 
 	// It is possible head repo has been deleted for merged pull requests
 	if pull.HeadRepo != nil {
@@ -386,8 +387,9 @@ func ViewPullFiles(c *context.Context) {
 
 		headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
 		c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", endCommitID)
-		c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", startCommitID)
 		c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", endCommitID)
+		c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", startCommitID)
+		c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", startCommitID)
 	}
 
 	c.Data["RequireHighlightJS"] = true
@@ -595,11 +597,13 @@ func PrepareCompareDiff(
 	c.Data["Username"] = headUser.Name
 	c.Data["Reponame"] = headRepo.Name
 	c.Data["IsImageFile"] = headCommit.IsImageFile
+	c.Data["IsImageFileByIndex"] = headCommit.IsImageFileByIndex
 
 	headTarget := path.Join(headUser.Name, repo.Name)
 	c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", headCommitID)
-	c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", meta.MergeBase)
 	c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", headCommitID)
+	c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", meta.MergeBase)
+	c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", meta.MergeBase)
 	return false
 }
 

+ 54 - 48
templates/repo/diff/box.tmpl

@@ -39,14 +39,12 @@
 				<h4 class="ui top attached normal header">
 					{{$.i18n.Tr "repo.diff.file_suppressed"}}
 					<div class="diff-counter count ui left">
-						{{if not $file.IsRenamed}}
-							<span class="add" data-line="{{.NumAdditions}}">+ {{.NumAdditions}}</span>
-							<span class="bar">
-								<span class="pull-left add"></span>
-								<span class="pull-left del"></span>
-							</span>
-							<span class="del" data-line="{{.NumDeletions}}">- {{.NumDeletions}}</span>
-						{{end}}
+						<span class="add" data-line="{{.NumAdditions}}">+ {{.NumAdditions}}</span>
+						<span class="bar">
+							<span class="pull-left add"></span>
+							<span class="pull-left del"></span>
+						</span>
+						<span class="del" data-line="{{.NumDeletions}}">- {{.NumDeletions}}</span>
 					</div>
 					<span class="file">{{$file.Name}}</span>
 				</h4>
@@ -57,7 +55,7 @@
 					<div class="diff-counter count ui left">
 						{{if $file.IsBinary}}
 							{{$.i18n.Tr "repo.diff.bin"}}
-						{{else if not $file.IsRenamed}}
+						{{else}}
 							<span class="add" data-line="{{.NumAdditions}}">+ {{.NumAdditions}}</span>
 							<span class="bar">
 								<span class="pull-left add"></span>
@@ -78,48 +76,56 @@
 					{{end}}
 				</h4>
 				<div class="ui unstackable attached table segment">
-					{{if not $file.IsRenamed}}
-						{{$isImage := (call $.IsImageFile $file.Name)}}
-						{{if $isImage}}
-							<div class="center">
+					{{$isImage := false}}
+					{{if $file.IsDeleted}}
+						{{$isImage = (call $.IsImageFileByIndex $file.Index)}}
+					{{else}}
+						{{$isImage = (call $.IsImageFile $file.Name)}}
+					{{end}}
+
+					{{if $isImage}}
+						<div class="center">
+							{{if $file.IsDeleted}}
+								<img src="{{$.BeforeRawPath}}/{{EscapePound .Name}}">
+							{{else}}
 								<img src="{{$.RawPath}}/{{EscapePound .Name}}">
-							</div>
-						{{else}}
-							<div class="file-body file-code code-view code-diff">
-								<table>
-									<tbody>
-										{{if $.IsSplitStyle}}
-											{{$highlightClass := $file.HighlightClass}}
-											{{range $j, $section := $file.Sections}}
-												{{range $k, $line := $section.Lines}}
-													<tr class="{{DiffLineTypeToStr .Type}}-code nl-{{$k}} ol-{{$k}}">
-														{{if eq .Type 4}}
-															<td class="lines-num"></td>
-															<td colspan="3"  class="lines-code">
-																<pre><code class="{{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{$section.ComputedInlineDiffFor $line}}</code></pre>
-															</td>
-														{{else}}
-															<td class="lines-num lines-num-old" {{if $line.LeftLine}} id="diff-{{Sha1 $file.Index}}L{{$line.LeftLine}}" data-line-number="{{$line.LeftLine}}"{{end}}>
-															</td>
-															<td class="lines-code halfwidth">
-																<pre><code class="wrap {{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{if $line.LeftLine}}{{$section.ComputedInlineDiffFor $line}}{{end}}</code></pre>
-															</td>
-															<td class="lines-num lines-num-new" {{if $line.RightLine}} id="diff-{{Sha1 $file.Index}}R{{$line.RightLine}}" data-line-number="{{$line.RightLine}}"{{end}}>
-															</td>
-															<td class="lines-code halfwidth">
-																<pre><code class="wrap {{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{if $line.RightLine}}{{$section.ComputedInlineDiffFor $line}}{{end}}</code></pre>
-															</td>
-														{{end}}
-													</tr>
-												{{end}}
+							{{end}}
+						</div>
+					{{else}}
+						<div class="file-body file-code code-view code-diff">
+							<table>
+								<tbody>
+									{{if $.IsSplitStyle}}
+										{{$highlightClass := $file.HighlightClass}}
+										{{range $j, $section := $file.Sections}}
+											{{range $k, $line := $section.Lines}}
+												<tr class="{{DiffLineTypeToStr .Type}}-code nl-{{$k}} ol-{{$k}}">
+													{{if eq .Type 4}}
+														<td class="lines-num"></td>
+														<td colspan="3"  class="lines-code">
+															<pre><code class="{{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{$section.ComputedInlineDiffFor $line}}</code></pre>
+														</td>
+													{{else}}
+														<td class="lines-num lines-num-old" {{if $line.LeftLine}} id="diff-{{Sha1 $file.Index}}L{{$line.LeftLine}}" data-line-number="{{$line.LeftLine}}"{{end}}>
+														</td>
+														<td class="lines-code halfwidth">
+															<pre><code class="wrap {{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{if $line.LeftLine}}{{$section.ComputedInlineDiffFor $line}}{{end}}</code></pre>
+														</td>
+														<td class="lines-num lines-num-new" {{if $line.RightLine}} id="diff-{{Sha1 $file.Index}}R{{$line.RightLine}}" data-line-number="{{$line.RightLine}}"{{end}}>
+														</td>
+														<td class="lines-code halfwidth">
+															<pre><code class="wrap {{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{if $line.RightLine}}{{$section.ComputedInlineDiffFor $line}}{{end}}</code></pre>
+														</td>
+													{{end}}
+												</tr>
 											{{end}}
-										{{else}}
-											{{template "repo/diff/section_unified" .}}
 										{{end}}
-									</tbody>
-								</table>
-							</div>
-						{{end}}
+									{{else}}
+										{{template "repo/diff/section_unified" .}}
+									{{end}}
+								</tbody>
+							</table>
+						</div>
 					{{end}}
 				</div>
 			</div>

Некоторые файлы не были показаны из-за большого количества измененных файлов