diff --git a/models/git_diff.go b/models/git_diff.go index 22075ef76b6..9656813bf8d 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 + ""+html.EscapeString(s.Text)+"" + } else if s.Type == diffmatchpatch.DiffDelete && lineType == DIFF_LINE_DEL { + result = result + ""+html.EscapeString(s.Text)+"" + } 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 00000000000..4f4d47bf9d7 --- /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 bar 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 bar 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 0160c5d91ab..fd9359af141 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 441832daf22..7bb9c8a94eb 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 c3fc4d177ac..9e46c2e8462 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 a12f4de4ec6..f7851822f09 100644 --- a/templates/repo/diff_box.tmpl +++ b/templates/repo/diff_box.tmpl @@ -76,13 +76,13 @@ {{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}
{{if $line.LeftIdx}}{{$line.Content}}{{end}}+
{{if $line.LeftIdx}}{{$line.ParsedContent}}{{end}}
{{if $line.RightIdx}}{{$line.Content}}{{end}}+
{{if $line.RightIdx}}{{$line.ParsedContent}}{{end}}
{{$line.Content}}+
{{$line.ParsedContent}}