Skip to content

Commit 434d3f1

Browse files
committed
cmd/goimports: ignore line breaks when comparing
1 parent 06a498a commit 434d3f1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cmd/goimports/goimports.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
143143
return err
144144
}
145145

146-
if !bytes.Equal(src, res) {
146+
if !compareByLine(src, res) {
147147
// formatting has changed
148148
if *list {
149149
fmt.Fprintln(out, filename)
@@ -183,6 +183,26 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
183183
return err
184184
}
185185

186+
func compareByLine(src, dst []byte) bool {
187+
src = bytes.Replace(src, []byte("\r\n"), []byte("\n"), -1)
188+
// dst is already normalized by imports.Process
189+
190+
linesA := bytes.Split(src, []byte("\n"))
191+
linesB := bytes.Split(dst, []byte("\n"))
192+
193+
if len(linesA) != len(linesB) {
194+
return false
195+
}
196+
197+
for i := range linesA {
198+
if !bytes.Equal(linesA[i], linesB[i]) {
199+
return false
200+
}
201+
}
202+
203+
return true
204+
}
205+
186206
func visitFile(path string, f os.FileInfo, err error) error {
187207
if err == nil && isGoFile(f) {
188208
err = processFile(path, nil, os.Stdout, multipleArg)

0 commit comments

Comments
 (0)