我想用
git config core.whitespace tab-in-indent,tabwidth=4
我想为 c++ 文件设置这些设置,以便在使用 git diff 时出现错误缩进时收到警告。但是,我也有需要选项卡的 Makefile。有没有办法为不同的文件配置不同的空白设置?
我想用
git config core.whitespace tab-in-indent,tabwidth=4
我想为 c++ 文件设置这些设置,以便在使用 git diff 时出现错误缩进时收到警告。但是,我也有需要选项卡的 Makefile。有没有办法为不同的文件配置不同的空白设置?
您可以使用gitattributes来调整这些设置。这是我的 .gitattributes 文件的片段:
*.c text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.cpp text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.h text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.hpp text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.py text diff=python whitespace=trailing-space,space-before-tab,tab-in-indent
*.tex text diff=tex whitespace=trailing-space,space-before-tab,tab-in-indent
*.java text diff=java whitespace=trailing-space,space-before-tab,tab-in-indent
*.pl text diff=perl whitespace=trailing-space,space-before-tab,tab-in-indent
*.php text diff=php whitespace=trailing-space,space-before-tab,tab-in-indent
*.rb text diff=ruby whitespace=trailing-space,space-before-tab,tab-in-indent
*.vcproj eol=crlf
*.dsp eol=crlf
*.dsw eol=crlf
*.sh eol=lf
*.jpg binary
*.png binary
*.gif binary
*.tiff binary
要调整您的空白设置,您可以使用以下内容:
*.ext whitespace=tab-in-indent,tabwidth=4
*.extcan 指向路径,包含 glob 等。这是非常灵活的机制。
John Szakmeister和UpAndAdam对答案的一些补充。
要设置特定于文件的规则,您必须.gitattributes在项目的根目录中添加一个文件。docs
(如果您不希望它受版本控制,可以将其添加为:.git/info/attributes.)
# Macro's
[attr]cpp diff=cpp whitespace=trailing-space,space-before-tab,indent-with-non-tab,tabwidth=4
[attr]makefile whitespace=trailing-space,indent-with-non-tab,space-before-tab,tabwidth=4
*.[ch] cpp
*.[ch]pp cpp
makefile makefile
s.makefile makefile
*匹配所有内容并[ch]匹配字符c和h. whitespace选项列出了要警告的事情。
tabwidth选项中的设置whitespace用于确定何时以及如何替换制表符和空格字符。(默认值为 8 个字符。) tab-indent认为使用制表符缩进是错误的。 indent-with-non-tab在行首使用 4 个或更多空格时在此处发出警告。请注意,接受 3 个空格的缩进!space-before-tab以捕获隐藏在选项卡之前和之间的空格。diff=cpp为 C 和 C++ 文件启用更智能的差异。trailing-space警告行尾和文件尾的尾随空格字符。.gitattributes请使用:git check-attr --all -- <pathname>
<pathname>不必是现有文件。(即some.cpp工作)
whitespace规则:git diff.trailing space
trailing tab
2 spaces
4 spaces
tab
tab and space
space and tab
tab, space, tab
调用时标记问题并在调用时git diff检查/修复git -apply ---whitespace=[warn|error|fix]。配置 with 的默认行为git apply:
git config --[global|local] apply.whitespace [warn|error|fix]
正如 jszakmeister 从我的评论中更新的那样,这里只是您所询问内容的一个小节。
请注意“makefile-ish”条目中修饰符的使用,-以表示不要调用 tab-in-indent 错误。
makefile text whitespace=-tab-in-indent,trailing-space,tabwidth=4
Makefile text whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.mk text whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.cpp text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4
*.hpp text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4