aboutsummaryrefslogtreecommitdiff
path: root/models/git_diff.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/git_diff.go')
-rw-r--r--models/git_diff.go118
1 files changed, 108 insertions, 10 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index 22075ef7..1913ec46 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -8,6 +8,8 @@ import (
"bufio"
"bytes"
"fmt"
+ "html"
+ "html/template"
"io"
"io/ioutil"
"os"
@@ -15,6 +17,7 @@ import (
"strings"
"github.com/Unknwon/com"
+ "github.com/sergi/go-diff/diffmatchpatch"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
@@ -25,30 +28,34 @@ import (
"github.com/gogits/gogs/modules/process"
)
-// Diff line types.
+type DiffLineType uint8
+
const (
- DIFF_LINE_PLAIN = iota + 1
+ DIFF_LINE_PLAIN DiffLineType = iota + 1
DIFF_LINE_ADD
DIFF_LINE_DEL
DIFF_LINE_SECTION
)
+type DiffFileType uint8
+
const (
- DIFF_FILE_ADD = iota + 1
+ DIFF_FILE_ADD DiffFileType = iota + 1
DIFF_FILE_CHANGE
DIFF_FILE_DEL
DIFF_FILE_RENAME
)
type DiffLine struct {
- LeftIdx int
- RightIdx int
- Type int
- Content string
+ LeftIdx int
+ RightIdx int
+ Type DiffLineType
+ Content string
+ ParsedContent template.HTML
}
-func (d DiffLine) GetType() int {
- return d.Type
+func (d *DiffLine) GetType() int {
+ return int(d.Type)
}
type DiffSection struct {
@@ -56,12 +63,99 @@ type DiffSection struct {
Lines []*DiffLine
}
+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(buf.Bytes())
+}
+
+// get an specific line by type (add or del) and file line number
+func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
+ difference := 0
+
+ for _, diffLine := range diffSection.Lines {
+ if diffLine.Type == DIFF_LINE_PLAIN {
+ // get the difference of line numbers between ADD and DEL versions
+ difference = diffLine.RightIdx - diffLine.LeftIdx
+ continue
+ }
+
+ if lineType == DIFF_LINE_DEL {
+ if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
+ return diffLine
+ }
+ } else if lineType == DIFF_LINE_ADD {
+ if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
+ return diffLine
+ }
+ }
+ }
+ return nil
+}
+
+// computes diff of each diff line and set the HTML on diffLine.ParsedContent
+func (diffSection *DiffSection) ComputeLinesDiff() {
+ for _, diffLine := range diffSection.Lines {
+ var compareDiffLine *DiffLine
+ var diff1, diff2 string
+
+ 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.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
+ if compareDiffLine == nil {
+ continue
+ }
+ diff1 = compareDiffLine.Content
+ diff2 = diffLine.Content
+ } else {
+ compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
+ 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
Index int
Addition, Deletion int
- Type int
+ Type DiffFileType
IsCreated bool
IsDeleted bool
IsBin bool
@@ -69,6 +163,10 @@ type DiffFile struct {
Sections []*DiffSection
}
+func (diffFile *DiffFile) GetType() int {
+ return int(diffFile.Type)
+}
+
type Diff struct {
TotalAddition, TotalDeletion int
Files []*DiffFile