Try to find a revision (commit hash) for the ref at 'remote' repo.

    Once you have the revision (commit hash), you can check it out. Of
    course, you may have to fetch it first.

    Note: Borrowed these ideas from Chef

        https://github.com/chef/chef/blob/master/lib/chef/provider/git.rb

    Returns None if no revision is found.

def git_remote_resolve_reference(repo_dir, ref, remote='origin'):
    """Try to find a revision (commit hash) for the ref at 'remote' repo.

    Once you have the revision (commit hash), you can check it out. Of
    course, you may have to fetch it first.

    Note: Borrowed these ideas from Chef

        https://github.com/chef/chef/blob/master/lib/chef/provider/git.rb

    Returns None if no revision is found.
    """
    ls_refs = git_ls_remote(repo_dir, remote=remote, refs='%s*' % ref)

    if ref == 'HEAD':
        return ls_refs['HEAD']
    else:
        matching_refs = [
            'refs/tags/%s^{}' % ref,
            'refs/heads/%s^{}' % ref,
            '%s^{}' % ref,
            'refs/tags/%s' % ref,
            'refs/heads/%s' % ref,
            ref,
        ]
        for _ref in matching_refs:
            if _ref in ls_refs:
                return ls_refs[_ref]