aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Nering <andrey.nering@gmail.com>2016-01-27 18:54:08 -0200
committerAndrey Nering <andrey.nering@gmail.com>2016-01-27 18:54:08 -0200
commit5deb726f3fc5335b7391a28f6b21328b1335dd9e (patch)
tree03044e2fb9dfee8f6c8f77356d983936cceb105b
parent93f40995b30b67e80e3fff36fe7e7ced756a13d8 (diff)
downloadgitea-5deb726f3fc5335b7391a28f6b21328b1335dd9e.tar.gz
gitea-5deb726f3fc5335b7391a28f6b21328b1335dd9e.zip
Refactoring of inline diff computing to prevent empty diff box. Fix #2489
-rw-r--r--models/git_diff.go61
-rw-r--r--routers/repo/commit.go6
-rw-r--r--routers/repo/pull.go6
-rw-r--r--templates/repo/diff_box.tmpl6
4 files changed, 33 insertions, 46 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index 1913ec46ea..e8bfe61027 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -51,7 +51,6 @@ type DiffLine struct {
RightIdx int
Type DiffLineType
Content string
- ParsedContent template.HTML
}
func (d *DiffLine) GetType() int {
@@ -112,42 +111,42 @@ func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLin
return nil
}
-// computes diff of each diff line and set the HTML on diffLine.ParsedContent
-func (diffSection *DiffSection) ComputeLinesDiff() {
- for _, diffLine := range diffSection.Lines {
- var compareDiffLine *DiffLine
- var diff1, diff2 string
+// computes inline diff for the given line
+func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
+ var compareDiffLine *DiffLine
+ var diff1, diff2 string
- diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:]))
+ getDefaultReturn := func() template.HTML {
+ return template.HTML(html.EscapeString(diffLine.Content[1:]))
+ }
- // just compute diff for adds and removes
- if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL {
- continue
- }
+ // just compute diff for adds and removes
+ if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL {
+ return getDefaultReturn()
+ }
- // try to find equivalent diff line. ignore, otherwise
- if diffLine.Type == DIFF_LINE_ADD {
- compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
- if compareDiffLine == nil {
- continue
- }
- diff1 = compareDiffLine.Content
- diff2 = diffLine.Content
- } else {
- compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
- if compareDiffLine == nil {
- continue
- }
- diff1 = diffLine.Content
- diff2 = compareDiffLine.Content
+ // try to find equivalent diff line. ignore, otherwise
+ if diffLine.Type == DIFF_LINE_ADD {
+ compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
+ if compareDiffLine == nil {
+ return getDefaultReturn()
+ }
+ diff1 = compareDiffLine.Content
+ diff2 = diffLine.Content
+ } else {
+ compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
+ if compareDiffLine == nil {
+ return getDefaultReturn()
}
+ diff1 = diffLine.Content
+ diff2 = compareDiffLine.Content
+ }
- dmp := diffmatchpatch.New()
- diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
- diffRecord = dmp.DiffCleanupSemantic(diffRecord)
+ dmp := diffmatchpatch.New()
+ diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
+ diffRecord = dmp.DiffCleanupSemantic(diffRecord)
- diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type)
- }
+ return diffToHTML(diffRecord, diffLine.Type)
}
type DiffFile struct {
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index 9e46c2e846..c3fc4d177a 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -168,12 +168,6 @@ func Diff(ctx *middleware.Context) {
}
}
- for _, diffFile := range diff.Files {
- for _, diffSection := range diffFile.Sections {
- diffSection.ComputeLinesDiff()
- }
- }
-
ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
ctx.Data["Username"] = userName
ctx.Data["Reponame"] = repoName
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index 770ec471e4..e3500716d3 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -349,12 +349,6 @@ func ViewPullFiles(ctx *middleware.Context) {
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
- for _, diffFile := range diff.Files {
- for _, diffSection := range diffFile.Sections {
- diffSection.ComputeLinesDiff()
- }
- }
-
commit, err := gitRepo.GetCommit(endCommitID)
if err != nil {
ctx.Handle(500, "GetCommit", err)
diff --git a/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl
index da512ebf0b..4ea531c3c5 100644
--- a/templates/repo/diff_box.tmpl
+++ b/templates/repo/diff_box.tmpl
@@ -76,13 +76,13 @@
<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
</td>
<td class="lines-code halfwidth">
- <pre class="wrap">{{if $line.LeftIdx}}{{$line.ParsedContent}}{{end}}</pre>
+ <pre class="wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
</td>
<td class="lines-num lines-num-new">
<span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span>
</td>
<td class="lines-code halfwidth">
- <pre class="wrap">{{if $line.RightIdx}}{{$line.ParsedContent}}{{end}}</pre>
+ <pre class="wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
</td>
</tr>
{{end}}
@@ -104,7 +104,7 @@
</td>
{{end}}
<td class="lines-code">
- <pre>{{$line.ParsedContent}}</pre>
+ <pre>{{$section.GetComputedInlineDiffFor $line}}</pre>
</td>
</tr>
{{end}}