假设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
正如这里所建议的,我在运行样式检查之前存储未暂存的文件,然后将它们弹出。但是,在仅暂存文件中的某些更改的情况下,当我在预提交挂钩的末尾弹出存储时,这会导致合并冲突。
有什么更好的方法来做到这一点?我想对即将提交的文件的暂存版本运行样式检查。