prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  FileExistsError : [WinError 183] Cannot create a file when that file already exists
  version : 1.15.2
  OS : Windows 10
  
  ```console
  $ pre-commit install
  
  An unexpected error has occurred: FileExistsError: [WinError 183] Cannot create a file when that file already exists: '.git\\hooks\\pre-commit' -> '.git\\hooks\\pre-commit.legacy'
  ```
  
  when i tried second times this was the error.
  
  --- 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/commands/install_uninstall.py`
  Content:
  ```
  1 from __future__ import print_function
  2 from __future__ import unicode_literals
  3 
  4 import io
  5 import itertools
  6 import logging
  7 import os.path
  8 import sys
  9 
  10 from pre_commit import git
  11 from pre_commit import output
  12 from pre_commit.clientlib import load_config
  13 from pre_commit.repository import all_hooks
  14 from pre_commit.repository import install_hook_envs
  15 from pre_commit.util import cmd_output
  16 from pre_commit.util import make_executable
  17 from pre_commit.util import mkdirp
  18 from pre_commit.util import resource_text
  19 
  20 
  21 logger = logging.getLogger(__name__)
  22 
  23 # This is used to identify the hook file we install
  24 PRIOR_HASHES = (
  25     '4d9958c90bc262f47553e2c073f14cfe',
  26     'd8ee923c46731b42cd95cc869add4062',
  27     '49fd668cb42069aa1b6048464be5d395',
  28     '79f09a650522a87b0da915d0d983b2de',
  29     'e358c9dae00eac5d06b38dfdb1e33a8c',
  30 )
  31 CURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03'
  32 TEMPLATE_START = '# start templated\n'
  33 TEMPLATE_END = '# end templated\n'
  34 
  35 
  36 def _hook_paths(hook_type):
  37     pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)
  38     return pth, '{}.legacy'.format(pth)
  39 
  40 
  41 def is_our_script(filename):
  42     if not os.path.exists(filename):  # pragma: windows no cover (symlink)
  43         return False
  44     with io.open(filename) as f:
  45         contents = f.read()
  46     return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
  47 
  48 
  49 def shebang():
  50     if sys.platform == 'win32':
  51         py = 'python'
  52     else:
  53         # Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
  54         path_choices = [p for p in os.defpath.split(os.pathsep) if p]
  55         exe_choices = [
  56             'python{}'.format('.'.join(str(v) for v in sys.version_info[:i]))
  57             for i in range(3)
  58         ]
  59         for path, exe in itertools.product(path_choices, exe_choices):
  60             if os.path.exists(os.path.join(path, exe)):
  61                 py = exe
  62                 break
  63         else:
  64             py = 'python'
  65     return '#!/usr/bin/env {}'.format(py)
  66 
  67 
  68 def install(
  69         config_file, store,
  70         overwrite=False, hooks=False, hook_type='pre-commit',
  71         skip_on_missing_conf=False,
  72 ):
  73     """Install the pre-commit hooks."""
  74     if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
  75         logger.error(
  76             'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
  77             'hint: `git config --unset-all core.hooksPath`',
  78         )
  79         return 1
  80 
  81     hook_path, legacy_path = _hook_paths(hook_type)
  82 
  83     mkdirp(os.path.dirname(hook_path))
  84 
  85     # If we have an existing hook, move it to pre-commit.legacy
  86     if os.path.lexists(hook_path) and not is_our_script(hook_path):
  87         os.rename(hook_path, legacy_path)
  88 
  89     # If we specify overwrite, we simply delete the legacy file
  90     if overwrite and os.path.exists(legacy_path):
  91         os.remove(legacy_path)
  92     elif os.path.exists(legacy_path):
  93         output.write_line(
  94             'Running in migration mode with existing hooks at {}\n'
  95             'Use -f to use only pre-commit.'.format(legacy_path),
  96         )
  97 
  98     params = {
  99         'CONFIG': config_file,
  100         'HOOK_TYPE': hook_type,
  101         'INSTALL_PYTHON': sys.executable,
  102         'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
  103     }
  104 
  105     with io.open(hook_path, 'w') as hook_file:
  106         contents = resource_text('hook-tmpl')
  107         before, rest = contents.split(TEMPLATE_START)
  108         to_template, after = rest.split(TEMPLATE_END)
  109 
  110         before = before.replace('#!/usr/bin/env python3', shebang())
  111 
  112         hook_file.write(before + TEMPLATE_START)
  113         for line in to_template.splitlines():
  114             var = line.split()[0]
  115             hook_file.write('{} = {!r}\n'.format(var, params[var]))
  116         hook_file.write(TEMPLATE_END + after)
  117     make_executable(hook_path)
  118 
  119     output.write_line('pre-commit installed at {}'.format(hook_path))
  120 
  121     # If they requested we install all of the hooks, do so.
  122     if hooks:
  123         install_hooks(config_file, store)
  124 
  125     return 0
  126 
  127 
  128 def install_hooks(config_file, store):
  129     install_hook_envs(all_hooks(load_config(config_file), store), store)
  130 
  131 
  132 def uninstall(hook_type='pre-commit'):
  133     """Uninstall the pre-commit hooks."""
  134     hook_path, legacy_path = _hook_paths(hook_type)
  135 
  136     # If our file doesn't exist or it isn't ours, gtfo.
  137     if not os.path.exists(hook_path) or not is_our_script(hook_path):
  138         return 0
  139 
  140     os.remove(hook_path)
  141     output.write_line('{} uninstalled'.format(hook_type))
  142 
  143     if os.path.exists(legacy_path):
  144         os.rename(legacy_path, hook_path)
  145         output.write_line('Restored previous hooks to {}'.format(hook_path))
  146 
  147     return 0
  148
  ```
  --- 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/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py
  --- a/pre_commit/commands/install_uninstall.py
  +++ b/pre_commit/commands/install_uninstall.py
  @@ -5,6 +5,7 @@
   import itertools
   import logging
   import os.path
  +import shutil
   import sys
   
   from pre_commit import git
  @@ -84,7 +85,7 @@
   
       # If we have an existing hook, move it to pre-commit.legacy
       if os.path.lexists(hook_path) and not is_our_script(hook_path):
  -        os.rename(hook_path, legacy_path)
  +        shutil.move(hook_path, legacy_path)
   
       # If we specify overwrite, we simply delete the legacy file
       if overwrite and os.path.exists(legacy_path):
  
verification_info: {"golden_diff": "diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py\n--- a/pre_commit/commands/install_uninstall.py\n+++ b/pre_commit/commands/install_uninstall.py\n@@ -5,6 +5,7 @@\n import itertools\n import logging\n import os.path\n+import shutil\n import sys\n \n from pre_commit import git\n@@ -84,7 +85,7 @@\n \n     # If we have an existing hook, move it to pre-commit.legacy\n     if os.path.lexists(hook_path) and not is_our_script(hook_path):\n-        os.rename(hook_path, legacy_path)\n+        shutil.move(hook_path, legacy_path)\n \n     # If we specify overwrite, we simply delete the legacy file\n     if overwrite and os.path.exists(legacy_path):\n", "issue": "FileExistsError : [WinError 183] Cannot create a file when that file already exists\nversion : 1.15.2\r\nOS : Windows 10\r\n\r\n```console\r\n$ pre-commit install\r\n\r\nAn unexpected error has occurred: FileExistsError: [WinError 183] Cannot create a file when that file already exists: '.git\\\\hooks\\\\pre-commit' -> '.git\\\\hooks\\\\pre-commit.legacy'\r\n```\r\n\r\nwhen i tried second times this was the error.\n", "before_files": [{"content": "from __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport io\nimport itertools\nimport logging\nimport os.path\nimport sys\n\nfrom pre_commit import git\nfrom pre_commit import output\nfrom pre_commit.clientlib import load_config\nfrom pre_commit.repository import all_hooks\nfrom pre_commit.repository import install_hook_envs\nfrom pre_commit.util import cmd_output\nfrom pre_commit.util import make_executable\nfrom pre_commit.util import mkdirp\nfrom pre_commit.util import resource_text\n\n\nlogger = logging.getLogger(__name__)\n\n# This is used to identify the hook file we install\nPRIOR_HASHES = (\n    '4d9958c90bc262f47553e2c073f14cfe',\n    'd8ee923c46731b42cd95cc869add4062',\n    '49fd668cb42069aa1b6048464be5d395',\n    '79f09a650522a87b0da915d0d983b2de',\n    'e358c9dae00eac5d06b38dfdb1e33a8c',\n)\nCURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03'\nTEMPLATE_START = '# start templated\\n'\nTEMPLATE_END = '# end templated\\n'\n\n\ndef _hook_paths(hook_type):\n    pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)\n    return pth, '{}.legacy'.format(pth)\n\n\ndef is_our_script(filename):\n    if not os.path.exists(filename):  # pragma: windows no cover (symlink)\n        return False\n    with io.open(filename) as f:\n        contents = f.read()\n    return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)\n\n\ndef shebang():\n    if sys.platform == 'win32':\n        py = 'python'\n    else:\n        # Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`\n        path_choices = [p for p in os.defpath.split(os.pathsep) if p]\n        exe_choices = [\n            'python{}'.format('.'.join(str(v) for v in sys.version_info[:i]))\n            for i in range(3)\n        ]\n        for path, exe in itertools.product(path_choices, exe_choices):\n            if os.path.exists(os.path.join(path, exe)):\n                py = exe\n                break\n        else:\n            py = 'python'\n    return '#!/usr/bin/env {}'.format(py)\n\n\ndef install(\n        config_file, store,\n        overwrite=False, hooks=False, hook_type='pre-commit',\n        skip_on_missing_conf=False,\n):\n    \"\"\"Install the pre-commit hooks.\"\"\"\n    if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():\n        logger.error(\n            'Cowardly refusing to install hooks with `core.hooksPath` set.\\n'\n            'hint: `git config --unset-all core.hooksPath`',\n        )\n        return 1\n\n    hook_path, legacy_path = _hook_paths(hook_type)\n\n    mkdirp(os.path.dirname(hook_path))\n\n    # If we have an existing hook, move it to pre-commit.legacy\n    if os.path.lexists(hook_path) and not is_our_script(hook_path):\n        os.rename(hook_path, legacy_path)\n\n    # If we specify overwrite, we simply delete the legacy file\n    if overwrite and os.path.exists(legacy_path):\n        os.remove(legacy_path)\n    elif os.path.exists(legacy_path):\n        output.write_line(\n            'Running in migration mode with existing hooks at {}\\n'\n            'Use -f to use only pre-commit.'.format(legacy_path),\n        )\n\n    params = {\n        'CONFIG': config_file,\n        'HOOK_TYPE': hook_type,\n        'INSTALL_PYTHON': sys.executable,\n        'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,\n    }\n\n    with io.open(hook_path, 'w') as hook_file:\n        contents = resource_text('hook-tmpl')\n        before, rest = contents.split(TEMPLATE_START)\n        to_template, after = rest.split(TEMPLATE_END)\n\n        before = before.replace('#!/usr/bin/env python3', shebang())\n\n        hook_file.write(before + TEMPLATE_START)\n        for line in to_template.splitlines():\n            var = line.split()[0]\n            hook_file.write('{} = {!r}\\n'.format(var, params[var]))\n        hook_file.write(TEMPLATE_END + after)\n    make_executable(hook_path)\n\n    output.write_line('pre-commit installed at {}'.format(hook_path))\n\n    # If they requested we install all of the hooks, do so.\n    if hooks:\n        install_hooks(config_file, store)\n\n    return 0\n\n\ndef install_hooks(config_file, store):\n    install_hook_envs(all_hooks(load_config(config_file), store), store)\n\n\ndef uninstall(hook_type='pre-commit'):\n    \"\"\"Uninstall the pre-commit hooks.\"\"\"\n    hook_path, legacy_path = _hook_paths(hook_type)\n\n    # If our file doesn't exist or it isn't ours, gtfo.\n    if not os.path.exists(hook_path) or not is_our_script(hook_path):\n        return 0\n\n    os.remove(hook_path)\n    output.write_line('{} uninstalled'.format(hook_type))\n\n    if os.path.exists(legacy_path):\n        os.rename(legacy_path, hook_path)\n        output.write_line('Restored previous hooks to {}'.format(hook_path))\n\n    return 0\n", "path": "pre_commit/commands/install_uninstall.py"}], "after_files": [{"content": "from __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport io\nimport itertools\nimport logging\nimport os.path\nimport shutil\nimport sys\n\nfrom pre_commit import git\nfrom pre_commit import output\nfrom pre_commit.clientlib import load_config\nfrom pre_commit.repository import all_hooks\nfrom pre_commit.repository import install_hook_envs\nfrom pre_commit.util import cmd_output\nfrom pre_commit.util import make_executable\nfrom pre_commit.util import mkdirp\nfrom pre_commit.util import resource_text\n\n\nlogger = logging.getLogger(__name__)\n\n# This is used to identify the hook file we install\nPRIOR_HASHES = (\n    '4d9958c90bc262f47553e2c073f14cfe',\n    'd8ee923c46731b42cd95cc869add4062',\n    '49fd668cb42069aa1b6048464be5d395',\n    '79f09a650522a87b0da915d0d983b2de',\n    'e358c9dae00eac5d06b38dfdb1e33a8c',\n)\nCURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03'\nTEMPLATE_START = '# start templated\\n'\nTEMPLATE_END = '# end templated\\n'\n\n\ndef _hook_paths(hook_type):\n    pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)\n    return pth, '{}.legacy'.format(pth)\n\n\ndef is_our_script(filename):\n    if not os.path.exists(filename):  # pragma: windows no cover (symlink)\n        return False\n    with io.open(filename) as f:\n        contents = f.read()\n    return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)\n\n\ndef shebang():\n    if sys.platform == 'win32':\n        py = 'python'\n    else:\n        # Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`\n        path_choices = [p for p in os.defpath.split(os.pathsep) if p]\n        exe_choices = [\n            'python{}'.format('.'.join(str(v) for v in sys.version_info[:i]))\n            for i in range(3)\n        ]\n        for path, exe in itertools.product(path_choices, exe_choices):\n            if os.path.exists(os.path.join(path, exe)):\n                py = exe\n                break\n        else:\n            py = 'python'\n    return '#!/usr/bin/env {}'.format(py)\n\n\ndef install(\n        config_file, store,\n        overwrite=False, hooks=False, hook_type='pre-commit',\n        skip_on_missing_conf=False,\n):\n    \"\"\"Install the pre-commit hooks.\"\"\"\n    if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():\n        logger.error(\n            'Cowardly refusing to install hooks with `core.hooksPath` set.\\n'\n            'hint: `git config --unset-all core.hooksPath`',\n        )\n        return 1\n\n    hook_path, legacy_path = _hook_paths(hook_type)\n\n    mkdirp(os.path.dirname(hook_path))\n\n    # If we have an existing hook, move it to pre-commit.legacy\n    if os.path.lexists(hook_path) and not is_our_script(hook_path):\n        shutil.move(hook_path, legacy_path)\n\n    # If we specify overwrite, we simply delete the legacy file\n    if overwrite and os.path.exists(legacy_path):\n        os.remove(legacy_path)\n    elif os.path.exists(legacy_path):\n        output.write_line(\n            'Running in migration mode with existing hooks at {}\\n'\n            'Use -f to use only pre-commit.'.format(legacy_path),\n        )\n\n    params = {\n        'CONFIG': config_file,\n        'HOOK_TYPE': hook_type,\n        'INSTALL_PYTHON': sys.executable,\n        'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,\n    }\n\n    with io.open(hook_path, 'w') as hook_file:\n        contents = resource_text('hook-tmpl')\n        before, rest = contents.split(TEMPLATE_START)\n        to_template, after = rest.split(TEMPLATE_END)\n\n        before = before.replace('#!/usr/bin/env python3', shebang())\n\n        hook_file.write(before + TEMPLATE_START)\n        for line in to_template.splitlines():\n            var = line.split()[0]\n            hook_file.write('{} = {!r}\\n'.format(var, params[var]))\n        hook_file.write(TEMPLATE_END + after)\n    make_executable(hook_path)\n\n    output.write_line('pre-commit installed at {}'.format(hook_path))\n\n    # If they requested we install all of the hooks, do so.\n    if hooks:\n        install_hooks(config_file, store)\n\n    return 0\n\n\ndef install_hooks(config_file, store):\n    install_hook_envs(all_hooks(load_config(config_file), store), store)\n\n\ndef uninstall(hook_type='pre-commit'):\n    \"\"\"Uninstall the pre-commit hooks.\"\"\"\n    hook_path, legacy_path = _hook_paths(hook_type)\n\n    # If our file doesn't exist or it isn't ours, gtfo.\n    if not os.path.exists(hook_path) or not is_our_script(hook_path):\n        return 0\n\n    os.remove(hook_path)\n    output.write_line('{} uninstalled'.format(hook_type))\n\n    if os.path.exists(legacy_path):\n        os.rename(legacy_path, hook_path)\n        output.write_line('Restored previous hooks to {}'.format(hook_path))\n\n    return 0\n", "path": "pre_commit/commands/install_uninstall.py"}]}