how to get Git branch name, commit IDs from local branch?

0
4 comments

To get current branch name:

git4idea.repo.GitRepositoryManager.getInstance(myProject).getRepositories()
gitRepository.getCurrentBranch().getName();

To get all commits in a local branch (query `git log <branch name>`)

git4idea.history.GitHistoryUtils#history(project, gitRepository.getRoot(), branchName);

 

0

Thanks for the reply.

I added the code to get branch name, and it compiled OK, but failed at testing:

git4idea/repo/GitRepositoryManager
java.lang.NoClassDefFoundError: git4idea/repo/GitRepositoryManager

Here is the code:

GitRepositoryManager gitRepositoryManager = GitRepositoryManager.getInstance(project);
List<GitRepository> repositories = gitRepositoryManager.getRepositories();
for (GitRepository repository : repositories) {
System.out.println("Current Branch Name: " + repository.getCurrentBranch().getName());
System.out.println("Current Branch Full Name: " + repository.getCurrentBranch().getFullName());
}

 

Here is part of build.gradle:

apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'jacoco'


compileJava {
options.compilerArgs << '-Xlint:unchecked'
options.compilerArgs << '-Werror'
options.fork = true
options.forkOptions.executable = 'javac'

sourceCompatibility = 1.8
targetCompatibility = 1.8
}

intellij {
version '2017.3.2' //or a full list of IntelliJ IDEA releases please see https://www.jetbrains.com/intellij-repository/releases
plugins 'git4idea'
}

 

 

 

 

 

 

0

You need to modify your `plugin.xml` and add dependency on git4idea plugin.

<depends>Git4Idea</depends>

See https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_dependencies.html

3

Thanks. It worked.

0

Please sign in to leave a comment.