How-to create custom plugin to automatically change deployment target based on current git branch?


As describe in my other post:

I would like to create an plugin that checks the git branch of the project directory. Based on the current branch, I want to update the default target deployment.

See the attached screenshot (file name: "Screen Shot 2019-04-24 at 5.54.09 PM.png", right now root@localhost:22372 is my default deployment target, i'm on branch "dev22372".
If I switch to branch "dev22400" (git checkout dev22400), It would be great if Pycharm could switch the default deployment target to "root@localhost:22400", same of every other branch (dev22100, dev2296, etc...).

Any leads or pointer on how to do this with a plugin? (I've never wrote an intellij plugin before).
The plugin could be very simple and very dumb, something like:

In a loop:
- Get current dir git branch
- Check if deployment target is matches the current branch. Branch name in format devXXXX, where XXXX is the localhost port for the deployment target.
---- Update current branch if needed: "git checkout devXXXX" && upload to root@localhost:XXXX
- Sleep 5

Is it doable?

1 comment

Hi Julien, yes, it is doable. 

1. No need to do this in a loop. You can subscribe to the `GitRepository.GIT_REPO_CHANGE` MessageBus Topic, and the listener will be pinged on each change in Git repository.

2. To get the current branch, use `GitRepository.getCurrentBranch()`. To get a GitRepository instance, ask GitRepositoryManager.getInstance().

Hope this helps. Please check the usages of these items in intellij-community to see some examples. In case of difficulties, please write back, we'll try to help.


Please sign in to leave a comment.