From 86bce4a2ae9915fad63c742cb68fcd0a5e64a223 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Sat, 9 Jan 2016 14:51:17 +0800
Subject: [PATCH] minor fix to #2335

---
 .gopmfile                    |  1 +
 README.md                    |  2 +-
 gogs.go                      |  2 +-
 models/git_diff.go           | 55 +++++++++++++++++++++---------------
 public/css/gogs.css          | 17 +++++------
 public/less/_repository.less | 20 +++++++------
 templates/.VERSION           |  2 +-
 7 files changed, 57 insertions(+), 42 deletions(-)

diff --git a/.gopmfile b/.gopmfile
index ddd761705c..ed7e595708 100644
--- a/.gopmfile
+++ b/.gopmfile
@@ -31,6 +31,7 @@ github.com/microcosm-cc/bluemonday = commit:4ac6f27
 github.com/msteinert/pam = commit:02ccfbf
 github.com/nfnt/resize = commit:dc93e1b98c
 github.com/russross/blackfriday = commit:d18b67a
+github.com/sergi/go-diff = 
 github.com/shurcooL/sanitized_anchor_name = commit:10ef21a
 github.com/Unknwon/cae = commit:7f5e046
 github.com/Unknwon/com = commit:28b053d
diff --git a/README.md b/README.md
index 2949693219..ba3ad35af8 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
 
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
 
-##### Current version: 0.8.17
+##### Current version: 0.8.18
 
 | Web | UI  | Preview  |
 |:-------------:|:-------:|:-------:|
diff --git a/gogs.go b/gogs.go
index 1d17923d60..006a272ed1 100644
--- a/gogs.go
+++ b/gogs.go
@@ -17,7 +17,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.8.17.0109"
+const APP_VER = "0.8.18.0109"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/models/git_diff.go b/models/git_diff.go
index bfbc9c73a0..b5483ffa60 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -8,18 +8,18 @@ import (
 	"bufio"
 	"bytes"
 	"fmt"
+	"html"
+	"html/template"
 	"io"
 	"io/ioutil"
 	"os"
 	"os/exec"
 	"strings"
-	"html/template"
-	"html"
 
 	"github.com/Unknwon/com"
+	"github.com/sergi/go-diff/diffmatchpatch"
 	"golang.org/x/net/html/charset"
 	"golang.org/x/text/transform"
-	"github.com/sergi/go-diff/diffmatchpatch"
 
 	"github.com/gogits/git-module"
 
@@ -47,10 +47,10 @@ const (
 )
 
 type DiffLine struct {
-	LeftIdx  int
-	RightIdx int
-	Type     DiffLineType
-	Content  string
+	LeftIdx       int
+	RightIdx      int
+	Type          DiffLineType
+	Content       string
 	ParsedContent template.HTML
 }
 
@@ -63,18 +63,29 @@ type DiffSection struct {
 	Lines []*DiffLine
 }
 
-func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType DiffLineType) 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)
+var (
+	addedCodePrefix   = []byte("<span class=\"added-code\">")
+	removedCodePrefix = []byte("<span class=\"removed-code\">")
+	codeTagSuffix     = []byte("</span>")
+)
+
+func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
+	var buf bytes.Buffer
+	for i := range diffs {
+		if diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD {
+			buf.Write(addedCodePrefix)
+			buf.WriteString(html.EscapeString(diffs[i].Text))
+			buf.Write(codeTagSuffix)
+		} else if diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DIFF_LINE_DEL {
+			buf.Write(removedCodePrefix)
+			buf.WriteString(html.EscapeString(diffs[i].Text))
+			buf.Write(codeTagSuffix)
+		} else if diffs[i].Type == diffmatchpatch.DiffEqual {
+			buf.WriteString(html.EscapeString(diffs[i].Text))
 		}
 	}
-	return template.HTML(result)
+
+	return template.HTML(buf.Bytes())
 }
 
 // get an specific line by type (add or del) and file line number
