/** @param removeProcessor parent, child */
 public static <T> Collection<T> removeAncestors(
     final Collection<T> files,
     final Convertor<T, String> convertor,
     final PairProcessor<T, T> removeProcessor) {
   if (files.isEmpty()) return files;
   final TreeMap<String, T> paths = new TreeMap<String, T>();
   for (T file : files) {
     final String path = convertor.convert(file);
     assert path != null;
     final String canonicalPath = toCanonicalPath(path);
     paths.put(canonicalPath, file);
   }
   final List<Map.Entry<String, T>> ordered =
       new ArrayList<Map.Entry<String, T>>(paths.entrySet());
   final List<T> result = new ArrayList<T>(ordered.size());
   result.add(ordered.get(0).getValue());
   for (int i = 1; i < ordered.size(); i++) {
     final Map.Entry<String, T> entry = ordered.get(i);
     final String child = entry.getKey();
     boolean parentNotFound = true;
     for (int j = i - 1; j >= 0; j--) {
       // possible parents
       final String parent = ordered.get(j).getKey();
       if (parent == null) continue;
       if (startsWith(child, parent)
           && removeProcessor.process(ordered.get(j).getValue(), entry.getValue())) {
         parentNotFound = false;
         break;
       }
     }
     if (parentNotFound) {
       result.add(entry.getValue());
     }
   }
   return result;
 }
 public static void assertNotEmpty(final Collection<?> collection) {
   if (collection == null) return;
   assertTrue(!collection.isEmpty());
 }
  @NotNull
  @Override
  public List<TimedVcsCommit> getCommitsMatchingFilter(
      @NotNull final VirtualFile root,
      @NotNull VcsLogFilterCollection filterCollection,
      int maxCount)
      throws VcsException {
    if (!isRepositoryReady(root)) {
      return Collections.emptyList();
    }

    List<String> filterParameters = ContainerUtil.newArrayList();

    if (filterCollection.getBranchFilter() != null
        && !filterCollection.getBranchFilter().getBranchNames().isEmpty()) {
      GitRepository repository = getRepository(root);
      assert repository != null
          : "repository is null for root " + root + " but was previously reported as 'ready'";

      boolean atLeastOneBranchExists = false;
      for (String branchName : filterCollection.getBranchFilter().getBranchNames()) {
        if (branchName.equals("HEAD")
            || repository.getBranches().findBranchByName(branchName) != null) {
          filterParameters.add(branchName);
          atLeastOneBranchExists = true;
        }
      }
      if (!atLeastOneBranchExists) { // no such branches in this repository => filter matches
        // nothing
        return Collections.emptyList();
      }
    } else {
      filterParameters.addAll(GitHistoryUtils.LOG_ALL);
    }

    if (filterCollection.getUserFilter() != null) {
      String authorFilter =
          StringUtil.join(
              ContainerUtil.map(
                  filterCollection.getUserFilter().getUserNames(root), UserNameRegex.INSTANCE),
              "|");
      filterParameters.add(prepareParameter("author", StringUtil.escapeBackSlashes(authorFilter)));
      filterParameters.add(
          "--extended-regexp"); // extended regexp required for correctly filtering user names
    }

    if (filterCollection.getDateFilter() != null) {
      // assuming there is only one date filter, until filter expressions are defined
      VcsLogDateFilter filter = filterCollection.getDateFilter();
      if (filter.getAfter() != null) {
        filterParameters.add(prepareParameter("after", filter.getAfter().toString()));
      }
      if (filter.getBefore() != null) {
        filterParameters.add(prepareParameter("before", filter.getBefore().toString()));
      }
    }

    if (filterCollection.getTextFilter() != null) {
      String textFilter = filterCollection.getTextFilter().getText();
      filterParameters.add(prepareParameter("grep", StringUtil.escapeChars(textFilter, '[', ']')));
    }

    filterParameters.add(
        "--regexp-ignore-case"); // affects case sensitivity of any filter (except file filter)
    if (maxCount > 0) {
      filterParameters.add(prepareParameter("max-count", String.valueOf(maxCount)));
    }

    // note: structure filter must be the last parameter, because it uses "--" which separates
    // parameters from paths
    if (filterCollection.getStructureFilter() != null) {
      Collection<VirtualFile> files = filterCollection.getStructureFilter().getFiles();
      if (!files.isEmpty()) {
        filterParameters.add("--full-history");
        filterParameters.add("--simplify-merges");
        filterParameters.add("--");
        for (VirtualFile file : files) {
          filterParameters.add(file.getPath());
        }
      }
    }

    List<TimedVcsCommit> commits = ContainerUtil.newArrayList();
    GitHistoryUtils.readCommits(
        myProject,
        root,
        filterParameters,
        EmptyConsumer.<VcsUser>getInstance(),
        EmptyConsumer.<VcsRef>getInstance(),
        new CollectConsumer<TimedVcsCommit>(commits));
    return commits;
  }