I\'d like to add an automatically generated file to the same commit using a pre- or post-commit hook in Git, dependent on the files that were modified in that commit. How w
Since git add was also not working for me in a pre commit, I followed mark's idea of using a .commit file and splitting the process into pre- and post-commit.
Here is some code that should be easy to understand
In the pre-commit:
- Touch a file .commit or something. (be sure to add this to .gitignore)
#!/bin/sh
echo
touch .commit
exit
In the post-commit:
if .commit exists you know a commit has just taken place but a post-commit hasn't run yet. So, you can do your code generation here. Additionally, test for .commit and if it exists:
- add the files
- commit --amend -C HEAD --no-verify (avoid looping)
- delete .commit file
#!/bin/sh
echo
if [ -e .commit ]
then
rm .commit
git add yourfile
git commit --amend -C HEAD --no-verify
fi
exit
Hope this makes it easier for people with few bash knowledge to follow mark's idea.
I had the same need and this approach worked pretty well for me:
#!/bin/sh
files='git diff --cached --name-only'
re="<files of importance>"
if [[ $files =~ $re ]]
then
echo "Creating files"
create_my_files && git add my_files
fi
where "create_my_files" should be executable, for example if it is a python file you could execute it as "python create_my_files && git add my_files"
and is true you don't need a pre-commit to commit again (that would create a infinite nasty loop :p)
It's possible to do what you want using pre-commit hooks. We do something similar for a heroku deployment (compiling coffeescript to javascript). The reason your script isn't working is because you used the exec
command improperly.
From the man page:
The exec builtin is used to replace the currently running shells process image with a new command. On successful completion, exec never returns. exec can not be used inside a pipeline.
Only your first exec command is running. After that your script is basically terminated.
Give something like this a try (as a pre-commit hook):
#!/bin/sh
files=`git diff --cached --name-status`
re="<files of importance>"
if [[ $files =~ $re ]]
then
echo "Creating files"
bundle exec create_my_files
git add my_files
fi
You could use a combination of a pre and post commit script.
In the pre-commit:
In the post-commit:
if .commit exists you know a commit has just taken place but a post-commit hasn't run yet. So, you can do your code generation here. Additionally, test for .commit and if it exists:
This is roughly the process I use to store a .metadata file in the repository generated from metastore.
If anyone knows a better way I'm all ears but it seems to work for now.
You can use update-index
:
git update-index --add my_files
I was facing same problem in pre-commit hook also. I was modifying one file and committing but it was taking previous file not updated file so by adding git command(as below) in pre-commit hook, it solved.
git add $file
note: $file
is your file to be added.
Thanks,