Suppose git status
给出这个:
# On branch X
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.cc
# modified: file1.h
# modified: file1_test.cc
# modified: SConscript
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
# (commit or discard the untracked or modified content in submodules)
#
# modified: file1.cc
# modified: tinyxml2 (untracked content)
#
在这种情况下,仅对 file1.cc 所做的部分更改已上演/为下一次提交建立索引。
我运行一个预提交脚本来运行样式检查器:
#!/bin/bash
git stash -q --keep-index
# Do the checks
RESULT=0
while read status file
do
if python ~/python/cpplint.py "$file"; then
let RESULT=1
fi
done < <(git diff --cached --name-status --diff-filter=ACM | grep -P '\.((cc)|(h)|(cpp)|(c))$' )
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0
正如建议的here http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/,我在运行样式检查之前隐藏未暂存的文件,然后弹出它们。但是,在仅暂存文件中的部分更改的情况下,当我在预提交挂钩末尾弹出存储时,这会导致合并冲突。
有什么更好的方法来做到这一点?我想对即将提交的文件的暂存版本运行样式检查。
我会避免使用git stash
自动进入钩子。我发现可以使用git show ':filename'
获取隐藏文件的内容。
相反,我使用了下一种方法:
git diff --cached --name-only --diff-filter=ACMR | while read filename; do
git show ":$filename" | GIT_VERIFY_FILENAME="$filename" verify_copyright \
|| exit $?
done \
|| exit $?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)