简体   繁体   English

git python pre-commit钩子添加文件

[英]git python pre-commit hook to add files

I have a pre-commit-hook written in python, which does some changes for a file. 我有一个用python编写的pre-commit-hook ,它会对文件进行一些更改。 Now I would want to also add this file, to include the changes the pre-commit-hook does. 现在,我还想添加此文件,以包括pre-commit-hook所做的更改。

I tried to use subprocess to do this 我试图用subprocess来做到这一点

subprocess.check_call(('git', 'add', filename))

If I try this in the interactive python session it properly adds the file, but I the hook it doesn't work. 如果我在交互式python会话中尝试此操作,它将正确添加文件,但是我将其钩住不起作用。
I already checked out, that the hook works form the proper directory. 我已经签出了,钩子形成正确的目录。

How can I add files the right way? 如何正确添加文件?

Edit: My git version is 2.1.4 编辑:我的git版本是2.1.4

Interesting: as long as the pre-commit hook really is running, the git add should take effect in any Git version later than 1.5.4. 有趣的是:只要预提交钩子确实在运行, git add应该在1.5.4之后的任何Git版本中生效。 See this commit in the Git repository for Git. 在Git的Git存储库中查看此提交

(I remember having this not work correctly in some version of Git, but do not recall it being broken in any specific version.) (我记得在某些版本的Git中无法正常工作,但不记得在任何特定版本中它都已损坏。)

The alternative, if this is broken in your particular Git version, is to have your pre-commit hook add the file, run its own git commit that arranges to have the inner, recursive commit not recurse again—it's up to you how to work this out—and then when the inner commit finishes, reject the commit. 如果您的特定Git版本中有此问题,替代方法是让您的预提交钩子添加文件,运行自己的 git commit ,以使内部递归提交不再再次递归,这取决于您的工作方式这样,然后当内部提交完成时,拒绝提交。 This solution is ugly since it makes every commit seem to fail. 这种解决方案很丑陋,因为它使每次提交似乎都失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM