@@ -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+
185231func setStyle (node * yaml.Node , style yaml.Style ) {
186232 node .Style = style
187233 for _ , child := range node .Content {
0 commit comments