def diff(from_, to):
    """
    Compare two viewfiles and return the difference between the two.

    Returns:
      A dictionary where the keys are directories that have changed and the
      values are a tuple containing the old and new viewfile entries.
    """
    # Create dictionaries of entries (indexed by dir)
    from_dict = {entry.dir: entry for entry in from_.entries}
    to_dict = {entry.dir: entry for entry in to.entries}

    # Find the directories that exist in either viewfile.
    all_dirs = set(from_dict).union(set(to_dict))

    # Find directories that have changed
    changes = {
        dir: (from_dict.get(dir), to_dict.get(dir))
        for dir in all_dirs
        if from_dict.get(dir) != to_dict.get(dir)
    }
    return changes