<commit_before>"""Fixtures for exercising scenarios with real Git."""
# =============================================================================
# CONTENTS
# -----------------------------------------------------------------------------
# phlgitu_fixture
#
# Public Classes:
#   TempRepo
#    .close
#    .repo
#   Worker
#    .create_new_file
#    .add_new_file
#    .commit_new_file
#    .commit_new_file_on_new_branch
#    .append_to_file
#    .add_append_to_file
#    .commit_append_to_file
#    .append_to_file_on_new_branch
#    .checkout_master
#    .repo
#
# Public Functions:
#   lone_worker_context
#   temprepo_context
#
# -----------------------------------------------------------------------------
# (this contents block is generated, edits will be lost)
# =============================================================================

from __future__ import absolute_import

import contextlib
import os
import shutil
import tempfile

import phlsys_git


@contextlib.contextmanager
def lone_worker_context():
    """Return a newly attached Worker with initial commit, close when expired.

    Usage examples:

        Create a temporary repo and attached worker:
        >>> with lone_worker_context() as worker:
        ...     content = worker.repo("show", "HEAD:README")
        ...     content is not None
        True

    """
    with temprepo_context() as repo:
        worker = Worker(repo)
        worker.commit_new_file('initial commit', 'README')
        yield worker


@contextlib.contextmanager
def temprepo_context():
    """Return a newly created phlsys_git.Repo, close when expired.

    Usage examples:

        Create a temporary repo:
        >>> with temprepo_context() as clone:
        ...     status = clone("rev-parse", "--is-inside-work-tree")
        ...     status.strip().lower() == 'true'
        True

    """
    with contextlib.closing(TempRepo()) as temp_repo:
        yield temp_repo.repo


class TempRepo(object):

    """Make a temporary repository, clean up on close().

    Usage examples:

        Create a temporary repo:
        >>> with temprepo_context() as repo:
        ...     status = repo("rev-parse", "--is-inside-work-tree")
        ...     status.strip().lower() == 'true'
        True

    """

    def __init__(self):
        super(TempRepo, self).__init__()
        self._tmp_dir = tempfile.mkdtemp()
        self._repo = phlsys_git.Repo(self._tmp_dir)
        self._repo("init")

    def close(self):
        """Free up any resources used by this instance.
<commit_msg>phlgitu_fixture: rename 'clone' to 'repo'<commit_after>"""Fixtures for exercising scenarios with real Git."""
# =============================================================================
# CONTENTS
# -----------------------------------------------------------------------------
# phlgitu_fixture
#
# Public Classes:
#   TempRepo
#    .close
#    .repo
#   Worker
#    .create_new_file
#    .add_new_file
#    .commit_new_file
#    .commit_new_file_on_new_branch
#    .append_to_file
#    .add_append_to_file
#    .commit_append_to_file
#    .append_to_file_on_new_branch
#    .checkout_master
#    .repo
#
# Public Functions:
#   lone_worker_context
#   temprepo_context
#
# -----------------------------------------------------------------------------
# (this contents block is generated, edits will be lost)
# =============================================================================

from __future__ import absolute_import

import contextlib
import os
import shutil
import tempfile

import phlsys_git


@contextlib.contextmanager
def lone_worker_context():
    """Return a newly attached Worker with initial commit, close when expired.

    Usage examples:

        Create a temporary repo and attached worker:
        >>> with lone_worker_context() as worker:
        ...     content = worker.repo("show", "HEAD:README")
        ...     content is not None
        True

    """
    with temprepo_context() as repo:
        worker = Worker(repo)
        worker.commit_new_file('initial commit', 'README')
        yield worker


@contextlib.contextmanager
def temprepo_context():
    """Return a newly created phlsys_git.Repo, close when expired.

    Usage examples:

        Create a temporary repo:
        >>> with temprepo_context() as repo:
        ...     status = repo("rev-parse", "--is-inside-work-tree")
        ...     status.strip().lower() == 'true'
        True

    """
    with contextlib.closing(TempRepo()) as temp_repo:
        yield temp_repo.repo


class TempRepo(object):

    """Make a temporary repository, clean up on close().

    Usage examples:

        Create a temporary repo:
        >>> with temprepo_context() as repo:
        ...     status = repo("rev-parse", "--is-inside-work-tree")
        ...     status.strip().lower() == 'true'
        True

    """

    def __init__(self):
        super(TempRepo, self).__init__()
        self._tmp_dir = tempfile.mkdtemp()
        self._repo = phlsys_git.Repo(self._tmp_dir)
        self._repo("init")

    def close(self):
        """Free up any resources used by this instance.

{'fraction_non_alphanumeric': 0.21859099804305285, 'fraction_numerical': 0.0, 'mean_word_length': 2.265814696485623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4408142', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1383, 'n_words': 356}