|
|
@ -8,18 +8,18 @@ import ( |
|
|
|
"bufio" |
|
|
|
"bufio" |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"html" |
|
|
|
|
|
|
|
"html/template" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"os/exec" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"html/template" |
|
|
|
|
|
|
|
"html" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Unknwon/com" |
|
|
|
"github.com/Unknwon/com" |
|
|
|
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch" |
|
|
|
"golang.org/x/net/html/charset" |
|
|
|
"golang.org/x/net/html/charset" |
|
|
|
"golang.org/x/text/transform" |
|
|
|
"golang.org/x/text/transform" |
|
|
|
"github.com/sergi/go-diff/diffmatchpatch" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogits/git-module" |
|
|
|
"github.com/gogits/git-module" |
|
|
|
|
|
|
|
|
|
|
@ -63,18 +63,29 @@ type DiffSection struct { |
|
|
|
Lines []*DiffLine |
|
|
|
Lines []*DiffLine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { |
|
|
|
var ( |
|
|
|
result := "" |
|
|
|
addedCodePrefix = []byte("<span class=\"added-code\">") |
|
|
|
for _, s := range diffRecord { |
|
|
|
removedCodePrefix = []byte("<span class=\"removed-code\">") |
|
|
|
if s.Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD { |
|
|
|
codeTagSuffix = []byte("</span>") |
|
|
|
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>" |
|
|
|
func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { |
|
|
|
} else if s.Type == diffmatchpatch.DiffEqual { |
|
|
|
var buf bytes.Buffer |
|
|
|
result = result + html.EscapeString(s.Text) |
|
|
|
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
|
|
|
|
// 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 lineType == DIFF_LINE_DEL { |
|
|
|
if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx - difference { |
|
|
|
if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference { |
|
|
|
return diffLine |
|
|
|
return diffLine |
|
|
|
} |
|
|
|
} |
|
|
|
} else if lineType == DIFF_LINE_ADD { |
|
|
|
} else if lineType == DIFF_LINE_ADD { |
|
|
|
if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx + difference { |
|
|
|
if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference { |
|
|
|
return diffLine |
|
|
|
return diffLine |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -107,11 +118,9 @@ func (diffSection *DiffSection) ComputeLinesDiff() { |
|
|
|
var compareDiffLine *DiffLine |
|
|
|
var compareDiffLine *DiffLine |
|
|
|
var diff1, diff2 string |
|
|
|
var diff1, diff2 string |
|
|
|
|
|
|
|
|
|
|
|
// default content: as is
|
|
|
|
|
|
|
|
diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// just compute diff for adds and removes
|
|
|
|
// just compute diff for adds and removes
|
|
|
|
if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL { |
|
|
|
if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL { |
|
|
|
|
|
|
|
diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:])) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -136,7 +145,7 @@ func (diffSection *DiffSection) ComputeLinesDiff() { |
|
|
|
diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true) |
|
|
|
diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true) |
|
|
|
diffRecord = dmp.DiffCleanupSemantic(diffRecord) |
|
|
|
diffRecord = dmp.DiffCleanupSemantic(diffRecord) |
|
|
|
|
|
|
|
|
|
|
|
diffLine.ParsedContent = diffToHtml(diffRecord, diffLine.Type) |
|
|
|
diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|