def clean_stale_refs(self, local_refs=None):  # pylint: disable=arguments-differ
        '''
        Clean stale local refs so they don't appear as fileserver environments
        '''
        if self.credentials is not None:
            log.debug(
                'pygit2 does not support detecting stale refs for '
                'authenticated remotes, saltenvs will not reflect '
                'branches/tags removed from remote \'{0}\''
                .format(self.id)
            )
            return []
        if local_refs is None:
            local_refs = self.repo.listall_references()
        remote_refs = []
        cmd_str = 'git ls-remote origin'
        cmd = subprocess.Popen(
            shlex.split(cmd_str),
            close_fds=not salt.utils.is_windows(),
            cwd=self.repo.workdir,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
        output = cmd.communicate()[0]
        if cmd.returncode != 0:
            log.warning(
                'Failed to list remote references for {0} remote \'{1}\'. '
                'Output from \'{2}\' follows:\n{3}'.format(
                    self.role,
                    self.id,
                    cmd_str,
                    output
                )
            )
            return []
        for line in salt.utils.itertools.split(output, '\n'):
            try:
                # Rename heads to match the remote ref names from
                # pygit2.Repository.listall_references()
                remote_refs.append(
                    line.split()[-1].replace(b'refs/heads/',
                                             b'refs/remotes/origin/')
                )
            except __HOLE__:
                continue
        cleaned = []
        if remote_refs:
            for ref in local_refs:
                if ref.startswith('refs/heads/'):
                    # Local head, ignore it
                    continue
                elif ref not in remote_refs:
                    self.repo.lookup_reference(ref).delete()
                    cleaned.append(ref)
        if cleaned:
            log.debug('{0} cleaned the following stale refs: {1}'
                      .format(self.role, cleaned))
        return cleaned
dataset/ETHPy150Open saltstack/salt/salt/utils/gitfs.py/Pygit2.clean_stale_refs