-
Notifications
You must be signed in to change notification settings - Fork 1
/
filediff.go
328 lines (280 loc) · 9.36 KB
/
filediff.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"github.com/gookit/color"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/udhos/equalfile"
)
// ErrorCanceled is returned when the user decideds to quit.
var ErrorCanceled = fmt.Errorf("canceled by user")
// compareFiles is the entry point for file comparison, diff reviews and apply patches
// TBD: Currently the match result is returned, not sure if we need this or not.
func compareFiles(fileAExt fileInfoExtended, fileBExt fileInfoExtended, dryRun bool, reportOnly bool) (bool, error) {
cmp := equalfile.New(nil, equalfile.Options{}) // compare using single mode
equal, err := cmp.CompareFile(fileAExt.osPathname, fileBExt.osPathname)
if err != nil {
logError("Comparing files failed", err)
return false, err
}
if reportOnly && !equal {
runtimeStats.FilesWDiff++
fmt.Printf("Files %s and %s differ\n", fileAExt.osPathname, fileBExt.osPathname)
return equal, nil
}
if equal {
// Files are the same
return equal, nil
}
runtimeStats.FilesWDiff++
loadFileContent(&fileAExt)
loadFileContent(&fileBExt)
resultDiffInfo, err := createDiffs(fileAExt, fileBExt)
if err != nil {
return equal, err
}
runtimeStats.PatchesApplied += resultDiffInfo.patchesApplied
runtimeStats.PatchesErrored += resultDiffInfo.patchesFailed
runtimeStats.PatchesSkipped += (resultDiffInfo.patchesTotal - resultDiffInfo.patchesApplied)
if resultDiffInfo.patchesFailed > 0 {
return equal, fmt.Errorf("while patching file, skip file writes: %s", fileAExt.osPathname)
}
if dryRun {
fmt.Printf("Dry-run enabled, skipping file writes: %s\n", fileAExt.osPathname)
return equal, nil
}
// dryrun is off and we have patched the file
if !dryRun && resultDiffInfo.patched {
err := ioutil.WriteFile(fileAExt.osPathname, resultDiffInfo.newContent, 0644)
return equal, err
}
return equal, nil
}
// The files have already be read by a quick compare utility
// If we get an I/O error here we should just exit.
func loadFileContent(fileX *fileInfoExtended) {
var err error
fileX.fileContent, err = ioutil.ReadFile(fileX.osPathname)
if err != nil {
log.Fatalf("Error reading file: %v, %v", fileX.osPathname, err)
}
fileX.fileContentString = string(fileX.fileContent)
}
func splitLines(s string) []string {
var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
// ColorDiff Returns the diff to the end user with colour
func ColorDiff(diffs []diffmatchpatch.Diff) string {
r := regexp.MustCompile(`\s+(\r?\n)`)
rEnd := regexp.MustCompile(`\s+$`)
out := ""
for i, diff := range diffs {
if i == 0 {
out += color.Style{color.Blue}.Sprint("---\n")
}
switch diff.Type {
case diffmatchpatch.DiffInsert:
// out += color.Style{color.Green}.Sprint(strings.ReplaceAll(diff.Text, "\t", "˲ "))
if diff.Text == r.ReplaceAllString(diff.Text, "$1") {
out += color.Style{color.Green}.Sprint(strings.ReplaceAll(diff.Text, "\t", "˲ "))
} else {
lines := splitLines(diff.Text)
for i, line := range lines {
noSpaceLine := rEnd.ReplaceAllString(line, "")
out += color.Style{color.Green}.Sprint(strings.ReplaceAll(noSpaceLine, "\t", "˲ "))
out += color.Style{color.BgGreen}.Sprint(strings.Replace(line, noSpaceLine, "", 1))
if i+1 != len(lines) {
out += color.ClearCode("\n")
}
}
if strings.HasSuffix(diff.Text, "\n") {
out += color.ClearCode("\n")
}
}
case diffmatchpatch.DiffDelete:
// out += color.Style{color.Red}.Sprint(strings.ReplaceAll(diff.Text, "\t", "˲ "))
if diff.Text == r.ReplaceAllString(diff.Text, "$1") {
out += color.Style{color.Red}.Sprint(strings.ReplaceAll(diff.Text, "\t", "˲ "))
} else {
lines := splitLines(diff.Text)
for i, line := range lines {
noSpaceLine := rEnd.ReplaceAllString(line, "")
out += color.Style{color.Red}.Sprint(strings.ReplaceAll(noSpaceLine, "\t", "˲ "))
out += color.Style{color.BgRed}.Sprint(strings.Replace(line, noSpaceLine, "", 1))
if i+1 != len(lines) {
out += color.ClearCode("\n")
}
}
if strings.HasSuffix(diff.Text, "\n") {
out += color.ClearCode("\n")
}
}
case diffmatchpatch.DiffEqual:
lines := splitLines(diff.Text)
if len(lines) > 0 &&
// Avoid just adding the beginning of the file as context if it is not close to the diff
!(i == 0 && len(lines) > 2) {
// TODO: Control the output of tab with an option
out += color.Style{color.White}.Sprint(strings.ReplaceAll(lines[0], "\t", "˲ "))
if len(lines) > 1 {
out += "\n"
}
if len(lines) > 2 {
out += color.Style{color.Blue}.Sprint("\n---\n")
}
}
if len(lines) > 1 &&
// Avoid just adding the end of the file as context if it is not close to the diff
!(i+1 == len(diffs) && len(lines) > 2) {
// TODO: Control the output of tab with an option
out += color.Style{color.White}.Sprint(strings.ReplaceAll(lines[len(lines)-1], "\t", "˲ "))
if strings.HasSuffix(diff.Text, "\n") {
out += "\n"
}
}
}
}
return out
}
// Get a list of Patches / Chunks
func createDiffs(fileAExt fileInfoExtended, fileBExt fileInfoExtended) (fileDiffInfo, error) {
fileDiffInfo := fileDiffInfo{}
dmp := diffmatchpatch.New()
dmp.MatchMaxBits = 100
// create the diffs between files
fileAdmp, fileBdmp, dmpStrings := dmp.DiffLinesToChars(fileAExt.fileContentString, fileBExt.fileContentString)
diffs := dmp.DiffMain(fileAdmp, fileBdmp, false)
diffs = dmp.DiffCharsToLines(diffs, dmpStrings)
diffs = dmp.DiffCleanupSemantic(diffs)
fileDiffInfo.diffCount = len(diffs)
//review the diff with the user
lookAtPatches, err := reviewDiff(ColorDiff(diffs), fileAExt.osPathname, fileBExt.osPathname, fileAExt.autoPatch)
if err != nil {
return fileDiffInfo, err
}
if !lookAtPatches {
return fileDiffInfo, nil
}
fileContent, patchesTotal, patchesFailed, err := handlePatches(dmp, diffs, fileAExt)
patchesApplied := patchesTotal - patchesFailed
fileDiffInfo.patchesTotal = patchesTotal
fileDiffInfo.patchesApplied = patchesApplied
fileDiffInfo.patchesFailed = patchesFailed
if patchesApplied > 0 {
fileDiffInfo.patched = true
fileDiffInfo.newContent = fileContent
}
logError("Error applying patching", err)
fmt.Printf("\nDiffs: %v, Patches: %v, Applied: %v, Failed: %v\n", len(diffs), patchesTotal, patchesApplied, patchesFailed)
return fileDiffInfo, nil
}
func reviewDiff(mydiffString string, fileAName string, fileBName string, autoPatch bool) (bool, error) {
color.Style{color.OpBold}.Printf("Appling diff to: %s, from: %s\n", fileAName, fileBName)
fmt.Println(mydiffString)
response := false
if autoPatch {
fmt.Print("Review patches and apply them [y,n,q]? AutoAppling")
response = true
} else {
color.Style{color.Blue, color.OpBold}.Print("Review patches and apply them [y,n,q]? ")
rsp, err := askForConfirmation()
if err != nil {
if errors.Is(err, ErrorCanceled) {
return rsp, err
}
}
response = rsp
}
return response, nil
}
func reviewPatchDetailed(patchString string, fileAName string, autoPatch bool) (bool, error) {
color.Style{color.OpBold}.Printf("Appling diff to: %s\n", fileAName)
fmt.Println(patchString)
response := false
if autoPatch {
fmt.Print("Apply patch [y,n,q]? AutoAppling")
response = true
} else {
color.Style{color.Blue, color.OpBold}.Print("Apply patch [y,n,q]? ")
rsp, err := askForConfirmation()
if err != nil {
if errors.Is(err, ErrorCanceled) {
return rsp, err
}
}
response = rsp
}
return response, nil
}
func handlePatches(dmp *diffmatchpatch.DiffMatchPatch, diffs []diffmatchpatch.Diff, fileAExt fileInfoExtended) ([]byte, int, int, error) {
myPatches := dmp.PatchMake(diffs)
applyPatchList, err := stagePatches(myPatches, fileAExt.osPathname, fileAExt.autoPatch)
if err != nil {
fmt.Println(err)
return nil, 0, 0, err
}
fileAtextnew, patchResults := dmp.PatchApply(applyPatchList, fileAExt.fileContentString)
patchesTotal := 0
patchesFailed := 0
for _, patchResult := range patchResults {
patchesTotal++
if !patchResult {
patchesFailed++
}
}
fileContent := []byte(fileAtextnew)
return fileContent, patchesTotal, patchesFailed, err
}
// Cycles through the patches and returns the patches the User has flagged to be applied.
func stagePatches(myPatches []diffmatchpatch.Patch, fileAName string, autoPatch bool) ([]diffmatchpatch.Patch, error) {
applyPatchList := []diffmatchpatch.Patch{}
for _, patch := range myPatches {
addChunk, err := reviewPatchDetailed(patch.StringByLine(), fileAName, autoPatch)
if err != nil {
logError("Error reviewing patch", err)
return applyPatchList, err
}
if addChunk {
applyPatchList = append(applyPatchList, patch)
}
}
return applyPatchList, nil
}
// Helper method to ask for confirmation from a User
func askForConfirmation() (bool, error) {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
if err.Error() == "unexpected newline" {
response = ""
} else {
logError("Error during confirmation", err)
// TODO: Should this still be nil?
return false, nil
}
}
switch strings.ToLower(response) {
case "y", "yes":
return true, nil
case "n", "no":
return false, nil
case "q", "quit":
return false, ErrorCanceled
default:
fmt.Print(`y - patch this hunk
n - do not patch this hunk
q - quit; do not patch this hunk or any of the remaining ones
`)
return askForConfirmation()
}
}