aboutsummaryrefslogtreecommitdiffstats
path: root/models/git_diff.go
diff options
context:
space:
mode:
authorAndrey Nering <andrey.nering@gmail.com>2016-01-03 19:26:46 -0200
committerAndrey Nering <andrey.nering@gmail.com>2016-01-06 17:46:56 -0200
commit73474c043bfdeeb33cd58bdfe42592fea3083422 (patch)
tree0aab45170a12b50e2f10c2125cb11ae18c879f87 /models/git_diff.go
parent0cb739684096396e26595ac70817a2a05b61b443 (diff)
downloadgitea-73474c043bfdeeb33cd58bdfe42592fea3083422.tar.gz
gitea-73474c043bfdeeb33cd58bdfe42592fea3083422.zip
Highlighting differences of lines in the diff view.
Diffstat (limited to 'models/git_diff.go')
-rw-r--r--models/git_diff.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index 22075ef76b..9656813bf8 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -13,6 +13,8 @@ import (
"os"
"os/exec"
"strings"
+ "html/template"
+ "html"
"github.com/Unknwon/com"
"golang.org/x/net/html/charset"
@@ -23,6 +25,7 @@ import (
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/process"
+ "github.com/sergi/go-diff/diffmatchpatch"
)
// Diff line types.
@@ -45,6 +48,7 @@ type DiffLine struct {
RightIdx int
Type int
Content string
+ ParsedContent template.HTML
}
func (d DiffLine) GetType() int {
@@ -56,6 +60,87 @@ type DiffSection struct {
Lines []*DiffLine
}
+func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType int) template.HTML {
+ result := ""
+ for _, s := range diffRecord {
+ if s.Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD {
+ result = result + "<span class=\"added-code\">"+html.EscapeString(s.Text)+"</span>"
+ } else if s.Type == diffmatchpatch.DiffDelete && lineType == DIFF_LINE_DEL {
+ result = result + "<span class=\"removed-code\">"+html.EscapeString(s.Text)+"</span>"
+ } else if s.Type == diffmatchpatch.DiffEqual {
+ result = result + html.EscapeString(s.Text)
+ }
+ }
+ return template.HTML(result)
+}
+
+func (diffSection *DiffSection) GetLeftLine(idx int, sliceIdx int) *DiffLine {
+ for i, diffLine := range diffSection.Lines {
+ if diffLine.LeftIdx == idx && diffLine.RightIdx == 0 {
+ // ignore the the lines are too far from each other
+ if i > sliceIdx-5 && i < sliceIdx+5 {
+ return diffLine
+ } else {
+ return nil
+ }
+ }
+ }
+ return nil
+}
+
+func (diffSection *DiffSection) GetRightLine(idx int, sliceIdx int) *DiffLine {
+ for i, diffLine := range diffSection.Lines {
+ if diffLine.RightIdx == idx && diffLine.LeftIdx == 0 {
+ // ignore the the lines are too far from each other
+ if i > sliceIdx-5 && i < sliceIdx+5 {
+ return diffLine
+ } else {
+ return nil
+ }
+ }
+ }
+ return nil
+}
+
+// computes diff of each diff line and set the HTML on diffLine.ParsedContent
+func (diffSection *DiffSection) ComputeLinesDiff() {
+ for i, diffLine := range diffSection.Lines {
+ var compareDiffLine *DiffLine
+ var diff1, diff2 string
+
+ // default content: as is
+ diffLine.ParsedContent = 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
+ }
+
+ // try to find equivalent diff line. ignore, otherwise
+ if diffLine.Type == DIFF_LINE_ADD {
+ compareDiffLine = diffSection.GetLeftLine(diffLine.RightIdx, i)
+ if compareDiffLine == nil {
+ continue
+ }
+ diff1 = compareDiffLine.Content
+ diff2 = diffLine.Content
+ } else {
+ compareDiffLine = diffSection.GetRightLine(diffLine.LeftIdx, i)
+ if compareDiffLine == nil {
+ continue
+ }
+ diff1 = diffLine.Content
+ diff2 = compareDiffLine.Content
+ }
+
+ dmp := diffmatchpatch.New()
+ diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
+ diffRecord = dmp.DiffCleanupSemantic(diffRecord)
+
+ diffLine.ParsedContent = diffToHtml(diffRecord, diffLine.Type)
+ }
+}
+
type DiffFile struct {
Name string
OldName string