Skip to content

Commit 17c7943

Browse files
committed
yamlfmt: implement our own diff, drop a dep
1 parent 775a12d commit 17c7943

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.12
44

55
require (
66
github.com/google/go-cmp v0.5.9
7-
github.com/kylelemons/godebug v1.1.0
87
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
98
sigs.k8s.io/randfill v1.0.0
109
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
22
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3-
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
4-
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
53
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
64
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
75
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=

yamlfmt/yamlfmt.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"io"
2424
"os"
2525
"path/filepath"
26+
"strings"
2627

27-
"github.com/kylelemons/godebug/diff"
2828
yaml "sigs.k8s.io/yaml/goyaml.v3"
2929
"sigs.k8s.io/yaml/kyaml"
3030
)
@@ -68,7 +68,7 @@ func main() {
6868
files := fs.Args()
6969

7070
if len(files) == 0 {
71-
if err := renderYAML(os.Stdin, *format, *diff, os.Stdout); err != nil {
71+
if err := renderYAML("<stdin>", os.Stdin, *format, *diff, os.Stdout); err != nil {
7272
fmt.Fprintln(os.Stderr, err)
7373
os.Exit(1)
7474
}
@@ -103,13 +103,13 @@ func main() {
103103
}
104104
out = tmp
105105
}
106-
if len(files) > 1 && !*write {
106+
if len(files) > 1 && !*write && !*diff {
107107
if i > 0 {
108108
fmt.Fprintln(out, "")
109109
}
110110
fmt.Fprintln(out, "# "+path)
111111
}
112-
if err := renderYAML(in, *format, *diff, out); err != nil {
112+
if err := renderYAML(path, in, *format, *diff, out); err != nil {
113113
fmt.Fprintln(os.Stderr, err)
114114
os.Exit(1)
115115
}
@@ -118,7 +118,7 @@ func main() {
118118
}
119119
}
120120

121-
func renderYAML(in io.Reader, format string, printDiff bool, out io.Writer) error {
121+
func renderYAML(path string, in io.Reader, format string, printDiff bool, out io.Writer) error {
122122
if format == fmtKYAML {
123123
ky := &kyaml.Encoder{}
124124

@@ -131,7 +131,7 @@ func renderYAML(in io.Reader, format string, printDiff bool, out io.Writer) erro
131131
if err := ky.FromYAML(bytes.NewReader(ibuf), &obuf); err != nil {
132132
return err
133133
}
134-
d := diff.Diff(string(ibuf), obuf.String())
134+
d := trivialDiff(path, string(ibuf), obuf.String())
135135
fmt.Fprint(out, d)
136136
return nil
137137
}
@@ -154,7 +154,7 @@ func renderYAML(in io.Reader, format string, printDiff bool, out io.Writer) erro
154154
decoder = yaml.NewDecoder(bytes.NewReader(ibuf))
155155
encoder = yaml.NewEncoder(&obuf)
156156
finish = func() {
157-
d := diff.Diff(string(ibuf), obuf.String())
157+
d := trivialDiff(path, string(ibuf), obuf.String())
158158
fmt.Fprint(out, d)
159159
}
160160
} else {
@@ -182,6 +182,52 @@ func renderYAML(in io.Reader, format string, printDiff bool, out io.Writer) erro
182182
return nil
183183
}
184184

185+
func trivialDiff(path, a, b string) string {
186+
if a == b {
187+
return ""
188+
}
189+
190+
x := strings.Split(strings.TrimSuffix(a, "\n"), "\n")
191+
y := strings.Split(strings.TrimSuffix(b, "\n"), "\n")
192+
buf := bytes.Buffer{}
193+
buf.WriteString(fmt.Sprintf("--- %s\n+++ %s\n", path, path))
194+
buf.WriteString(fmt.Sprintf("@@ -%d,%d +%d,%d\n", 0, len(x), 0, len(y)))
195+
for {
196+
n := 0
197+
for ; n < len(x) && n < len(y) && x[n] == y[n]; n++ {
198+
buf.WriteString(" " + x[n] + "\n")
199+
}
200+
x = x[n:]
201+
y = y[n:]
202+
203+
nextX, nextY := nextCommon(x, y)
204+
for i := 0; i < nextX; i++ {
205+
buf.WriteString("-" + x[i] + "\n")
206+
}
207+
x = x[nextX:]
208+
for j := 0; j < nextY; j++ {
209+
buf.WriteString("+" + y[j] + "\n")
210+
}
211+
y = y[nextY:]
212+
213+
if len(x) == 0 && len(y) == 0 {
214+
break
215+
}
216+
}
217+
return buf.String()
218+
}
219+
220+
func nextCommon(x, y []string) (int, int) {
221+
for i := 0; i < len(x); i++ {
222+
for j := 0; j < len(y); j++ {
223+
if x[i] == y[j] {
224+
return i, j
225+
}
226+
}
227+
}
228+
return len(x), len(y)
229+
}
230+
185231
func setStyle(node *yaml.Node, style yaml.Style) {
186232
node.Style = style
187233
for _, child := range node.Content {

0 commit comments

Comments
 (0)