git:EOF 处的新空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27059239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:34:47  来源:igfitidea点击:

git: new blank line at EOF

macosgitvimnewlineeof

提问by Maxim Chetrusca

So I run git diff --checkbefore add-ing files and commit-ing them, and on two specific files I get path/filename:linenumber: new blank line at EOF. If I delete the last empty line in those files, I don't get any messages, but I think it is good style to end my files with a newline. Strangely enough, the other files which I think have exactly the same endings, don't give any messages. I am new to git, using git 2.0.1 on OS X Yosemite. I use vim as my editor.

所以我git diff --checkadd-ing 文件和commit-ing 它们之前运行,并且在两个特定文件上我得到path/filename:linenumber: new blank line at EOF. 如果我删除这些文件中的最后一个空行,我不会收到任何消息,但我认为用换行符结束我的文件是一种很好的方式。奇怪的是,我认为具有完全相同结尾的其他文件没有给出任何消息。我是 git 新手,在 OS X Yosemite 上使用 git 2.0.1。我使用 vim 作为我的编辑器。

How can I have my files end with newline while avoiding this message? Should I ignore it?

如何在避免此消息的同时让我的文件以换行符结尾?我应该忽略它吗?

回答by romainl

There are two issues, here.

这里有两个问题。

  • you are confused about "newline" vs "new line":

    A "new line" is an actual empty line (contains only a "newline" character) and a "newline" is a special character used to mark the end of the current line.

    Most compilers, interpreters and Unix tools expect your text files to end with a newlineto avoid ambiguity when dealing with multiple files, not a "new line".

    Most editors, Vim included, add that necessary character at the end of every line, last line included, so there's nothing to do on your side to ensure that requirement is met.

    Especially not adding a "new line" at the EOF.

  • you are probably used to bad behavior:

    The "newline" character is traditionally interpreted as a "line terminator" by Unix tools, most compilers and Vim. This means that whatever comes after that character is considered to be on another line. Since that character is the last one in the file, there's no reason to show a line that is not there to the user.

    Alas, most GUI editors interpret it as a "line separator", meaning that it marks the separation between two lines and those editors usually show a non-existing line at the end of your file to satisfy that interpretation.

    I'm probably assuming too much, but it looks like you are used to that wrong behavior and try to mimic it by adding a totally unnecessary "new line" at the end of your files.

  • 您对“换行”与“换行”感到困惑:

    “换行”是实际的空行(仅包含“换行”字符),“换行”是用于标记当前行结束的特殊字符。

    大多数编译器、解释器和 Unix 工具都希望您的文本文件以换行符结尾,以避免在处理多个文件时产生歧义,而不是“换行”。

    大多数编辑器,包括 Vim,都会在每一行的末尾添加必要的字符,包括最后一行,因此您无需采取任何措施来确保满足要求。

    特别是不要在 EOF 处添加“新行”。

  • 你可能已经习惯了不良行为:

    “换行符”传统上被 Unix 工具、大多数编译器和 Vim 解释为“行终止符”。这意味着该字符之后的任何内容都被视为在另一行上。由于该字符是文件中的最后一个字符,因此没有理由向用户显示不存在的行。

    唉,大多数 GUI 编辑器将其解释为“行分隔符”,这意味着它标志着两行之间的分隔,而这些编辑器通常会在文件末尾显示一个不存在的行以满足该解释。

    我可能假设太多了,但看起来您已经习惯了这种错误的行为,并试图通过在文件末尾添加一个完全不必要的“新行”来模仿它。

Either you keep adding "new lines" at the bottom of your source files, consider that as some sort of formatting and coding guideline and stop considering those Git messages as error messages.

要么继续在源文件底部添加“新行”,将其视为某种格式和编码指南,并停止将这些 Git 消息视为错误消息。

Or you stop adding those useless "new lines".

或者你停止添加那些无用的“新行”。

I would go with the second option.

我会选择第二个选项。

回答by Sascha Wolf

From the git diffdocumentation:

git diff文档

--check

Warn if changes introduce whitespace errors. What are considered whitespace errors is controlled by core.whitespaceconfiguration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors. Exits with non-zero status if problems are found. Not compatible with --exit-code.

- 查看

如果更改引入空格错误,则发出警告。什么被认为是空白错误是由core.whitespace配置控制的。默认情况下,尾随空格(包括仅由空格组成的行)和在行的初始缩进内紧跟一个制表符的空格字符被视为空格错误。如果发现问题,则以非零状态退出。与 --exit-code 不兼容。

Correspondingly, the git configdocumentation:

相应地,git config文档

core.whitespace

A comma separated list of common whitespace problems to notice. git diff will use color.diff.whitespaceto highlight them, and git apply --whitespace=errorwill consider them as errors. You can prefix -to disable any of them (e.g. -trailing-space):

  • blank-at-eoltreats trailing whitespaces at the end of the line as an error (enabled by default).

  • space-before-tabtreats a space character that appears immediately before a tab character in the initial indent part of the line as an error (enabled by default).

  • indent-with-non-tabtreats a line that is indented with space characters instead of the equivalent tabs as an error (not enabled by default).

  • tab-in-indenttreats a tab character in the initial indent part of the line as an error (not enabled by default).

  • blank-at-eoftreats blank lines added at the end of file as an error (enabled by default).

  • trailing-spaceis a short-hand to cover both blank-at-eoland blank-at-eof.

  • cr-at-eoltreats a carriage-return at the end of line as part of the line terminator, i.e. with it, trailing-space does not trigger if the character before such a carriage-return is not a whitespace (not enabled by default).

  • tabwidth=<n>tells how many character positions a tab occupies; this is relevant for indent-with-non-tab and when Git fixes tab-in-indent errors. The default tab width is 8. Allowed values are 1 to 63.

core.whitespace

要注意的常见空白问题的逗号分隔列表。git diff 将用于color.diff.whitespace突出显示它们,而git apply --whitespace=error会将它们视为错误。您可以添加前缀-以禁用其中任何一个(例如-trailing-space):

  • blank-at-eol将行尾的尾随空格视为错误(默认启用)。

  • space-before-tab将在行的初始缩进部分中紧接在制表符之前出现的空格字符视为错误(默认情况下启用)。

  • indent-with-non-tab将使用空格字符而不是等效制表符缩进的行视为错误(默认情况下未启用)。

  • tab-in-indent将行的初始缩进部分中的制表符视为错误(默认情况下未启用)。

  • blank-at-eof将在文件末尾添加的空行视为错误(默认启用)。

  • trailing-space是涵盖blank-at-eol和的简写blank-at-eof

  • cr-at-eol将行尾的回车符视为行终止符的一部分,即如果这样的回车符之前的字符不是空格(默认情况下不启用),则不会触发尾随空格。

  • tabwidth=<n>告诉一个选项卡占据多少个字符位置;这与 indent-with-non-tab 以及 Git 修复 tab-in-indent 错误有关。默认选项卡宽度为 8。允许的值为 1 到 63。



As you can see, blank-at-eofis enabled by default. You can disable it by adding -blank-at-eofto the core.whitespaceconfiguration, or alternatively by using your own core.whitespaceconfiguration.

如您所见,blank-at-eof默认启用。您可以通过添加-blank-at-eofcore.whitespace配置或使用您自己的core.whitespace配置来禁用它。