this pre-commit hook中发生了什么?我以为更改文件会导致它们被重新加载。
#!/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我认为这个想法是在这个提交涉及到的所有文件中删除尾随空格。
发布于 2010-04-21 02:15:20
关键是提交正确的内容,即:
引入的修改
第一点是通过git diff-index实现的
将通过树对象找到的blobs的内容和模式与当前索引的内容进行比较,并且可以选择忽略磁盘上文件的stat状态。
exec git diff-index --check --cached $against --使用选项--cached
根本不考虑磁盘上的文件
然后,任何修改都将被考虑为新提交的一部分。
您可以查看source of commit.c
static int prepare_to_commit(const char *index_file, const char *prefix,
struct wt_status *s)
{
...
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
return 0;
...
/*
* Re-read the index as pre-commit hook could have updated it,
* and write it out as a tree. We must do this before we invoke
* the editor and after we invoke run_status above.
*/
discard_cache();
read_cache_from(index_file);发布于 2011-02-17 06:21:09
但这并不起作用。我尝试在预提交钩子的末尾执行以下操作:
exec git diff-index --check --cached $against --但是在这些钩子中所做的更改仍然没有真正被提交(至少在git 1.7.3.4中)。
如果您真的希望更改生效,则必须显式
git add "$file"对于您在预提交阶段修改的每个文件。
发布于 2016-07-21 11:27:04
这样做是可能的,但需要一个棘手的脚本。
在这里,您可以找到相同的问题已解决。在这里,它在每次提交时更新文件版本,而不是颤动空格。它完全正常工作:https://github.com/addonszz/Galileo/tree/master/githooks
然后,您只需将文件“updateVersion.sh”上的“版本文件替换”算法替换为您的“Trilling Spaces”算法。也许你需要更改一些东西,比如删除分支限制,因为在那里,脚本只有在你处于“开发”分支时才会运行。
此外,如果是暂存的,它只会更改文件。如果文件未暂存,则不会执行任何操作。更准确地说,它打印出它在每一步中正在做的事情。
https://stackoverflow.com/questions/2677425
复制相似问题