Git pre-commit hook:获取已更改文件的列表

Mik*_*maa 7 python git pre-commit pre-commit-hook

我正在开发验证和linting实用程序,以与各种提交钩子集成,包括Git one

https://github.com/miohtama/vvv

目前,验证器和连接器在每次提交时都针对整个项目代码库运行.但是,仅针对更改的文件运行它们会更加优化.为此,我需要知道我的Git precommit钩子中的已更改文件列表(在Python中)

https://github.com/miohtama/vvv/blob/master/vvv/hooks/git.py

我有什么选择来提取已更改的文件列表(如果重要的话,在Python中)?

tor*_*rek 6

如果你真的想让事情"正确",那么预提交钩子有点痛苦,因为工作树中的内容不一定与提交内容相同:

$ echo morestuff >> file1; echo morestuff >> file2
$ git add file1 # but not file2
$ git commit -m 'modified two files but check in just one'
Run Code Online (Sandbox Code Playgroud)

您可以使用git diff-index --cached HEAD获取"即将登记的内容"的列表.另见,例如,http://newartisans.com/2009/02/building-a-better-pre-commit-hook-for-git/.

  • 到目前为止,工作完美。这是完整的解决方案https://github.com/miohtama/vvv/blob/master/vvv/hooks/git.py (2认同)