private boolean switchBranch(LocalTask task, Runnable invokeAfter) {
    List<BranchInfo> branches = task.getBranches(false);
    // we should have exactly one branch per repo
    MultiMap<String, BranchInfo> multiMap = new MultiMap<>();
    for (BranchInfo branch : branches) {
      multiMap.putValue(branch.repository, branch);
    }
    for (String repo: multiMap.keySet()) {
      Collection<BranchInfo> infos = multiMap.get(repo);
      if (infos.size() > 1) {
        // cleanup needed
        List<BranchInfo> existing = getAllBranches(repo);
        for (Iterator<BranchInfo> iterator = infos.iterator(); iterator.hasNext(); ) {
          BranchInfo info = iterator.next();
          if (!existing.contains(info)) {
            iterator.remove();
            if (infos.size() == 1) {
              break;
            }
          }
        }
      }
    }
    VcsTaskHandler.TaskInfo info = fromBranches(new ArrayList<>(multiMap.values()));
    VcsTaskHandler[] handlers = VcsTaskHandler.getAllHandlers(myProject);
    boolean switched = false;
    for (VcsTaskHandler handler : handlers) {
      switched |= handler.switchToTask(info, invokeAfter);
    }
    return switched;
  }
private boolean switchBranch(LocalTask task, Runnable invokeAfter)