我正在和我的团队一起使用Git,并希望从我的差异、日志、合并等中删除空白更改。我假设做到这一点最简单的方法是Git在应用所有提交时自动删除尾随空白(和其他空白错误)。

我已经尝试将以下内容添加到~/。gitconfig文件,但是当我提交时它什么也不做。也许它是为别的东西设计的。解决方案是什么?

[core]
    whitespace = trailing-space,space-before-tab
[apply]
    whitespace = fix

我使用Ruby,以防有人对Ruby有任何具体的想法。在提交之前自动格式化代码将是下一步,但这是一个困难的问题,并不是真正造成大问题。


当前回答

崇高的文本用户。

在“设置-用户”配置中正确设置以下内容。

"trim_trailing_white_space_on_save": true

其他回答

使用Git属性,并使用Git配置设置过滤器

好的,这是解决这个问题的一个新方法……我的方法是不使用任何钩子,而是使用过滤器和Git属性。这允许您在开发的每台机器上设置一组过滤器,这些过滤器将在提交文件之前去除额外的尾随空白和额外的空行。

然后设置一个.gitattributes文件,说明过滤器应该应用于哪种类型的文件。过滤器有两个阶段,在将文件添加到索引时应用clean,在将文件添加到工作目录时应用smudge。

告诉Git查找全局属性文件

首先,告诉全局配置使用全局属性文件:

git config --global core.attributesfile ~/.gitattributes_global

创建全局过滤器

现在,创建过滤器:

git config --global filter.fix-eol-eof.clean fixup-eol-eof %f
git config --global filter.fix-eol-eof.smudge cat
git config --global filter.fix-eol-eof.required true

添加sed脚本魔法

最后,将fixup-eol-eof脚本放在路径中的某个位置,并使其可执行。该脚本使用sed执行一些动态编辑(删除行尾的空格和空格,以及文件末尾无关的空行)。

fix -eol-eof应该是这样的:

#!/bin/bash
sed -e 's/[     ]*$//' -e :a -e '/^\n*$/{$d;N;ba' -e '}' $1

我的要点是

告诉Git将新创建的过滤器应用于哪些文件类型

最后,创建或打开文件~/。在你最喜欢的文本编辑器中添加Gitattributes_global,并添加如下行:

pattern attr1 [attr2 [attr3 […]]]

所以如果我们想要修复空白的问题,对于我们所有的C源文件,我们将添加一行,看起来像这样:

*.c filter=fix-eol-eof

滤波器的讨论

过滤器有两个阶段。当东西被添加到索引或签入时应用的清洁阶段,以及当Git将东西放入工作目录时应用的涂抹阶段。

在这里,我们的smudge只是通过cat命令运行内容,这应该使它们保持不变,除了可能添加一个尾随换行符(如果文件末尾没有换行符的话)。

The clean command is the white space filtering which I cobbled together from notes at http://sed.sourceforge.net/sed1line.txt. It seems that it must be put into a shell script. I couldn’t figure out how to inject the sed command, including the sanitation of the extraneous extra lines at the end of the file directly into the git-config file. (You can get rid of trailing blanks, however, without the need of a separate sed script. Just set the filter.fix-eol-eof to something like sed 's/[ \t]*$//' %f where the \t is an actual tab, by pressing Tab.)

如果出现错误,require = true会引发一个错误,以避免遇到麻烦。

这可能不会直接解决您的问题,但您可能希望在实际的项目空间中通过git-config设置这些,它编辑文件./。Git /config相对于~/.gitconfig文件。在所有项目成员之间保持设置一致是很好的。

git config core.whitespace "trailing-space,space-before-tab"
git config apply.whitespace "trailing-space,space-before-tab"

同样的结果。

import subprocess                                                                 
                                                                              
def get_trailing_lines():                                                         
                                                                              
    result = subprocess.run([                                                     
                            'git',                                            
                            'diff',                                           
                            '--check'                                         
                        ], capture_output=True)                               
                                                                              
    return result.stdout.decode().split('\n')                                     
                                                                              
                                                                              
def modify_line(file_path, l_num):                                                
                                                                              
    f_lines = open(file_path).readlines()                                         
    f_lines[l_num] = f_lines[l_num].rstrip()+'\n'\                                
                     if '\n' in f_lines[l_num] else f_lines[l_num].rstrip()    
                                                                              
    with open(file_path, "w") as w_fp:                                            
        w_fp.writelines(f_lines)                                                  
                                                                              
                                                                              
if __name__ == '__main__':                                                        
                                                                              
    l = get_trailing_lines()                                                      
    for m, d in zip(l[::2], l[1::2]):                                             
        f_path, l_no, *_ = m.split(":")                                           
        modify_line(f_path, int(l_no)-1)                                          

请试试我的预提交钩子。它可以自动检测尾随空白并删除它。

它可以在Git Bash (Windows), Mac OS X和Linux下工作!


快照:

$ git commit -am "test"
auto remove trailing whitespace in foobar/main.m!
auto remove trailing whitespace in foobar/AppDelegate.m!
[master 80c11fe] test
1 file changed, 2 insertions(+), 2 deletions(-)

下面是Ubuntu和Mac OS X兼容的版本:

#!/bin/sh
#

# A Git hook script to find and fix trailing white space
# in your commits. Bypass it with the --no-verify option
# to git-commit
#

if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
  against=HEAD
else
  # Initial commit: diff against an empty tree object
  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Find files with trailing whitespace
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | (sed -r 's/:[0-9]+:.*//' > /dev/null 2>&1 || sed -E 's/:[0-9]+:.*//') | uniq` ; do
  # Fix them!
  (sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || sed -i '' -E 's/[[:space:]]*$//' "$FILE")
  git add "$FILE"
done

# Now we can commit
exit