From 73474c043bfdeeb33cd58bdfe42592fea3083422 Mon Sep 17 00:00:00 2001
From: Andrey Nering <andrey.nering@gmail.com>
Date: Sun, 3 Jan 2016 19:26:46 -0200
Subject: [PATCH] Highlighting differences of lines in the diff view.

---
 models/git_diff.go           | 85 ++++++++++++++++++++++++++++++++++++
 models/git_diff_test.go      | 29 ++++++++++++
 public/css/gogs.css          |  6 +++
 public/less/_repository.less |  8 ++++
 routers/repo/commit.go       |  6 +++
 templates/repo/diff_box.tmpl |  6 +--
 6 files changed, 137 insertions(+), 3 deletions(-)
 create mode 100644 models/git_diff_test.go

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
diff --git a/models/git_diff_test.go b/models/git_diff_test.go
new file mode 100644
index 0000000000..4f4d47bf9d
--- /dev/null
+++ b/models/git_diff_test.go
@@ -0,0 +1,29 @@
+package models
+
+import (
+  dmp "github.com/sergi/go-diff/diffmatchpatch"
+  "html/template"
+  "testing"
+)
+
+func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
+  if s1 != string(s2) {
+    t.Errorf("%s should be equal %s", s2, s1)
+  }
+}
+
+func TestDiffToHtml(t *testing.T) {
+  assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHtml([]dmp.Diff{
+    dmp.Diff{dmp.DiffEqual, "foo "},
+    dmp.Diff{dmp.DiffInsert, "bar"},
+    dmp.Diff{dmp.DiffDelete, " baz"},
+    dmp.Diff{dmp.DiffEqual, " biz"},
+  }, DIFF_LINE_ADD))
+
+  assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHtml([]dmp.Diff{
+    dmp.Diff{dmp.DiffEqual, "foo "},
+    dmp.Diff{dmp.DiffDelete, "bar"},
+    dmp.Diff{dmp.DiffInsert, " baz"},
+    dmp.Diff{dmp.DiffEqual, " biz"},
+  }, DIFF_LINE_DEL))
+}
diff --git a/public/css/gogs.css b/public/css/gogs.css
index 0160c5d91a..fd9359af14 100755
--- a/public/css/gogs.css
+++ b/public/css/gogs.css
@@ -2764,6 +2764,12 @@ footer .container .links > *:first-child {
 #delete-repo-modal .ui.message {
   width: 100%!important;
 }
+.removed-code {
+  background-color: #ff9999;
+}
+.added-code {
+  background-color: #99ff99;
+}
 .organization {
   padding-top: 15px;
   padding-bottom: 80px;
diff --git a/public/less/_repository.less b/public/less/_repository.less
index 441832daf2..7bb9c8a94e 100644
--- a/public/less/_repository.less
+++ b/public/less/_repository.less
@@ -1217,3 +1217,11 @@
 		width: 100%!important;
 	}
 }
+
+.removed-code {
+  background-color: #ff9999;
+}
+
+.added-code {
+  background-color: #99ff99;
+}
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index c3fc4d177a..9e46c2e846 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -168,6 +168,12 @@ 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/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl
index a12f4de4ec..f7851822f0 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.Content}}{{end}}</pre>
+														<pre class="wrap">{{if $line.LeftIdx}}{{$line.ParsedContent}}{{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.Content}}{{end}}</pre>
+														<pre class="wrap">{{if $line.RightIdx}}{{$line.ParsedContent}}{{end}}</pre>
 													</td>
 												</tr>
 											{{end}}
@@ -104,7 +104,7 @@
 													</td>
 													{{end}}
 													<td class="lines-code">
-														<pre>{{$line.Content}}</pre>
+														<pre>{{$line.ParsedContent}}</pre>
 													</td>
 												</tr>
 											{{end}}