Search code examples
gitintellij-ideaintellij-plugin

IDEA plugin: How to open the git windows and focus on special commit by commit id?


I am developing a plugin to work with Git. I want to open the Git windows and focus on a special commit by the commit id.

I found I can open the Git window with this code:

ProjectLevelVcsManager.getInstance(project).showConsole();

But I have no idea how to focus on a special commit.


Solution

  • I solve the problem,Here is the code :

    String commitId = "123456"; //suppose it is commit id
    VcsLogContentUtil.runInMainLog(project,(logUi) -> jumpToRevisionUnderProgress(project,logUi,commitId));
    
    
    private void jumpToRevisionUnderProgress(Project project,VcsLogUiEx logUi,String commitId){
        //some null check
        //....
        Future<Boolean> future = VcsLogNavigationUtil.jumpToHash(logUi, commitId, false, true);
        if (!future.isDone()) {
          ProgressManager.getInstance().run(new Task.Backgroundable(project,
    "my title", false,PerformInBackgroundOption.ALWAYS_BACKGROUND) {
            @Override
            public void run(@NotNull ProgressIndicator indicator) {
              try {
                future.get();
              }
              catch (CancellationException | InterruptedException ignored) {
              }
              catch (ExecutionException e) {
                LOG.error(e);
              }
            }
          });
        }
     }
    

    Also see IDEA Community