【发布时间】:2012-11-26 16:06:18
【问题描述】:
假设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 的建议,我在运行样式检查之前存储未暂存的文件,然后将它们弹出。但是,在仅暂存文件中的某些更改的情况下,当我在预提交挂钩的末尾弹出存储时,这会导致合并冲突。
有什么更好的方法来做到这一点?我想对即将提交的文件的暂存版本运行样式检查。
【问题讨论】:
-
我认为那里接受的答案比这里的任何答案都更好(我正在尝试将文件传递给几个不同的样式检查器)。
标签: git pre-commit-hook pre-commit