summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-01-28 05:33:27 -0500
committerUnknwon <u@gogs.io>2016-01-28 05:33:27 -0500
commit9b8ad01bc08bdcdb9aa69659215de4909aa8026c (patch)
tree924d26eb0ee263c50d8cd3577d9271a8813b3b7f /models
parent8eb057779149fb89ebeef8385415455f8093658c (diff)
parent5deb726f3fc5335b7391a28f6b21328b1335dd9e (diff)
downloadgitea-9b8ad01bc08bdcdb9aa69659215de4909aa8026c.tar.gz
gitea-9b8ad01bc08bdcdb9aa69659215de4909aa8026c.zip
Merge pull request #2493 from andreynering/fix-2489
Refactoring of inline diff computing to prevent empty diff box.
Diffstat (limited to 'models')
-rw-r--r--models/git_diff.go61
1 files changed, 30 insertions, 31 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 {