问题
I made a git hook that checks my code style before commit. It passes the staged files to CodeSniffer. I use the PSR-2 code style which means newlines should be \n
even on Windows. However even after changing PhpStorm settings and git settings it still gives me the error that the newlines are \r\n
. Why does this happen?
PhpStorm
Searching for \r\n
with regex on does not return instances, so I believe the problem to lie with git.
.gitconfig
[core]
autocrlf = false
editor = \"C:/Program Files (x86)/GitExtensions/GitExtensions.exe\" fileeditor
eol = lf
[user]
name = Thomas Moors
email = thomas.moors@*****.nl
[merge]
tool = kdiff3
[mergetool "kdiff3"]
path = C:/Program Files (x86)/KDiff3/kdiff3.exe
[diff]
guitool = kdiff3
[difftool "kdiff3"]
path = C:/Program Files (x86)/KDiff3/kdiff3.exe
The error
Transcription:
FILE: ...\Thomas\Documents\example-live\laravel\app\Models\DoMdokUser.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
1 | ERROR | [x] End of line character is invalid; expected "\n" but
| | found "\r\n"
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 105ms; Memory: 4Mb
Fix the error before commit please
edit: using another editor (n++) the problem does seem to lie with phpstorm. Replacing \r\n fixes the problem. So why does PHPStorm not work properly?
回答1:
Searching for
\r\n
with regex on does not return instances, so I believe the problem to lie with git.
Not necessarily with git.
IDE (PhpStorm) stores all lines in memory with normalized line separator (which is \n
/ LF
) and when you saving the file it replaces them with actual line separator symbol detected on file opening. This also means that if you happen to have mixed line endings in one file (e.g. both CRLF
and LF
) .. after save it will use only one style (e.g. only LF
).
Normalized line ending allows you to execute your regex searches/replaces in a bit more simpler way -- no need to worry about what the actual symbol is.
Now -- the Code Style
settings page -- as you can see in that hint below the field, it says: "Applied to new files". This means that this setting does NOT affect existing files in any way.
To change line ending for a particular existing file: open the file and either change it via appropriate section in Status Bar .. or via File | Line Separators
So why does PhpStorm not work properly?
It works properly -- you just happen to not know how to change the line separator style.
来源:https://stackoverflow.com/questions/48422694/why-are-my-line-endings-still-wrong-according-to-codesniffer