How to find the current git branch in a plugin

Answered

The code is this:

List<GitRepository> repositories = GitUtil.getRepositoryManager(e.getProject()).getRepositories();
if (repositories.size() > 0){
System.out.println(repositories.get(0).getCurrentBranch());
}

Assuming you are using intellij community edition to build your plugin, you will need to add the following:

/snap/intellij-idea-community/185/plugins/git4idea/lib/git4idea.jar

( you can check the path for other jars in this window )

To:

File > Project Structure >  SDKs > Intellij IDEA Community Edition IC-XXX.XXXX.XX > Classpath

Also, you will need to add the following to plugin.xml:

<depends>Git4Idea</depends>

https://github.com/uwolfer/gerrit-intellij-plugin/commit/ef256e28aea0412cb7bc8f42965e09549ec60bcd

Ref: https://intellij-support.jetbrains.com/hc/en-us/community/posts/203361804-How-to-build-and-use-git4idea-as-a-library-in-plugin

Ref: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009312059-Git4idea-in-installed-plugin-doesn-t-work

Ref: https://github.com/zielu/GitToolBox/blob/master/src/main/resources/META-INF/plugin.xml

 

 

 

3 comments
Comment actions Permalink

Sorry, is this a question or ..?

0
Comment actions Permalink

Hello, This is not a question. Its a PSA. It took me a while to figure this out so I posted it hoping it will help someone in the future.

0
Comment actions Permalink

Ok. Please note that described way of setup uses Devkit-setup, which is not recommended for new plugins. See https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html.

0

Please sign in to leave a comment.