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
ここで提案されているように、スタイルチェックを実行する前にステージングされていないファイルを隠し、後でポップします。ただし、ファイル内の変更の一部のみがステージングされた場合、pre-commitフックの最後にスタッシュをポップすると、マージの競合が発生します。
これを行うためのより良い方法は何ですか?コミットしようとしているファイルのステージングされたバージョンに対してスタイルチェックを実行したいと思います。