Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/goimports: ignore line breaks when comparing #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cmd/goimports/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
return err
}

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

func compareByLine(src, dst []byte) bool {
src = bytes.Replace(src, []byte("\r\n"), []byte("\n"), -1)
// dst is already normalized by imports.Process

linesA := bytes.Split(src, []byte("\n"))
linesB := bytes.Split(dst, []byte("\n"))

if len(linesA) != len(linesB) {
return false
}

for i := range linesA {
if !bytes.Equal(linesA[i], linesB[i]) {
return false
}
}

return true
}

func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, nil, os.Stdout, multipleArg)
Expand Down