python - Git 预提交 Hook : getting list of changed files

标签 python git pre-commit-hook pre-commit

我正在开发验证和 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/

相关文章:

python - 将 CPLEX 参数传递给 CVXPY

python - 删除 SQLAlchemy 生成的查询中的引号

Python正则表达式获取特定字符串后的第一个元素

git - 如果我制作了一个开源项目的本地副本来继续工作,我如何将开发人员的新更改 merge 到我的代码中?

python - SVN钩子(Hook)脚本冲突

hook - Gitlab 预提交钩子(Hook)

python - 从外部调用时找不到模块

git rerere 不自动提交自动更新 merge 解决方案

git - 如何通过 Autohotkey 读取 Git Bash 中命令的输出

git clone git hooks 中的不同存储库造成麻烦