private static Groups prepareGroups(
      @NotNull VcsLogDataPack dataPack,
      @Nullable Collection<VirtualFile> visibleRoots,
      @Nullable List<List<String>> recentItems) {
    Groups filteredGroups = new Groups();
    Collection<VcsRef> allRefs = dataPack.getRefs().getBranches();
    for (Map.Entry<VirtualFile, Set<VcsRef>> entry :
        VcsLogUtil.groupRefsByRoot(allRefs).entrySet()) {
      VirtualFile root = entry.getKey();
      if (visibleRoots != null && !visibleRoots.contains(root)) continue;
      Collection<VcsRef> refs = entry.getValue();
      VcsLogProvider provider = dataPack.getLogProviders().get(root);
      VcsLogRefManager refManager = provider.getReferenceManager();
      List<RefGroup> refGroups = refManager.group(refs);

      putActionsForReferences(refGroups, filteredGroups);
    }

    if (recentItems != null) {
      for (List<String> recentItem : recentItems) {
        if (recentItem.size() == 1) {
          final String item = ContainerUtil.getFirstItem(recentItem);
          if (filteredGroups.singletonGroups.contains(item)
              || ContainerUtil.find(
                      filteredGroups.expandedGroups.values(), strings -> strings.contains(item))
                  != null) {
            continue;
          }
        }
        filteredGroups.recentGroups.add(recentItem);
      }
    }

    return filteredGroups;
  }
 private static <T> void addNewElements(
     @NotNull Collection<T> original, @NotNull Collection<T> toAdd) {
   for (T item : toAdd) {
     if (!original.contains(item)) {
       original.add(item);
     }
   }
 }