gpt4 book ai didi

git - 让 git 在差异中突出显示制表符?

转载 作者:IT王子 更新时间:2023-10-29 01:16:55 25 4
gpt4 key购买 nike

我正在努力确保我不会提交使用制表符进行缩进的代码。这是我对自己的提交应用的软约束(现在我们没有缩进字符的标准,但我想使用空格,因为在空格的宽度上没有分歧,但有些人们使用宽度为 4 的标签与宽度为 8 的标签)。

检查此类约束的最简单方法通常是在每次要提交时查看 git diff 的实际输出,看看是否存在任何问题。例如,对我来说,默认情况下尾随空格会突出显示,Windows 换行符也会在 diff 中可见,因此如果我不小心提交了带有尾随空格的代码,我会收到警告。有没有办法让制表符也出现在 git diff 中?

最佳答案

Git 在 1.7.2(2010 年 7 月 21 日)中学会了 tab-in-indent 空白类别。
来自 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 diff 中的高亮显示与其他空白错误相同。
使用 git diff --check 可以进行检查。
等等。

tab-in-indent 添加到 core.whitespace 配置变量的值中以启用它(可能在一个或多个特定存储库中或在您的“全局”中”(每次使用)配置)。

set-show-tabs() {
global=
test "$1" = -g || test "$1" = --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"

或者,您可以为单个命令设置它(git -c 也来自 1.7.2):

git -c core.whitespace=tab-in-indent diff --check

您可以在 pre-commit Hook 中使用类似的东西来检查选项卡,而无需将它放在任何实际的存储库配置文件中。

关于git - 让 git 在差异中突出显示制表符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5574195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com