def exclude_ancestor_filediffs(to_filter, all_filediffs=None):
    """Exclude all ancestor FileDiffs from the given list and return the rest.

    A :pyclass:`~reviewboard.diffviewer.models.filediff.FileDiff` is considered
    an ancestor of another if it occurs in a previous commit and modifies the
    same file.

    As a result, only the most recent (commit-wise) FileDiffs that modify a
    given file will be included in the result.

    Args:
        to_filter (list of reviewboard.diffviewer.models.filediff.FileDiff):
            The FileDiffs to filter.

        all_filediffs (list of reviewboard.diffviewer.models.filediff.FileDiff,
                       optional):
            The list of all FileDiffs in the :py:class:`~reviewboard.
            diffviewer.models.diffset.DiffSet>`.

            If not provided, it is assumed that ``to_filter`` is a list of all
            FileDiffs in the :py:class:`~reviewboard.diffviewer.models.
            diffset.DiffSet>`.

    Returns:
        list of reviewboard.diffviewer.models.filediff.FileDiff:
        The FileDiffs that are not ancestors of other FileDiffs.
    """
    if all_filediffs is None:
        all_filediffs = to_filter

    ancestor_pks = {
        ancestor.pk
        for filediff in to_filter
        for ancestor in filediff.get_ancestors(minimal=False,
                                               filediffs=all_filediffs)
    }

    return [
        filediff
        for filediff in to_filter
        if filediff.pk not in ancestor_pks
    ]