简体   繁体   中英

git python pre-commit hook to add files

I have a pre-commit-hook written in python, which does some changes for a file. Now I would want to also add this file, to include the changes the pre-commit-hook does.

I tried to use subprocess to do this

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.
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

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. See this commit in the Git repository for Git.

(I remember having this not work correctly in some version of Git, but do not recall it being broken in any specific version.)

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. This solution is ugly since it makes every commit seem to fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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