@Override public void update(AnActionEvent e) { super.update(e); Presentation presentation = e.getPresentation(); Project project = e.getProject(); if (project == null) { presentation.setEnabled(false); presentation.setVisible(false); return; } VirtualFile[] vFiles = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY); if (vFiles == null || vFiles.length != 1 || vFiles[0] == null) { // only 1 file for now presentation.setEnabled(false); presentation.setVisible(true); return; } GitRepositoryManager manager = GitUtil.getRepositoryManager(project); GitRepository repository = manager.getRepositoryForFile(vFiles[0]); if (repository == null || repository.isFresh() || noBranchesToCompare(repository)) { presentation.setEnabled(false); presentation.setVisible(true); return; } presentation.setEnabled(true); presentation.setVisible(true); }
@Override public void actionPerformed(final AnActionEvent event) { final Project project = event.getProject(); assert project != null; final VirtualFile file = getAffectedFile(event); GitRepositoryManager manager = GitUtil.getRepositoryManager(project); GitRepository repository = manager.getRepositoryForFile(file); assert repository != null; GitBranch currentBranch = repository.getCurrentBranch(); final String head; if (currentBranch == null) { String currentRevision = repository.getCurrentRevision(); LOG.assertTrue( currentRevision != null, "Current revision is null for " + repository + ". Compare with branch shouldn't be available for fresh repository"); head = DvcsUtil.getShortHash(currentRevision); } else { head = currentBranch.getName(); } final List<String> branchNames = getBranchNamesExceptCurrent(repository); // prepare and invoke popup final JBList list = new JBList(branchNames); JBPopupFactory.getInstance() .createListPopupBuilder(list) .setTitle("Select branch to compare") .setItemChoosenCallback(new OnBranchChooseRunnable(project, file, head, list)) .setAutoselectOnMouseMove(true) .createPopup() .showInBestPositionFor(event.getDataContext()); }
@NotNull private GitRepository getRepository(@NotNull FilePath path) { GitRepository repository = myRepositoryManager.getRepositoryForFile(path); LOG.assertTrue(repository != null, "Repository is null for " + path); return repository; }