Sometimes I/You/We need to use git inside docker container. For example to launch a lint checks hook for php and js before creating commit. All paths in git hook will be directed to container executables because they should be contained inside the container. I hope I don't need to explain why.
And if that is your case and you want to use git in PhpStorm you are on right web page
And BTW, I'm sorry for my english and my bash skills :)
Long story short:
- We create executable file like 'my-executable' and give it +x rights.
touch my-executable
sudo chmod +x my-executable
- Edit file using ONLY Vim!!!!! One love...
<your-editor> my-executable
#!/bin/bash
file_path_pattern="\/tmp\/(git-commit-msg-.txt|git-commit-msg-[0-9]*.txt)"
commit_pattern=" -F $file_path_pattern"
double_dash_pattern=" --$"
if [[ $@ =~ $command_pattern ]]
then
if [[ $@ =~ $file_path_pattern ]]
then
commit_msg=$(<$BASH_REMATCH)
else
commit_msg="Placeholder commit message"
fi
command=$(echo "$@" | sed -r -e "s/$commit_pattern//g")
docker exec <your-container> git $(echo $command | sed -re "s/$double_dash_pattern//g") -m "$commit_msg" --
else
docker exec <your-container> git $@
fi
Why we need this patterns and etc? PhpStorm is... annoyingly using command git commit -F /tmp/git-commit-msg-.txt
to create commit, and container obviously don't know about /tmp folder on host machine. So we need to read content from this file and replace -F <file path>
part to -m <message>
-
Then we go to PhpStorm settings Version Control > Git, and change
Path to Git executable
to our my-executable file./home/<username>/my-executable
for example. -
Pressing Test button to make sure everything working. Enjoy!
This is not perfect code. Please leave a comment if you have idea how to improve it
I think that $command_pattern definition is missing here, could you attach it?