这个pre-commit hook如何修复尾随空格? [英] How does this pre-commit hook fix trailing whitespace?

查看:154
本文介绍了这个pre-commit hook如何修复尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个预先设定的内容正在发生什么,提交钩子

 #!/ bin / sh $ b我认为更改文件会导致它们被重新设置。 $ b#
#一个git钩子脚本,用于在您的提交中查找并修复尾部空白符
#。使用--no-verify选项旁路它
#git-commit


if git-rev-parse --verify HEAD> / dev / null 2> & 1;然后
对= HE​​AD
else
#初始提交:针对空树对象的差异
针对= 4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
#查找带尾随空白的文件$在`exec git diff-index --check --cached $针对 - |的情况下,b $ b用于FILE sed'/ ^ [+ - ] / d'| sed -r's /:[0-9] +:。* //'| uniq`;做
#修复它们!
sed -i's / [[:space:]] * $ //'$ FILE
done

#现在我们可以提交
exit

我认为这个想法是删除这个提交触及的所有文件中的尾部空白。

解决方案

除此之外无效。我已经尝试在我的预提交钩子结束时执行以下操作:

  exec git diff-index --check  - -cached $ against  -  

但是在这些钩子中所做的更改仍然没有实际提交(at至少在git 1.7.3.4中)。



如果您确实想要进行更改,您必须明确地

  git为每个文件添加$ file

在预先提交阶段进行修改。

What is going on in this pre-commit hook? I thought changing files would cause them to be restaged.

#!/bin/sh
#
# A git hook script to find and fix trailing whitespace
# 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]+:.*//' | uniq` ; do
    # Fix them!
    sed -i 's/[[:space:]]*$//' "$FILE"
done

# Now we can commit
exit

I think the idea is to remove trailing whitespace in all files this commit touches.

解决方案

Except that this does not work. I've tried doing the following at the end of my pre-commit hooks:

exec git diff-index --check --cached $against --

but the changes made in those hooks still do not actually get committed (at least in git 1.7.3.4).

If you actually want the changes to go in, you must explicitly

git add "$file"

for each file that you modified during the pre-commit phase.

这篇关于这个pre-commit hook如何修复尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