def with_rw_and_rw_remote_repo(working_tree_ref):
    """Same as with_rw_repo, but also provides a writable remote repository from which
    the rw_repo has been forked as well as a handle for a git-daemon that may be started
    to run the remote_repo.

    The remote repository was cloned as bare repository from the ro repo, whereas the rw
    repo has a working tree and was cloned from the remote repository.

    remote_repo has two remotes: origin and daemon_origin. One uses a local url, the
    other uses a server url. The daemon setup must be done on system level and should be
    an inetd service that serves tempdir.gettempdir() and all directories in it.

    The following sketch demonstrates this::

        rorepo ---<bare clone>---> rw_remote_repo ---<clone>---> rw_repo

    The test case needs to support the following signature::

        def case(self, rw_repo, rw_daemon_repo)

    This setup allows you to test push and pull scenarios and hooks nicely.

    See working dir info in :func:`with_rw_repo`.

    :note: We attempt to launch our own invocation of git-daemon, which will be shut
        down at the end of the test.
    """
    from git import Git, Remote  # To avoid circular deps.

    assert isinstance(working_tree_ref, str), "Decorator requires ref name for working tree checkout"

    def argument_passer(func):
        @wraps(func)
        def remote_repo_creator(self):
            rw_daemon_repo_dir = tempfile.mktemp(prefix="daemon_repo-%s-" % func.__name__)
            rw_repo_dir = tempfile.mktemp(prefix="daemon_cloned_repo-%s-" % func.__name__)

            rw_daemon_repo = self.rorepo.clone(rw_daemon_repo_dir, shared=True, bare=True)
            # Recursive alternates info?
            rw_repo = rw_daemon_repo.clone(rw_repo_dir, shared=True, bare=False, n=True)
            try:
                rw_repo.head.commit = working_tree_ref
                rw_repo.head.reference.checkout()

                # Prepare for git-daemon.
                rw_daemon_repo.daemon_export = True

                # This thing is just annoying!
                with rw_daemon_repo.config_writer() as crw:
                    section = "daemon"
                    try:
                        crw.add_section(section)
                    except Exception:
                        pass
                    crw.set(section, "receivepack", True)

                # Initialize the remote - first do it as local remote and pull, then
                # we change the url to point to the daemon.
                d_remote = Remote.create(rw_repo, "daemon_origin", rw_daemon_repo_dir)
                d_remote.fetch()

                base_daemon_path, rel_repo_dir = osp.split(rw_daemon_repo_dir)

                remote_repo_url = Git.polish_url("git://localhost:%s/%s" % (GIT_DAEMON_PORT, rel_repo_dir))
                with d_remote.config_writer as cw:
                    cw.set("url", remote_repo_url)

                with git_daemon_launched(
                    Git.polish_url(base_daemon_path),
                    "127.0.0.1",
                    GIT_DAEMON_PORT,
                ):
                    # Try listing remotes, to diagnose whether the daemon is up.
                    rw_repo.git.ls_remote(d_remote)

                    with cwd(rw_repo.working_dir):
                        try:
                            return func(self, rw_repo, rw_daemon_repo)
                        except:  # noqa: E722 B001
                            _logger.info(
                                "Keeping repos after failure: \n  rw_repo_dir: %s \n  rw_daemon_repo_dir: %s",
                                rw_repo_dir,
                                rw_daemon_repo_dir,
                            )
                            rw_repo_dir = rw_daemon_repo_dir = None
                            raise

            finally:
                rw_repo.git.clear_cache()
                rw_daemon_repo.git.clear_cache()
                del rw_repo
                del rw_daemon_repo
                gc.collect()
                gitdb.util.mman.collect()
                gc.collect()
                if rw_repo_dir:
                    rmtree(rw_repo_dir)
                if rw_daemon_repo_dir:
                    rmtree(rw_daemon_repo_dir)
            # END cleanup

        # END bare repo creator
        return remote_repo_creator
        # END remote repo creator

    # END argument parser

    return argument_passer
Same as with_rw_repo, but also provides a writable remote repository from which
    the rw_repo has been forked as well as a handle for a git-daemon that may be started
    to run the remote_repo.

    The remote repository was cloned as bare repository from the ro repo, whereas the rw
    repo has a working tree and was cloned from the remote repository.

    remote_repo has two remotes: origin and daemon_origin. One uses a local url, the
    other uses a server url. The daemon setup must be done on system level and should be
    an inetd service that serves tempdir.gettempdir() and all directories in it.

    The following sketch demonstrates this::

        rorepo ---<bare clone>---> rw_remote_repo ---<clone>---> rw_repo

    The test case needs to support the following signature::

        def case(self, rw_repo, rw_daemon_repo)

    This setup allows you to test push and pull scenarios and hooks nicely.

    See working dir info in :func:`with_rw_repo`.

    :note: We attempt to launch our own invocation of git-daemon, which will be shut
        down at the end of the test.
    
test/lib/helper.py