prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Allow to `mamba install` environments
  Thanks for maintaining pre-commit, we really enjoying working with it!
  
  When installing conda environments, [mamba](https://github.com/mamba-org/mamba) is often the preferred option, as it is faster and [requires less memory](https://github.com/conda/conda/issues/5003) when solving an environment.
  
  As of today, pre-commit uses conda for creating environments, as this is hard-coded into the source code:
  
  https://github.com/pre-commit/pre-commit/blob/b944395d6638629a93756ed7279b4368473177af/pre_commit/languages/conda.py#L62-L66
  
  We have a use case, where we want to install pre-commit hooks in CI, but the installation fails because of out-of-memory errors. Installing the same environment with mamba however, succeeds.
  
  It would be nice if one could define whether pre-commit uses conda or mamba for installing environments. This could for example be accomplished by introducing an environment variable `PRE_COMMIT_USE_MAMBA`. If there is a better way to pass such configuration to pre-commit, please let me know.
  
  I'd be glad to work on this feature, if this is something you'd like to see added to pre-commit.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pre_commit/languages/conda.py`
  Content:
  ```
  1 import contextlib
  2 import os
  3 from typing import Generator
  4 from typing import Sequence
  5 from typing import Tuple
  6 
  7 from pre_commit.envcontext import envcontext
  8 from pre_commit.envcontext import PatchesT
  9 from pre_commit.envcontext import SubstitutionT
  10 from pre_commit.envcontext import UNSET
  11 from pre_commit.envcontext import Var
  12 from pre_commit.hook import Hook
  13 from pre_commit.languages import helpers
  14 from pre_commit.prefix import Prefix
  15 from pre_commit.util import clean_path_on_failure
  16 from pre_commit.util import cmd_output_b
  17 
  18 ENVIRONMENT_DIR = 'conda'
  19 get_default_version = helpers.basic_get_default_version
  20 healthy = helpers.basic_healthy
  21 
  22 
  23 def get_env_patch(env: str) -> PatchesT:
  24     # On non-windows systems executable live in $CONDA_PREFIX/bin, on Windows
  25     # they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,
  26     # $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only
  27     # seems to be used for python.exe.
  28     path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
  29     if os.name == 'nt':  # pragma: no cover (platform specific)
  30         path = (env, os.pathsep, *path)
  31         path = (os.path.join(env, 'Scripts'), os.pathsep, *path)
  32         path = (os.path.join(env, 'Library', 'bin'), os.pathsep, *path)
  33 
  34     return (
  35         ('PYTHONHOME', UNSET),
  36         ('VIRTUAL_ENV', UNSET),
  37         ('CONDA_PREFIX', env),
  38         ('PATH', path),
  39     )
  40 
  41 
  42 @contextlib.contextmanager
  43 def in_env(
  44         prefix: Prefix,
  45         language_version: str,
  46 ) -> Generator[None, None, None]:
  47     directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)
  48     envdir = prefix.path(directory)
  49     with envcontext(get_env_patch(envdir)):
  50         yield
  51 
  52 
  53 def install_environment(
  54         prefix: Prefix,
  55         version: str,
  56         additional_dependencies: Sequence[str],
  57 ) -> None:
  58     helpers.assert_version_default('conda', version)
  59     directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
  60 
  61     env_dir = prefix.path(directory)
  62     with clean_path_on_failure(env_dir):
  63         cmd_output_b(
  64             'conda', 'env', 'create', '-p', env_dir, '--file',
  65             'environment.yml', cwd=prefix.prefix_dir,
  66         )
  67         if additional_dependencies:
  68             cmd_output_b(
  69                 'conda', 'install', '-p', env_dir, *additional_dependencies,
  70                 cwd=prefix.prefix_dir,
  71             )
  72 
  73 
  74 def run_hook(
  75         hook: Hook,
  76         file_args: Sequence[str],
  77         color: bool,
  78 ) -> Tuple[int, bytes]:
  79     # TODO: Some rare commands need to be run using `conda run` but mostly we
  80     #       can run them without which is much quicker and produces a better
  81     #       output.
  82     # cmd = ('conda', 'run', '-p', env_dir) + hook.cmd
  83     with in_env(hook.prefix, hook.language_version):
  84         return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
  85
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pre_commit/languages/conda.py b/pre_commit/languages/conda.py
  --- a/pre_commit/languages/conda.py
  +++ b/pre_commit/languages/conda.py
  @@ -50,6 +50,15 @@
           yield
   
   
  +def _conda_exe() -> str:
  +    if os.environ.get('PRE_COMMIT_USE_MICROMAMBA'):
  +        return 'micromamba'
  +    elif os.environ.get('PRE_COMMIT_USE_MAMBA'):
  +        return 'mamba'
  +    else:
  +        return 'conda'
  +
  +
   def install_environment(
           prefix: Prefix,
           version: str,
  @@ -58,15 +67,17 @@
       helpers.assert_version_default('conda', version)
       directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
   
  +    conda_exe = _conda_exe()
  +
       env_dir = prefix.path(directory)
       with clean_path_on_failure(env_dir):
           cmd_output_b(
  -            'conda', 'env', 'create', '-p', env_dir, '--file',
  +            conda_exe, 'env', 'create', '-p', env_dir, '--file',
               'environment.yml', cwd=prefix.prefix_dir,
           )
           if additional_dependencies:
               cmd_output_b(
  -                'conda', 'install', '-p', env_dir, *additional_dependencies,
  +                conda_exe, 'install', '-p', env_dir, *additional_dependencies,
                   cwd=prefix.prefix_dir,
               )
  
verification_info: {"golden_diff": "diff --git a/pre_commit/languages/conda.py b/pre_commit/languages/conda.py\n--- a/pre_commit/languages/conda.py\n+++ b/pre_commit/languages/conda.py\n@@ -50,6 +50,15 @@\n         yield\n \n \n+def _conda_exe() -> str:\n+    if os.environ.get('PRE_COMMIT_USE_MICROMAMBA'):\n+        return 'micromamba'\n+    elif os.environ.get('PRE_COMMIT_USE_MAMBA'):\n+        return 'mamba'\n+    else:\n+        return 'conda'\n+\n+\n def install_environment(\n         prefix: Prefix,\n         version: str,\n@@ -58,15 +67,17 @@\n     helpers.assert_version_default('conda', version)\n     directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n \n+    conda_exe = _conda_exe()\n+\n     env_dir = prefix.path(directory)\n     with clean_path_on_failure(env_dir):\n         cmd_output_b(\n-            'conda', 'env', 'create', '-p', env_dir, '--file',\n+            conda_exe, 'env', 'create', '-p', env_dir, '--file',\n             'environment.yml', cwd=prefix.prefix_dir,\n         )\n         if additional_dependencies:\n             cmd_output_b(\n-                'conda', 'install', '-p', env_dir, *additional_dependencies,\n+                conda_exe, 'install', '-p', env_dir, *additional_dependencies,\n                 cwd=prefix.prefix_dir,\n             )\n", "issue": "Allow to `mamba install` environments\nThanks for maintaining pre-commit, we really enjoying working with it!\r\n\r\nWhen installing conda environments, [mamba](https://github.com/mamba-org/mamba) is often the preferred option, as it is faster and [requires less memory](https://github.com/conda/conda/issues/5003) when solving an environment.\r\n\r\nAs of today, pre-commit uses conda for creating environments, as this is hard-coded into the source code:\r\n\r\nhttps://github.com/pre-commit/pre-commit/blob/b944395d6638629a93756ed7279b4368473177af/pre_commit/languages/conda.py#L62-L66\r\n\r\nWe have a use case, where we want to install pre-commit hooks in CI, but the installation fails because of out-of-memory errors. Installing the same environment with mamba however, succeeds.\r\n\r\nIt would be nice if one could define whether pre-commit uses conda or mamba for installing environments. This could for example be accomplished by introducing an environment variable `PRE_COMMIT_USE_MAMBA`. If there is a better way to pass such configuration to pre-commit, please let me know.\r\n\r\nI'd be glad to work on this feature, if this is something you'd like to see added to pre-commit.\n", "before_files": [{"content": "import contextlib\nimport os\nfrom typing import Generator\nfrom typing import Sequence\nfrom typing import Tuple\n\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import PatchesT\nfrom pre_commit.envcontext import SubstitutionT\nfrom pre_commit.envcontext import UNSET\nfrom pre_commit.envcontext import Var\nfrom pre_commit.hook import Hook\nfrom pre_commit.languages import helpers\nfrom pre_commit.prefix import Prefix\nfrom pre_commit.util import clean_path_on_failure\nfrom pre_commit.util import cmd_output_b\n\nENVIRONMENT_DIR = 'conda'\nget_default_version = helpers.basic_get_default_version\nhealthy = helpers.basic_healthy\n\n\ndef get_env_patch(env: str) -> PatchesT:\n    # On non-windows systems executable live in $CONDA_PREFIX/bin, on Windows\n    # they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,\n    # $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only\n    # seems to be used for python.exe.\n    path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))\n    if os.name == 'nt':  # pragma: no cover (platform specific)\n        path = (env, os.pathsep, *path)\n        path = (os.path.join(env, 'Scripts'), os.pathsep, *path)\n        path = (os.path.join(env, 'Library', 'bin'), os.pathsep, *path)\n\n    return (\n        ('PYTHONHOME', UNSET),\n        ('VIRTUAL_ENV', UNSET),\n        ('CONDA_PREFIX', env),\n        ('PATH', path),\n    )\n\n\n@contextlib.contextmanager\ndef in_env(\n        prefix: Prefix,\n        language_version: str,\n) -> Generator[None, None, None]:\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)\n    envdir = prefix.path(directory)\n    with envcontext(get_env_patch(envdir)):\n        yield\n\n\ndef install_environment(\n        prefix: Prefix,\n        version: str,\n        additional_dependencies: Sequence[str],\n) -> None:\n    helpers.assert_version_default('conda', version)\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n\n    env_dir = prefix.path(directory)\n    with clean_path_on_failure(env_dir):\n        cmd_output_b(\n            'conda', 'env', 'create', '-p', env_dir, '--file',\n            'environment.yml', cwd=prefix.prefix_dir,\n        )\n        if additional_dependencies:\n            cmd_output_b(\n                'conda', 'install', '-p', env_dir, *additional_dependencies,\n                cwd=prefix.prefix_dir,\n            )\n\n\ndef run_hook(\n        hook: Hook,\n        file_args: Sequence[str],\n        color: bool,\n) -> Tuple[int, bytes]:\n    # TODO: Some rare commands need to be run using `conda run` but mostly we\n    #       can run them without which is much quicker and produces a better\n    #       output.\n    # cmd = ('conda', 'run', '-p', env_dir) + hook.cmd\n    with in_env(hook.prefix, hook.language_version):\n        return helpers.run_xargs(hook, hook.cmd, file_args, color=color)\n", "path": "pre_commit/languages/conda.py"}], "after_files": [{"content": "import contextlib\nimport os\nfrom typing import Generator\nfrom typing import Sequence\nfrom typing import Tuple\n\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import PatchesT\nfrom pre_commit.envcontext import SubstitutionT\nfrom pre_commit.envcontext import UNSET\nfrom pre_commit.envcontext import Var\nfrom pre_commit.hook import Hook\nfrom pre_commit.languages import helpers\nfrom pre_commit.prefix import Prefix\nfrom pre_commit.util import clean_path_on_failure\nfrom pre_commit.util import cmd_output_b\n\nENVIRONMENT_DIR = 'conda'\nget_default_version = helpers.basic_get_default_version\nhealthy = helpers.basic_healthy\n\n\ndef get_env_patch(env: str) -> PatchesT:\n    # On non-windows systems executable live in $CONDA_PREFIX/bin, on Windows\n    # they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,\n    # $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only\n    # seems to be used for python.exe.\n    path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))\n    if os.name == 'nt':  # pragma: no cover (platform specific)\n        path = (env, os.pathsep, *path)\n        path = (os.path.join(env, 'Scripts'), os.pathsep, *path)\n        path = (os.path.join(env, 'Library', 'bin'), os.pathsep, *path)\n\n    return (\n        ('PYTHONHOME', UNSET),\n        ('VIRTUAL_ENV', UNSET),\n        ('CONDA_PREFIX', env),\n        ('PATH', path),\n    )\n\n\n@contextlib.contextmanager\ndef in_env(\n        prefix: Prefix,\n        language_version: str,\n) -> Generator[None, None, None]:\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)\n    envdir = prefix.path(directory)\n    with envcontext(get_env_patch(envdir)):\n        yield\n\n\ndef _conda_exe() -> str:\n    if os.environ.get('PRE_COMMIT_USE_MICROMAMBA'):\n        return 'micromamba'\n    elif os.environ.get('PRE_COMMIT_USE_MAMBA'):\n        return 'mamba'\n    else:\n        return 'conda'\n\n\ndef install_environment(\n        prefix: Prefix,\n        version: str,\n        additional_dependencies: Sequence[str],\n) -> None:\n    helpers.assert_version_default('conda', version)\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n\n    conda_exe = _conda_exe()\n\n    env_dir = prefix.path(directory)\n    with clean_path_on_failure(env_dir):\n        cmd_output_b(\n            conda_exe, 'env', 'create', '-p', env_dir, '--file',\n            'environment.yml', cwd=prefix.prefix_dir,\n        )\n        if additional_dependencies:\n            cmd_output_b(\n                conda_exe, 'install', '-p', env_dir, *additional_dependencies,\n                cwd=prefix.prefix_dir,\n            )\n\n\ndef run_hook(\n        hook: Hook,\n        file_args: Sequence[str],\n        color: bool,\n) -> Tuple[int, bytes]:\n    # TODO: Some rare commands need to be run using `conda run` but mostly we\n    #       can run them without which is much quicker and produces a better\n    #       output.\n    # cmd = ('conda', 'run', '-p', env_dir) + hook.cmd\n    with in_env(hook.prefix, hook.language_version):\n        return helpers.run_xargs(hook, hook.cmd, file_args, color=color)\n", "path": "pre_commit/languages/conda.py"}]}