1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-26 13:29:12 -05:00

Highlighting differences of lines in the diff view.

This commit is contained in:
Andrey Nering 2016-01-03 19:26:46 -02:00
parent 0cb7396840
commit 73474c043b
6 changed files with 137 additions and 3 deletions

View file

@ -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

29
models/git_diff_test.go Normal file
View file

@ -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))
}

View file

@ -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;

View file

@ -1217,3 +1217,11 @@
width: 100%!important;
}
}
.removed-code {
background-color: #ff9999;
}
.added-code {
background-color: #99ff99;
}

View file

@ -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

View file

@ -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}}