-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
diff.go
195 lines (161 loc) · 5.02 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitutil
import (
"bytes"
"fmt"
"html"
"html/template"
"io"
"sync"
"github.com/sergi/go-diff/diffmatchpatch"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
"github.com/gogs/git-module"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/template/highlight"
"gogs.io/gogs/internal/tool"
)
// DiffSection is a wrapper to git.DiffSection with helper methods.
type DiffSection struct {
*git.DiffSection
initOnce sync.Once
dmp *diffmatchpatch.DiffMatchPatch
}
// ComputedInlineDiffFor computes inline diff for the given line.
func (s *DiffSection) ComputedInlineDiffFor(line *git.DiffLine) template.HTML {
fallback := template.HTML(html.EscapeString(line.Content))
if conf.Git.DisableDiffHighlight {
return fallback
}
// Find equivalent diff line, ignore when not found.
var diff1, diff2 string
switch line.Type {
case git.DiffLineAdd:
compareLine := s.Line(git.DiffLineDelete, line.RightLine)
if compareLine == nil {
return fallback
}
diff1 = compareLine.Content
diff2 = line.Content
case git.DiffLineDelete:
compareLine := s.Line(git.DiffLineAdd, line.LeftLine)
if compareLine == nil {
return fallback
}
diff1 = line.Content
diff2 = compareLine.Content
default:
return fallback
}
s.initOnce.Do(func() {
s.dmp = diffmatchpatch.New()
s.dmp.DiffEditCost = 100
})
diffs := s.dmp.DiffMain(diff1[1:], diff2[1:], true)
diffs = s.dmp.DiffCleanupEfficiency(diffs)
return diffsToHTML(diffs, line.Type)
}
func diffsToHTML(diffs []diffmatchpatch.Diff, lineType git.DiffLineType) template.HTML {
buf := bytes.NewBuffer(nil)
// Reproduce signs which are cutted for inline diff before.
switch lineType {
case git.DiffLineAdd:
buf.WriteByte('+')
case git.DiffLineDelete:
buf.WriteByte('-')
}
const (
addedCodePrefix = `<span class="added-code">`
removedCodePrefix = `<span class="removed-code">`
codeTagSuffix = `</span>`
)
for i := range diffs {
switch {
case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == git.DiffLineAdd:
buf.WriteString(addedCodePrefix)
buf.WriteString(html.EscapeString(diffs[i].Text))
buf.WriteString(codeTagSuffix)
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == git.DiffLineDelete:
buf.WriteString(removedCodePrefix)
buf.WriteString(html.EscapeString(diffs[i].Text))
buf.WriteString(codeTagSuffix)
case diffs[i].Type == diffmatchpatch.DiffEqual:
buf.WriteString(html.EscapeString(diffs[i].Text))
}
}
return template.HTML(buf.Bytes())
}
// DiffFile is a wrapper to git.DiffFile with helper methods.
type DiffFile struct {
*git.DiffFile
Sections []*DiffSection
}
// HighlightClass returns the detected highlight class for the file.
func (diffFile *DiffFile) HighlightClass() string {
return highlight.FileNameToHighlightClass(diffFile.Name)
}
// Diff is a wrapper to git.Diff with helper methods.
type Diff struct {
*git.Diff
Files []*DiffFile
}
// NewDiff returns a new wrapper of given git.Diff.
func NewDiff(oldDiff *git.Diff) *Diff {
newDiff := &Diff{
Diff: oldDiff,
Files: make([]*DiffFile, oldDiff.NumFiles()),
}
// FIXME: detect encoding while parsing.
var buf bytes.Buffer
for i := range oldDiff.Files {
buf.Reset()
newDiff.Files[i] = &DiffFile{
DiffFile: oldDiff.Files[i],
Sections: make([]*DiffSection, oldDiff.Files[i].NumSections()),
}
for j := range oldDiff.Files[i].Sections {
newDiff.Files[i].Sections[j] = &DiffSection{
DiffSection: oldDiff.Files[i].Sections[j],
}
for k := range newDiff.Files[i].Sections[j].Lines {
buf.WriteString(newDiff.Files[i].Sections[j].Lines[k].Content)
buf.WriteString("\n")
}
}
charsetLabel, err := tool.DetectEncoding(buf.Bytes())
if charsetLabel != "UTF-8" && err == nil {
encoding, _ := charset.Lookup(charsetLabel)
if encoding != nil {
d := encoding.NewDecoder()
for j := range newDiff.Files[i].Sections {
for k := range newDiff.Files[i].Sections[j].Lines {
if c, _, err := transform.String(d, newDiff.Files[i].Sections[j].Lines[k].Content); err == nil {
newDiff.Files[i].Sections[j].Lines[k].Content = c
}
}
}
}
}
}
return newDiff
}
// ParseDiff parses the diff from given io.Reader.
func ParseDiff(r io.Reader, maxFiles, maxFileLines, maxLineChars int) (*Diff, error) {
done := make(chan git.SteamParseDiffResult)
go git.StreamParseDiff(r, done, maxFiles, maxFileLines, maxLineChars)
result := <-done
if result.Err != nil {
return nil, fmt.Errorf("stream parse diff: %v", result.Err)
}
return NewDiff(result.Diff), nil
}
// RepoDiff parses the diff on given revisions of given repository.
func RepoDiff(repo *git.Repository, rev string, maxFiles, maxFileLines, maxLineChars int, opts ...git.DiffOptions) (*Diff, error) {
diff, err := repo.Diff(rev, maxFiles, maxFileLines, maxLineChars, opts...)
if err != nil {
return nil, fmt.Errorf("get diff: %v", err)
}
return NewDiff(diff), nil
}