@@ -89,11 +100,11 @@ func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLin
 		}
 
 		if lineType == DIFF_LINE_DEL {
-			if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx - difference {
+			if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
 				return diffLine
 			}
 		} else if lineType == DIFF_LINE_ADD {
-			if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx + difference {
+			if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
 				return diffLine
 			}
 		}
@@ -107,11 +118,9 @@ func (diffSection *DiffSection) ComputeLinesDiff() {
 		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 {
+			diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:]))
 			continue
 		}
 
@@ -136,7 +145,7 @@ func (diffSection *DiffSection) ComputeLinesDiff() {
 		diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
 		diffRecord = dmp.DiffCleanupSemantic(diffRecord)
 
-		diffLine.ParsedContent = diffToHtml(diffRecord, diffLine.Type)
+		diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type)
 	}
 }
 
diff --git a/public/css/gogs.css b/public/css/gogs.css
index fd9359af14..0a7daaf008 100755
--- a/public/css/gogs.css
+++ b/public/css/gogs.css
@@ -2306,9 +2306,10 @@ footer .container .links > *:first-child {
 }
 .repository .diff-box .count {
   margin-right: 12px;
+  font-size: 13px;
 }
 .repository .diff-box .count .bar {
-  background-color: #e75316;
+  background-color: #bd2c00 ;
   height: 12px;
   width: 40px;
   display: inline-block;
@@ -2316,7 +2317,7 @@ footer .container .links > *:first-child {
   vertical-align: text-top;
 }
 .repository .diff-box .count .bar .add {
-  background-color: #77c64a;
+  background-color: #55a532;
   height: 12px;
 }
 .repository .diff-box .file {
@@ -2387,6 +2388,12 @@ footer .container .links > *:first-child {
 .repository .diff-file-box .code-diff tbody tr.add-code td.halfwidth {
   width: 50%;
 }
+.repository .diff-file-box .code-diff tbody tr .removed-code {
+  background-color: #ff9999;
+}
+.repository .diff-file-box .code-diff tbody tr .added-code {
+  background-color: #99ff99;
+}
 .repository .diff-file-box.file-content img {
   max-width: 100%;
   padding: 5px 5px 0 5px;
@@ -2764,12 +2771,6 @@ 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 7bb9c8a94e..124b2b29d0 100644
--- a/public/less/_repository.less
+++ b/public/less/_repository.less
@@ -641,15 +641,17 @@
 	.diff-box {
 		.count {
 			margin-right: 12px;
+			font-size: 13px;
+
 			.bar {
-				background-color: #e75316;
+				background-color: #bd2c00 ;
 				height: 12px;
 				width: 40px;
 				display: inline-block;
 				margin: 2px 4px 0 4px;
 				vertical-align: text-top;
 				.add {
-					background-color: #77c64a;
+					background-color: #55a532;
 					height: 12px;
 				}
 			}
@@ -716,6 +718,7 @@
 					// 	}
 					// }
 					&.del-code {
+						// Duplicate here to enforce add code color.
 						td.add-code {
 							background-color: #eaffea !important;
 							border-color: #c1e9c1 !important;
@@ -749,6 +752,13 @@
 						// 	background-color: #ffffdd !important;
 						// }
 					}
+
+					.removed-code {
+					  background-color: #ff9999;
+					}
+					.added-code {
+					  background-color: #99ff99;
+					}
 				}
 			}
 		}
@@ -1218,10 +1228,4 @@
 	}
 }
 
-.removed-code {
-  background-color: #ff9999;
-}
 
-.added-code {
-  background-color: #99ff99;
-}
diff --git a/templates/.VERSION b/templates/.VERSION
index 35fe4c73bf..861f2d9a71 100644
--- a/templates/.VERSION
+++ b/templates/.VERSION
@@ -1 +1 @@
-0.8.17.0109
\ No newline at end of file
+0.8.18.0109
\ No newline at end of file