forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix kubernetes#973
- Loading branch information
Showing
16 changed files
with
4,939 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package diff | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
"github.com/golang/glog" | ||
"github.com/sergi/go-diff/diffmatchpatch" | ||
) | ||
|
||
func FormatDiff(lString, rString string) string { | ||
results := buildDiffLines(lString, rString) | ||
|
||
return renderText(results, 2) | ||
} | ||
|
||
func renderText(results []lineRecord, context int) string { | ||
keep := make([]bool, len(results)) | ||
for i := range results { | ||
if results[i].Type == diffmatchpatch.DiffEqual { | ||
continue | ||
} | ||
for j := i - context; j <= i+context; j++ { | ||
if j >= 0 && j < len(keep) { | ||
keep[j] = true | ||
} | ||
} | ||
} | ||
|
||
var b bytes.Buffer | ||
wroteSkip := false | ||
for i := range results { | ||
if !keep[i] { | ||
if !wroteSkip { | ||
b.WriteString("...\n") | ||
wroteSkip = true | ||
} | ||
continue | ||
} | ||
|
||
switch results[i].Type { | ||
case diffmatchpatch.DiffDelete: | ||
b.WriteString("- ") | ||
case diffmatchpatch.DiffInsert: | ||
b.WriteString("+ ") | ||
case diffmatchpatch.DiffEqual: | ||
b.WriteString(" ") | ||
} | ||
b.WriteString(results[i].Line) | ||
b.WriteString("\n") | ||
wroteSkip = false | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
type lineRecord struct { | ||
Type diffmatchpatch.Operation | ||
Line string | ||
} | ||
|
||
func buildDiffLines(lString, rString string) []lineRecord { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(lString, rString, false) | ||
// No need to cleanup, we're going to do a line based diff | ||
//diffs = dmp.DiffCleanupSemantic(diffs) | ||
|
||
l := "" | ||
r := "" | ||
|
||
var results []lineRecord | ||
for _, diff := range diffs { | ||
lines := strings.Split(diff.Text, "\n") | ||
switch diff.Type { | ||
case diffmatchpatch.DiffInsert: | ||
if len(lines) > 0 { | ||
r += lines[0] | ||
if len(lines) > 1 { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r}) | ||
r = "" | ||
} | ||
} | ||
for i := 1; i < len(lines)-1; i++ { | ||
line := lines[i] | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: line}) | ||
} | ||
if len(lines) > 1 { | ||
r = lines[len(lines)-1] | ||
} | ||
|
||
case diffmatchpatch.DiffDelete: | ||
if len(lines) > 0 { | ||
l += lines[0] | ||
if len(lines) > 1 { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l}) | ||
l = "" | ||
} | ||
} | ||
for i := 1; i < len(lines)-1; i++ { | ||
line := lines[i] | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: line}) | ||
} | ||
if len(lines) > 1 { | ||
l = lines[len(lines)-1] | ||
} | ||
|
||
case diffmatchpatch.DiffEqual: | ||
if len(lines) > 0 { | ||
if l != "" || r != "" { | ||
l += lines[0] | ||
r += lines[0] | ||
} else { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: lines[0]}) | ||
} | ||
if len(lines) > 1 { | ||
if r != "" { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r}) | ||
r = "" | ||
} | ||
|
||
if l != "" { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l}) | ||
l = "" | ||
} | ||
} | ||
} | ||
for i := 1; i < len(lines)-1; i++ { | ||
line := lines[i] | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: line}) | ||
} | ||
if len(lines) > 1 { | ||
l = lines[len(lines)-1] | ||
r = lines[len(lines)-1] | ||
} | ||
|
||
default: | ||
glog.Fatalf("unexpected dmp type: %v", diff.Type) | ||
} | ||
} | ||
|
||
if l != "" && r != "" { | ||
if l == r { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l}) | ||
} else { | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l}) | ||
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r}) | ||
} | ||
} | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package diff | ||
|
||
import ( | ||
"github.com/sergi/go-diff/diffmatchpatch" | ||
"testing" | ||
) | ||
|
||
func Test_Diff_1(t *testing.T) { | ||
l := `A | ||
B | ||
C | ||
D | ||
E | ||
F` | ||
r := `A | ||
D | ||
E | ||
F` | ||
expectedDiff := ` A | ||
- B | ||
- C | ||
D | ||
E | ||
... | ||
` | ||
|
||
{ | ||
dl := buildDiffLines(l, r) | ||
actual := "" | ||
for i := range dl { | ||
switch dl[i].Type { | ||
case diffmatchpatch.DiffDelete: | ||
actual += "-" | ||
case diffmatchpatch.DiffInsert: | ||
actual += "+" | ||
case diffmatchpatch.DiffEqual: | ||
actual += "=" | ||
default: | ||
t.Errorf("Unexpected diff type: %v", dl[i].Type) | ||
} | ||
} | ||
expected := "=--===" | ||
if actual != expected { | ||
t.Fatalf("unexpected diff. expected=%v, actual=%v", expected, actual) | ||
} | ||
} | ||
|
||
actual := FormatDiff(l, r) | ||
if actual != expectedDiff { | ||
t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual) | ||
} | ||
} | ||
|
||
func Test_Diff_2(t *testing.T) { | ||
l := `A | ||
B | ||
C | ||
D | ||
E | ||
F` | ||
r := `A | ||
B | ||
C | ||
D | ||
E2 | ||
F` | ||
expectedDiff := `... | ||
C | ||
D | ||
+ E2 | ||
- E | ||
F | ||
` | ||
actual := FormatDiff(l, r) | ||
if actual != expectedDiff { | ||
t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual) | ||
} | ||
} | ||
|
||
func Test_Diff_3(t *testing.T) { | ||
l := `A | ||
B | ||
C | ||
D | ||
E | ||
F` | ||
r := `A | ||
B | ||
C | ||
D | ||
E | ||
F2` | ||
expectedDiff := `... | ||
D | ||
E | ||
- F | ||
+ F2 | ||
` | ||
actual := FormatDiff(l, r) | ||
if actual != expectedDiff { | ||
t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.