使 git 在差异中突出显示制表符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574195/
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
Make git highlight tab characters in a diff?
提问by jonderry
I'm trying to ensure that I don't commit code that uses tab characters for indentation. This is a soft constraint that I'm applying to my own commits (right now we don't have a standard for indentation characters, but I'd like to use spaces, since there's no disagreement on the width of a space, but some people use width-4 tabs versus width-8 tabs).
我试图确保我不提交使用制表符进行缩进的代码。这是我应用于自己提交的软约束(现在我们没有缩进字符的标准,但我想使用空格,因为在空格的宽度上没有分歧,但有些人们使用宽度为 4 的标签与宽度为 8 的标签)。
The easiest way to check such constraints is often to look at the actual output of git diff every time you are about to commit and see if there are any problems. For example, for me, by default, trailing whitespace is highlighted and windows newlines are also visible in the diff, so if I'm about to accidentally commit code with trailing whitespace, I will be alerted to this. Is there a way to make tab characters also show up in git diff?
检查此类约束的最简单方法通常是每次将要提交时查看 git diff 的实际输出,看看是否有任何问题。例如,对我来说,默认情况下,尾随空格被突出显示,并且窗口换行符也在差异中可见,所以如果我不小心提交带有尾随空格的代码,我会收到警告。有没有办法让制表符也显示在 git diff 中?
回答by Chris Johnsen
Git learned the tab-in-indent
whitespace category in 1.7.2 (2010 July 21).
From Documentation/RelNotes/1.7.2.txt:
Gittab-in-indent
在 1.7.2(2010 年 7 月 21 日)中学习了空白类别。
来自Documentation/RelNotes/1.7.2.txt:
- The whitespace rules used in "git apply --whitespace" and "git diff" gained a new member in the family (tab-in-indent) to help projects with policy to indent only with spaces.
- “git apply --whitespace”和“git diff”中使用的空格规则在家族中获得了一个新成员(tab-in-indent),以帮助项目制定仅使用空格缩进的策略。
It is controlled and used in the same ways as the other whitespace checking options.
它的控制和使用方式与其他空白检查选项相同。
The highlighting in git diff
is the same as the other whitespace errors.
Checking is available with git diff --check
.
Et cetera.
突出显示git diff
与其他空白错误相同。
检查可用git diff --check
。
等等。
Add tab-in-indent
to the value of the core.whitespace
configuration variable to enable it (probably either in one or more specific repositories or in your “global” (per-use) configuration).
添加tab-in-indent
到core.whitespace
配置变量的值以启用它(可能在一个或多个特定存储库中或在您的“全局”(每次使用)配置中)。
set-show-tabs() {
global=
test "" = -g || test "" = --global && global=--global
cws=$(git config $global core.whitespace)
case "$cws" in
tab-in-indent,*|*,tab-in-indent|*,tab-in-indent,*) ;;
*) git config $global core.whitespace "$cws"${cws:+,}tab-in-indent ;;
esac
}
set-show-tabs # only in local repository
set-show-tabs --global # for all your Git activities
# or just edit it manually with "git config [--global] --edit"
Or, you can set it for individual commands (git -c
is also from 1.7.2):
或者,您可以为单个命令设置它(git -c
也来自 1.7.2):
git -c core.whitespace=tab-in-indent diff --check
You could use something like this in a pre-commit
hook to check for tabs without having it in any of your actual repository configuration files.
您可以在pre-commit
钩子中使用类似的东西来检查选项卡,而无需在任何实际的存储库配置文件中使用它。
回答by Matt Curtis
To locate lines with tabs:
要定位带有制表符的行:
git grep -n --cached 'LITERAL TAB HERE'
In bash or zsh you can enter a literal tab with Ctrl-VCtrl-I. That command will show you all files+lines with tabs.
在 bash 或 zsh 中,您可以输入带有Ctrl-VCtrl-I. 该命令将向您显示所有带有选项卡的文件+行。
If you want to enforce your policy by preventing a commit with tabs, put this in .git/hooks/pre-commit
and mark it executable (chmod +x
):
如果您想通过防止使用选项卡提交来强制执行您的策略,请将其放入.git/hooks/pre-commit
并将其标记为可执行 ( chmod +x
):
#!/bin/sh
allowtabs=$(git config hooks.allowtabs)
if [ "$allowtabs" != "true" ] &&
git diff --cached | egrep '^\+.* '>/dev/null
then
cat<<END;
Error: This commit would contain a tab, which is against this repo's policy.
If you know what you are doing you can force this commit with:
git commit --no-verify
Or change the repo policy like so:
git config hooks.allowtabs true
END
exit 1
fi
There's a literal tab between the *
and the '
on the git diff --cached | egrep
line. You can get this in Vim with Ctrl-VCtrl-I, or Emacs with C-qC-i.
还有的之间的文字标签*
和'
上git diff --cached | egrep
线。你可以在 Vim 中使用Ctrl-VCtrl-I,或在 Emacs 中使用C-qC-i.
What it does is look for a new line in the diff (starting with "+") which contains a tab. You could put the git grep
line in the error message if you want to show the offending tabs in the hook.
它的作用是在差异(以“+”开头)中查找包含选项卡的新行。git grep
如果您想在挂钩中显示有问题的选项卡,您可以将该行放在错误消息中。
I've put this hook on github here.
我把这个钩子放在 github here 上。
回答by Martin Joiner
I have made a pre-commit hook that stops you from committing tab-indented code https://github.com/martinjoiner/portable-code-pre-commit-hookit looks like this...
我做了一个预提交钩子,阻止你提交制表符缩进的代码https://github.com/martinjoiner/portable-code-pre-commit-hook它看起来像这样......
I routinely use it on all my projects now. Feel free to use it yourself.
我现在经常在我所有的项目中使用它。随意使用它自己。
I work on a team that write code in a mix of Mac, Windows and Linux environments as well as reviewing it in-browser via the Github website. The team are really happy to have a helping hand checking their code so it looks consistent across all those places.
我在一个团队中工作,该团队在 Mac、Windows 和 Linux 的混合环境中编写代码,并通过 Github 网站在浏览器中对其进行。团队很高兴能得到帮助来检查他们的代码,因此它在所有这些地方看起来都是一致的。
If you find any problems please let me know, I'd like the chance to improve any weaknesses. Thanks.
如果您发现任何问题,请告诉我,我希望有机会改进任何弱点。谢谢。
回答by xaizek
One way of making tabulation characters visible is to replace them with something that's noticeable, for example .???????
(that's a dot followed by seven spaces). This can be achieved by substituting pager in your configuration to add invocation of sed
. Like this:
使制表字符可见的一种方法是将它们替换为一些引人注目的东西,例如.???????
(一个点后跟七个空格)。这可以通过在您的配置中替换 pager 以添加对sed
. 像这样:
[core]
pager = sed 's/\t/. /g' | less -R
Example
例子
Without custom pager:
没有自定义寻呼机:
for (int i = 0; i < 3; ++i) {
- for (int h = 0; h < 4; ++h) {
+ for (int h = 0; h < 4; ++h) {
for (int k = 0; k < 4; ++k) {
With custom pager:
使用自定义寻呼机:
. for (int i = 0; i < 3; ++i) {
- for (int h = 0; h < 4; ++h) {
+. . for (int h = 0; h < 4; ++h) {
. . . for (int k = 0; k < 4; ++k) {