'Set a refname to new_ref only if it currently equals old_ref.
This method follows all symbolic references, and can be used to perform
an atomic compare-and-swap operation.
:param name: The refname to set.
:param old_ref: The old sha the refname must refer to, or None to set
unconditionally.
:param new_ref: The new sha the refname will refer to.
:return: True if the set was successful, False otherwise.'

def set_if_equals(self, name, old_ref, new_ref):

    self._check_refname(name)
    try:
        (realnames, _) = self.follow(name)
        realname = realnames[(-1)]
    except (KeyError, IndexError):
        realname = name
    filename = self.refpath(realname)
    ensure_dir_exists(os.path.dirname(filename))
    with GitFile(filename, 'wb') as f:
        if (old_ref is not None):
            try:
                orig_ref = self.read_loose_ref(realname)
                if (orig_ref is None):
                    orig_ref = self.get_packed_refs().get(realname, ZERO_SHA)
                if (orig_ref != old_ref):
                    f.abort()
                    return False
            except (OSError, IOError):
                f.abort()
                raise
        try:
            f.write((new_ref + '\n'))
        except (OSError, IOError):
            f.abort()
            raise
    return True
