我正在开发验证和 linting 实用程序以与各种提交 Hook 集成,包括 Git one
https://github.com/miohtama/vvv
目前验证器和 linters 在每次提交时针对整个项目代码库运行。但是,最好只针对更改的文件运行它们。为此,我需要知道我的 Git 预提交 Hook (在 Python 中)中更改的文件列表
https://github.com/miohtama/vvv/blob/master/vvv/hooks/git.py
我必须使用哪些选项来提取已更改的文件列表(如果重要,则使用 Python)?
最佳答案
预提交 Hook 有点麻烦,如果你真的想让事情“正确”地工作,因为工作树中的内容不一定与要提交的内容相同:
$ echo morestuff >> file1; echo morestuff >> file2
$ git add file1 # but not file2
$ git commit -m 'modified two files but check in just one'
您可以使用 git diff-index --cached HEAD
来获取“将要 checkin 的内容”的列表。另见,例如 http://newartisans.com/2009/02/building-a-better-pre-commit-hook-for-git/ .
关于python - Git 预提交 Hook : getting list of changed files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162695/