prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Some versions of git don't create .git/hooks directory
  Noticed here: https://github.com/victorlin/bugbuzz-python/pull/1#issuecomment-104971132
  
  
  --- 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 logging
  6 import os
  7 import os.path
  8 import stat
  9 import sys
  10 
  11 from pre_commit.logging_handler import LoggingHandler
  12 from pre_commit.util import resource_filename
  13 
  14 
  15 logger = logging.getLogger('pre_commit')
  16 
  17 
  18 # This is used to identify the hook file we install
  19 PREVIOUS_IDENTIFYING_HASHES = (
  20     '4d9958c90bc262f47553e2c073f14cfe',
  21     'd8ee923c46731b42cd95cc869add4062',
  22     '49fd668cb42069aa1b6048464be5d395',
  23 )
  24 
  25 
  26 IDENTIFYING_HASH = '79f09a650522a87b0da915d0d983b2de'
  27 
  28 
  29 def is_our_pre_commit(filename):
  30     return IDENTIFYING_HASH in io.open(filename).read()
  31 
  32 
  33 def is_previous_pre_commit(filename):
  34     contents = io.open(filename).read()
  35     return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
  36 
  37 
  38 def make_executable(filename):
  39     original_mode = os.stat(filename).st_mode
  40     os.chmod(
  41         filename,
  42         original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
  43     )
  44 
  45 
  46 def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
  47     """Install the pre-commit hooks."""
  48     hook_path = runner.get_hook_path(hook_type)
  49     legacy_path = hook_path + '.legacy'
  50 
  51     # If we have an existing hook, move it to pre-commit.legacy
  52     if (
  53         os.path.exists(hook_path) and
  54         not is_our_pre_commit(hook_path) and
  55         not is_previous_pre_commit(hook_path)
  56     ):
  57         os.rename(hook_path, legacy_path)
  58 
  59     # If we specify overwrite, we simply delete the legacy file
  60     if overwrite and os.path.exists(legacy_path):
  61         os.remove(legacy_path)
  62     elif os.path.exists(legacy_path):
  63         print(
  64             'Running in migration mode with existing hooks at {0}\n'
  65             'Use -f to use only pre-commit.'.format(
  66                 legacy_path,
  67             )
  68         )
  69 
  70     with io.open(hook_path, 'w') as pre_commit_file_obj:
  71         if hook_type == 'pre-push':
  72             with io.open(resource_filename('pre-push-tmpl')) as fp:
  73                 pre_push_contents = fp.read()
  74         else:
  75             pre_push_contents = ''
  76 
  77         contents = io.open(resource_filename('hook-tmpl')).read().format(
  78             sys_executable=sys.executable,
  79             hook_type=hook_type,
  80             pre_push=pre_push_contents,
  81         )
  82         pre_commit_file_obj.write(contents)
  83     make_executable(hook_path)
  84 
  85     print('pre-commit installed at {0}'.format(hook_path))
  86 
  87     # If they requested we install all of the hooks, do so.
  88     if hooks:
  89         # Set up our logging handler
  90         logger.addHandler(LoggingHandler(False))
  91         logger.setLevel(logging.INFO)
  92         for repository in runner.repositories:
  93             repository.require_installed()
  94 
  95     return 0
  96 
  97 
  98 def uninstall(runner, hook_type='pre-commit'):
  99     """Uninstall the pre-commit hooks."""
  100     hook_path = runner.get_hook_path(hook_type)
  101     legacy_path = hook_path + '.legacy'
  102     # If our file doesn't exist or it isn't ours, gtfo.
  103     if (
  104         not os.path.exists(hook_path) or (
  105             not is_our_pre_commit(hook_path) and
  106             not is_previous_pre_commit(hook_path)
  107         )
  108     ):
  109         return 0
  110 
  111     os.remove(hook_path)
  112     print('{0} uninstalled'.format(hook_type))
  113 
  114     if os.path.exists(legacy_path):
  115         os.rename(legacy_path, hook_path)
  116         print('Restored previous hooks to {0}'.format(hook_path))
  117 
  118     return 0
  119
  ```
  --- 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
  @@ -48,6 +48,9 @@
       hook_path = runner.get_hook_path(hook_type)
       legacy_path = hook_path + '.legacy'
   
  +    if not os.path.exists(os.path.dirname(hook_path)):
  +        os.makedirs(os.path.dirname(hook_path))
  +
       # If we have an existing hook, move it to pre-commit.legacy
       if (
           os.path.exists(hook_path) and
  
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@@ -48,6 +48,9 @@\n     hook_path = runner.get_hook_path(hook_type)\n     legacy_path = hook_path + '.legacy'\n \n+    if not os.path.exists(os.path.dirname(hook_path)):\n+        os.makedirs(os.path.dirname(hook_path))\n+\n     # If we have an existing hook, move it to pre-commit.legacy\n     if (\n         os.path.exists(hook_path) and\n", "issue": "Some versions of git don't create .git/hooks directory\nNoticed here: https://github.com/victorlin/bugbuzz-python/pull/1#issuecomment-104971132\n\n", "before_files": [{"content": "from __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport io\nimport logging\nimport os\nimport os.path\nimport stat\nimport sys\n\nfrom pre_commit.logging_handler import LoggingHandler\nfrom pre_commit.util import resource_filename\n\n\nlogger = logging.getLogger('pre_commit')\n\n\n# This is used to identify the hook file we install\nPREVIOUS_IDENTIFYING_HASHES = (\n    '4d9958c90bc262f47553e2c073f14cfe',\n    'd8ee923c46731b42cd95cc869add4062',\n    '49fd668cb42069aa1b6048464be5d395',\n)\n\n\nIDENTIFYING_HASH = '79f09a650522a87b0da915d0d983b2de'\n\n\ndef is_our_pre_commit(filename):\n    return IDENTIFYING_HASH in io.open(filename).read()\n\n\ndef is_previous_pre_commit(filename):\n    contents = io.open(filename).read()\n    return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)\n\n\ndef make_executable(filename):\n    original_mode = os.stat(filename).st_mode\n    os.chmod(\n        filename,\n        original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,\n    )\n\n\ndef install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):\n    \"\"\"Install the pre-commit hooks.\"\"\"\n    hook_path = runner.get_hook_path(hook_type)\n    legacy_path = hook_path + '.legacy'\n\n    # If we have an existing hook, move it to pre-commit.legacy\n    if (\n        os.path.exists(hook_path) and\n        not is_our_pre_commit(hook_path) and\n        not is_previous_pre_commit(hook_path)\n    ):\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        print(\n            'Running in migration mode with existing hooks at {0}\\n'\n            'Use -f to use only pre-commit.'.format(\n                legacy_path,\n            )\n        )\n\n    with io.open(hook_path, 'w') as pre_commit_file_obj:\n        if hook_type == 'pre-push':\n            with io.open(resource_filename('pre-push-tmpl')) as fp:\n                pre_push_contents = fp.read()\n        else:\n            pre_push_contents = ''\n\n        contents = io.open(resource_filename('hook-tmpl')).read().format(\n            sys_executable=sys.executable,\n            hook_type=hook_type,\n            pre_push=pre_push_contents,\n        )\n        pre_commit_file_obj.write(contents)\n    make_executable(hook_path)\n\n    print('pre-commit installed at {0}'.format(hook_path))\n\n    # If they requested we install all of the hooks, do so.\n    if hooks:\n        # Set up our logging handler\n        logger.addHandler(LoggingHandler(False))\n        logger.setLevel(logging.INFO)\n        for repository in runner.repositories:\n            repository.require_installed()\n\n    return 0\n\n\ndef uninstall(runner, hook_type='pre-commit'):\n    \"\"\"Uninstall the pre-commit hooks.\"\"\"\n    hook_path = runner.get_hook_path(hook_type)\n    legacy_path = hook_path + '.legacy'\n    # If our file doesn't exist or it isn't ours, gtfo.\n    if (\n        not os.path.exists(hook_path) or (\n            not is_our_pre_commit(hook_path) and\n            not is_previous_pre_commit(hook_path)\n        )\n    ):\n        return 0\n\n    os.remove(hook_path)\n    print('{0} uninstalled'.format(hook_type))\n\n    if os.path.exists(legacy_path):\n        os.rename(legacy_path, hook_path)\n        print('Restored previous hooks to {0}'.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 logging\nimport os\nimport os.path\nimport stat\nimport sys\n\nfrom pre_commit.logging_handler import LoggingHandler\nfrom pre_commit.util import resource_filename\n\n\nlogger = logging.getLogger('pre_commit')\n\n\n# This is used to identify the hook file we install\nPREVIOUS_IDENTIFYING_HASHES = (\n    '4d9958c90bc262f47553e2c073f14cfe',\n    'd8ee923c46731b42cd95cc869add4062',\n    '49fd668cb42069aa1b6048464be5d395',\n)\n\n\nIDENTIFYING_HASH = '79f09a650522a87b0da915d0d983b2de'\n\n\ndef is_our_pre_commit(filename):\n    return IDENTIFYING_HASH in io.open(filename).read()\n\n\ndef is_previous_pre_commit(filename):\n    contents = io.open(filename).read()\n    return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)\n\n\ndef make_executable(filename):\n    original_mode = os.stat(filename).st_mode\n    os.chmod(\n        filename,\n        original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,\n    )\n\n\ndef install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):\n    \"\"\"Install the pre-commit hooks.\"\"\"\n    hook_path = runner.get_hook_path(hook_type)\n    legacy_path = hook_path + '.legacy'\n\n    if not os.path.exists(os.path.dirname(hook_path)):\n        os.makedirs(os.path.dirname(hook_path))\n\n    # If we have an existing hook, move it to pre-commit.legacy\n    if (\n        os.path.exists(hook_path) and\n        not is_our_pre_commit(hook_path) and\n        not is_previous_pre_commit(hook_path)\n    ):\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        print(\n            'Running in migration mode with existing hooks at {0}\\n'\n            'Use -f to use only pre-commit.'.format(\n                legacy_path,\n            )\n        )\n\n    with io.open(hook_path, 'w') as pre_commit_file_obj:\n        if hook_type == 'pre-push':\n            with io.open(resource_filename('pre-push-tmpl')) as fp:\n                pre_push_contents = fp.read()\n        else:\n            pre_push_contents = ''\n\n        contents = io.open(resource_filename('hook-tmpl')).read().format(\n            sys_executable=sys.executable,\n            hook_type=hook_type,\n            pre_push=pre_push_contents,\n        )\n        pre_commit_file_obj.write(contents)\n    make_executable(hook_path)\n\n    print('pre-commit installed at {0}'.format(hook_path))\n\n    # If they requested we install all of the hooks, do so.\n    if hooks:\n        # Set up our logging handler\n        logger.addHandler(LoggingHandler(False))\n        logger.setLevel(logging.INFO)\n        for repository in runner.repositories:\n            repository.require_installed()\n\n    return 0\n\n\ndef uninstall(runner, hook_type='pre-commit'):\n    \"\"\"Uninstall the pre-commit hooks.\"\"\"\n    hook_path = runner.get_hook_path(hook_type)\n    legacy_path = hook_path + '.legacy'\n    # If our file doesn't exist or it isn't ours, gtfo.\n    if (\n        not os.path.exists(hook_path) or (\n            not is_our_pre_commit(hook_path) and\n            not is_previous_pre_commit(hook_path)\n        )\n    ):\n        return 0\n\n    os.remove(hook_path)\n    print('{0} uninstalled'.format(hook_type))\n\n    if os.path.exists(legacy_path):\n        os.rename(legacy_path, hook_path)\n        print('Restored previous hooks to {0}'.format(hook_path))\n\n    return 0\n", "path": "pre_commit/commands/install_uninstall.py"}]}