import copy


def override_repo_refs(repos, override_ref=None, overrides=None):
    """
    Apply ref overrides to the `repos` dictionary.

    Arguments:
        repos (dict): A dict mapping Repository objects to openedx.yaml data.
        override_ref (str): a ref to use in all repos.
        overrides (dict mapping repo names to refs): refs to use in specific repos.

    Returns:
        A new dict mapping Repository objects to openedx.yaml data, with refs overridden.

    """
    repos = {r: copy.deepcopy(data) for r, data in repos.items()}
    overrides = overrides or {}
    if override_ref or overrides:
        for repo, repo_data in repos.items():
            local_override = overrides.get(repo.full_name, override_ref)
            if local_override:
                repo_data["openedx-release"]["ref"] = local_override
    return repos