Skip to content

Instantly share code, notes, and snippets.

@adovbos
Last active February 7, 2024 11:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adovbos/0d34fc5a60469c4592cbe5e10307853b to your computer and use it in GitHub Desktop.
Save adovbos/0d34fc5a60469c4592cbe5e10307853b to your computer and use it in GitHub Desktop.
How to make PhpStorm work with git inside the docker

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:

  1. We create executable file like 'my-executable' and give it +x rights.

touch my-executable

sudo chmod +x my-executable

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

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

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

@choinek
Copy link

choinek commented Jan 6, 2023

I think that $command_pattern definition is missing here, could you attach it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment