def get_latest_commit_for_ref(repo, ref):
    """
    Given a repo name and a ref in that repo, return some information about
    the commit that the ref refers to. This function is called by
    commit_ref_info(), and it returns information in the same structure.
    """
    # is it a branch?
    branch = repo.branch(ref)
    if branch:
        commit = branch.commit.commit
        return {
            "ref": ref,
            "ref_type": "branch",
            "sha": branch.commit.sha,
            "message": commit.message,
            "author": commit.author,
            "committer": commit.committer,
        }

    try:
        tag = repo.ref('tags/{}'.format(ref))
    except TypeError as err:
        # GitHub unfortunately returns a list of partial matches if you ask for
        # a ref that doesn't exist.  This means the code in github3 that
        # expects a dict (the ref that matches) actually gets a list instead.
        # Then it tries to pop, and a TypeError occurs
        # https://github.com/sigmavirus24/github3.py/issues/310
        # We'll catch the error and make the problem clearer.
        if "pop() takes at most 1 argument (2 given)" in str(err):
            raise Exception("In repo {}, ref {!r} doesn't exist.".format(repo, ref))
        else:
            raise
    if tag:
        if tag.object.type == "tag":
            # An annotated tag, one more level of indirection.
            tag = repo.tag(tag.object.sha)
        # need to do a subsequent API call to get the tagged commit
        commit = repo.commit(tag.object.sha)
        if commit:
            return {
                "ref": ref,
                "ref_type": "tag",
                "sha": commit.sha,
                "message": commit.commit.message,
                "author": commit.commit.author,
                "committer": commit.commit.committer,
            }

    msg = "No commit for {ref} in {repo}".format(
        ref=ref, repo=repo.full_name,
    )
    raise ValueError(msg)