def full_ref(repo, ref):
    """Generate a fully qualified git reference

    If ref refers to a branch or tag, the function will return the correct
    fully qualified reference, otherwise it returns the ref as provided.

    e.g. for ref 'master', return 'refs/heads/master'

    Parameters
    ----------
    repo : dulwich.repo.Repo

    Returns
    -------
    str
    """
    refs = repo.refs.keys()
    for ref_type in ['refs/heads/', 'refs/tags/']:
        full_ref = ref_type + ref
        if bytes(full_ref, encoding='ascii') in refs:
            ref = full_ref
    return ref
